Skip to content

Commit 962d7d1

Browse files
committed
feat: new implementation based on tpetry/php-mysql-explain
1 parent 1a00db4 commit 962d7d1

22 files changed

+262
-430
lines changed

.github/workflows/unit-tests.yml

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ on: [push, pull_request]
55
jobs:
66
test:
77
runs-on: ubuntu-latest
8+
services:
9+
database:
10+
image: container-registry.oracle.com/mysql/community-server:8.0
11+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
12+
env:
13+
MYSQL_DATABASE: testing
14+
MYSQL_USER: testing
15+
MYSQL_PASSWORD: testing
16+
ports:
17+
- 3306:3306
818
strategy:
919
fail-fast: false
1020
matrix:
@@ -68,3 +78,7 @@ jobs:
6878

6979
- name: Execute tests
7080
run: vendor/bin/phpunit
81+
env:
82+
DB_DATABASE: testing
83+
DB_USERNAME: testing
84+
DB_PASSWORD: testing

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Film::where('description', 'like', '%astronaut%')
5555
In some cases you are executing raw SQL queries and don't use the query builder. You can use the `MysqlExplain` facade to get the EXPLAIN url for them:
5656

5757
```php
58-
use Tpetry\MysqlExplain\Facades\MysqlExplain;
58+
use Tpetry\LaravelMysqlExplain\Facades\MysqlExplain;
5959

6060
// $url will be e.g. https://mysqlexplain.com/explain/01j2gctgtheyva7a7mhpv8azje
6161
$url = MysqlExplain::submitQuery(

composer.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
"require": {
1919
"php": "^8.0",
2020
"ext-pdo": "*",
21-
"guzzlehttp/guzzle": "^6.0|^7.0",
22-
"illuminate/contracts": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0"
21+
"guzzlehttp/guzzle": "^7.0",
22+
"illuminate/contracts": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
23+
"tpetry/php-mysql-explain": "^1.0"
2324
},
2425
"require-dev": {
2526
"mockery/mockery": "^1.6",
@@ -32,12 +33,12 @@
3233
},
3334
"autoload": {
3435
"psr-4": {
35-
"Tpetry\\MysqlExplain\\": "src/"
36+
"Tpetry\\LaravelMysqlExplain\\": "src/"
3637
}
3738
},
3839
"autoload-dev": {
3940
"psr-4": {
40-
"Tpetry\\MysqlExplain\\Tests\\": "tests/"
41+
"Tpetry\\LaravelMysqlExplain\\Tests\\": "tests/"
4142
}
4243
},
4344
"scripts": {
@@ -56,10 +57,10 @@
5657
"extra": {
5758
"laravel": {
5859
"providers": [
59-
"Tpetry\\MysqlExplain\\MysqlExplainServiceProvider"
60+
"Tpetry\\LaravelMysqlExplain\\MysqlExplainServiceProvider"
6061
],
6162
"aliases": {
62-
"MysqlExplain": "Tpetry\\MysqlExplain\\Facades\\MysqlExplain"
63+
"MysqlExplain": "Tpetry\\LaravelMysqlExplain\\Facades\\MysqlExplain"
6364
}
6465
}
6566
},

phpstan-baseline.neon

-21
This file was deleted.

phpstan.neon.dist

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
includes:
2-
- phpstan-baseline.neon
3-
41
parameters:
52
level: max
63
paths:

src/Facades/MysqlExplain.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
declare(strict_types=1);
44

5-
namespace Tpetry\MysqlExplain\Facades;
5+
namespace Tpetry\LaravelMysqlExplain\Facades;
66

77
use Illuminate\Support\Facades\Facade;
88

99
/**
1010
* @method static string submitBuilder(\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Contracts\Database\Query\Builder $builder)
1111
* @method static string submitQuery(\Illuminate\Database\ConnectionInterface $connection, string $sql, mixed[] $bindings = [])
1212
*
13-
* @see \Tpetry\MysqlExplain\MysqlExplain
13+
* @see \Tpetry\LaravelMysqlExplain\MysqlExplain
1414
*/
1515
class MysqlExplain extends Facade
1616
{
1717
protected static function getFacadeAccessor()
1818
{
19-
return \Tpetry\MysqlExplain\MysqlExplain::class;
19+
return \Tpetry\LaravelMysqlExplain\MysqlExplain::class;
2020
}
2121
}

src/Helpers/ApiHelper.php

-56
This file was deleted.

src/Helpers/DatabaseHelper.php

-59
This file was deleted.

src/LaravelQuery.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tpetry\LaravelMysqlExplain;
6+
7+
use Illuminate\Database\ConnectionInterface;
8+
use Tpetry\PhpMysqlExplain\Queries\QueryInterface;
9+
10+
class LaravelQuery implements QueryInterface
11+
{
12+
/**
13+
* @param array<array-key,mixed> $parameters
14+
*/
15+
public function __construct(
16+
private ConnectionInterface $connection,
17+
private string $sql,
18+
private array $parameters,
19+
) {}
20+
21+
public function execute(string $sql, bool $useParams): array
22+
{
23+
$rows = match ($useParams) {
24+
true => $this->connection->select($sql, $this->parameters),
25+
false => $this->connection->select($sql),
26+
};
27+
28+
// Laravel creates array<object> instead of array<arrays> as requested by interface.
29+
$rows = array_map(fn ($row) => (array) $row, $rows);
30+
31+
return $rows;
32+
}
33+
34+
public function getParameters(): array
35+
{
36+
// Transform special values like DateTimeInterface and bool to their string value.
37+
return $this->connection->prepareBindings($this->parameters);
38+
}
39+
40+
public function getSql(): string
41+
{
42+
return $this->sql;
43+
}
44+
45+
public function name(): string
46+
{
47+
return 'laravel@'.MysqlExplain::$VERSION;
48+
}
49+
}

src/Mixins/BuilderMixin.php

+8-16
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
declare(strict_types=1);
44

5-
namespace Tpetry\MysqlExplain\Mixins;
5+
namespace Tpetry\LaravelMysqlExplain\Mixins;
66

77
use Closure;
8-
use Tpetry\MysqlExplain\Facades\MysqlExplain;
8+
use Tpetry\LaravelMysqlExplain\Facades\MysqlExplain;
99

1010
/**
1111
* @internal
@@ -17,8 +17,7 @@ public function ddExplainForHumans(): Closure
1717
/** @return never-returns */
1818
return function () {
1919
/** @var \Illuminate\Contracts\Database\Query\Builder $this */
20-
$url = MysqlExplain::submitBuilder($this);
21-
dd($url);
20+
dd(MysqlExplain::submitBuilder($this));
2221
};
2322
}
2423

@@ -27,17 +26,15 @@ public function ddVisualExplain(): Closure
2726
/** @return never-returns */
2827
return function () {
2928
/** @var \Illuminate\Contracts\Database\Query\Builder $this */
30-
$url = MysqlExplain::submitBuilder($this);
31-
dd($url);
29+
dd(MysqlExplain::submitBuilder($this));
3230
};
3331
}
3432

3533
public function dumpExplainForHumans(): Closure
3634
{
3735
return function () {
3836
/** @var \Illuminate\Contracts\Database\Query\Builder $this */
39-
$url = MysqlExplain::submitBuilder($this);
40-
dump($url);
37+
dump(MysqlExplain::submitBuilder($this));
4138

4239
return $this;
4340
};
@@ -47,8 +44,7 @@ public function dumpVisualExplain(): Closure
4744
{
4845
return function () {
4946
/** @var \Illuminate\Contracts\Database\Query\Builder $this */
50-
$url = MysqlExplain::submitBuilder($this);
51-
dump($url);
47+
dump(MysqlExplain::submitBuilder($this));
5248

5349
return $this;
5450
};
@@ -58,19 +54,15 @@ public function explainForHumans(): Closure
5854
{
5955
return function (): string {
6056
/** @var \Illuminate\Contracts\Database\Query\Builder $this */
61-
$url = MysqlExplain::submitBuilder($this);
62-
63-
return $url;
57+
return MysqlExplain::submitBuilder($this);
6458
};
6559
}
6660

6761
public function visualExplain(): Closure
6862
{
6963
return function (): string {
7064
/** @var \Illuminate\Contracts\Database\Query\Builder $this */
71-
$url = MysqlExplain::submitBuilder($this);
72-
73-
return $url;
65+
return MysqlExplain::submitBuilder($this);
7466
};
7567
}
7668
}

0 commit comments

Comments
 (0)