Skip to content

Commit b456a43

Browse files
authored
Merge pull request #5 from envor/main
cleanup code
2 parents bd59aff + 83883a3 commit b456a43

9 files changed

+141
-125
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-schema-macros` will be documented in this file.
44

5+
## v1.0.2 - 2024-02-04
6+
7+
**Full Changelog**: https://github.com/envor/laravel-schema-macros/compare/v1.0.0...v1.0.2
8+
59
## v1.0.1 - 2024-02-04
610

711
### What's Changed

phpstan-baseline.neon

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@ parameters:
55
count: 1
66
path: src/MySql/MySqlCreateDatabaseIfNotExists.php
77

8-
-
9-
message: "#^Call to method getConnection\\(\\) on an unknown class Illuminate\\\\Database\\\\MySqlBuilder\\.$#"
10-
count: 1
11-
path: src/MySql/MySqlDatabaseExists.php
12-
13-
-
14-
message: "#^PHPDoc tag @var for variable \\$this contains unknown class Illuminate\\\\Database\\\\MySqlBuilder\\.$#"
15-
count: 1
16-
path: src/MySql/MySqlDatabaseExists.php
17-
188
-
199
message: "#^Cannot cast mixed to string\\.$#"
2010
count: 1
@@ -31,26 +21,36 @@ parameters:
3121
path: src/MySql/MySqlTrashDatabase.php
3222

3323
-
34-
message: "#^Negated boolean expression is always true\\.$#"
24+
message: "#^Cannot call method make\\(\\) on mixed\\.$#"
3525
count: 1
36-
path: src/SQLite/SQLiteCreateDatabaseIfNotExists.php
26+
path: src/SchemaMacros.php
3727

3828
-
39-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
29+
message: "#^Cannot call method singleton\\(\\) on mixed\\.$#"
4030
count: 1
41-
path: src/SQLite/SQLiteCreateDatabaseIfNotExists.php
31+
path: src/SchemaMacros.php
4232

4333
-
44-
message: "#^Instanceof between \\$this\\(Envor\\\\SchemaMacros\\\\SchemaMacrosServiceProvider\\) and Illuminate\\\\Database\\\\Schema\\\\MySqlBuilder will always evaluate to false\\.$#"
45-
count: 4
46-
path: src/SchemaMacrosServiceProvider.php
34+
message: "#^Method Envor\\\\SchemaMacros\\\\SchemaMacros\\:\\:getMacroForDriver\\(\\) has parameter \\$args with no type specified\\.$#"
35+
count: 1
36+
path: src/SchemaMacros.php
4737

4838
-
49-
message: "#^Instanceof between \\$this\\(Envor\\\\SchemaMacros\\\\SchemaMacrosServiceProvider\\) and Illuminate\\\\Database\\\\Schema\\\\SQLiteBuilder will always evaluate to false\\.$#"
50-
count: 4
51-
path: src/SchemaMacrosServiceProvider.php
39+
message: "#^Method Envor\\\\SchemaMacros\\\\SchemaMacros\\:\\:registerMacro\\(\\) has parameter \\$args with no type specified\\.$#"
40+
count: 1
41+
path: src/SchemaMacros.php
5242

5343
-
5444
message: "#^Trying to invoke mixed but it's not a callable\\.$#"
55-
count: 2
56-
path: src/SchemaMacrosServiceProvider.php
45+
count: 1
46+
path: src/SchemaMacros.php
47+
48+
-
49+
message: "#^Undefined variable\\: \\$this$#"
50+
count: 1
51+
path: src/SchemaMacros.php
52+
53+
-
54+
message: "#^Class Envor\\\\SchemaMacros\\\\SchemaMacrosCollection extends generic class Illuminate\\\\Support\\\\Collection but does not specify its types\\: TKey, TValue$#"
55+
count: 1
56+
path: src/SchemaMacrosCollection.php

src/MySql/MySqlDatabaseExists.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __invoke(): callable
2020
return function (string|Stringable $database): bool {
2121
$database = (string) $database;
2222

23-
/** @var \Illuminate\Database\MySqlBuilder $this */
23+
/** @var \Illuminate\Database\Schema\MySqlBuilder $this */
2424
return (bool) $this->getConnection()->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{$database}'");
2525
};
2626
}

src/SQLite/SQLiteCreateDatabaseIfNotExists.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,8 @@ public function __invoke(): callable
3232
File::makeDirectory($directory, 0755, true);
3333
}
3434

35-
if (! File::exists($database)) {
36-
37-
/** @var \Illuminate\Database\Schema\SQLiteBuilder $this */
38-
return $this->createDatabase($database);
39-
}
40-
41-
return false;
35+
/** @var \Illuminate\Database\Schema\SQLiteBuilder $this */
36+
return $this->createDatabase($database);
4237
};
4338
}
4439
}

src/SQLite/SQLiteEmptyTrash.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class SQLiteEmptyTrash
1919
{
2020
public function __invoke(): callable
2121
{
22-
return function (int $daysOld, $trashDisk): int|bool {
22+
return function (int $daysOld = 0, $trashDisk = 'local'): int|bool {
2323
$trashPath = Storage::disk($trashDisk)->path('.trash');
2424

2525
if (! File::isDirectory($trashPath)) {

src/SchemaMacros.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Envor\SchemaMacros;
44

5+
use Illuminate\Database\Schema\Builder;
6+
use Illuminate\Support\Collection;
7+
58
class SchemaMacros
69
{
710
/**
@@ -10,4 +13,73 @@ class SchemaMacros
1013
* @var string
1114
*/
1215
public const TRASH_DATE_FORMAT = 'Y-m-d_H-i-s';
16+
17+
/**
18+
* @return array<string, array<string, string>>
19+
*/
20+
public static function macros(): array
21+
{
22+
return app()->make(SchemaMacrosCollection::class)->toArray();
23+
}
24+
25+
public static function registerMacros(): mixed
26+
{
27+
return Collection::make(static::macros())
28+
->keys()
29+
->reject(fn ($macro) => Builder::hasMacro($macro))
30+
->each(fn ($macro) => Builder::macro($macro, fn (...$args) => SchemaMacros::registerMacro($macro, $this, ...$args)));
31+
}
32+
33+
/**
34+
* @param array<string, array<string, string>> $macros
35+
*/
36+
public static function registerMacrosUsing(array $macros): mixed
37+
{
38+
return app()->singleton(SchemaMacrosCollection::class, fn () => new SchemaMacrosCollection($macros));
39+
}
40+
41+
/**
42+
* @return array<string>
43+
*/
44+
public static function supportedDrivers(string $macro): array
45+
{
46+
return array_keys(static::macros()[$macro]);
47+
}
48+
49+
protected static function validDriver(string $macro, string $driver): bool
50+
{
51+
return in_array($driver, static::supportedDrivers($macro));
52+
}
53+
54+
protected static function ensureDriverIsSupported(string $macro, Builder $builder): bool
55+
{
56+
if (! static::validDriver($macro, $driver = $builder->getConnection()->getDriverName())) {
57+
$supportedDrivers = implode(', ', static::supportedDrivers($macro));
58+
throw new UnsupportedDriver("The {$macro}() macro does not support {$driver}. Supported drivers are: {$supportedDrivers}");
59+
}
60+
61+
return true;
62+
}
63+
64+
public static function registerMacro(string $macro, Builder $builder, ...$args): mixed
65+
{
66+
static::ensureDriverIsSupported($macro, $builder);
67+
68+
return Collection::make(static::supportedDrivers($macro))
69+
->filter(fn ($supportedDriver) => $builder->getConnection()->getDriverName() === $supportedDriver)
70+
->map(fn ($driver) => static::getMacroForDriver($driver, $macro, $builder, ...$args))
71+
->first();
72+
}
73+
74+
protected static function getMacroForDriver(string $driver, string $macro, Builder $builder, ...$args): mixed
75+
{
76+
static::registerMacroForDriver($driver, $macro);
77+
78+
return $builder->{$driver.ucfirst($macro)}(...$args);
79+
}
80+
81+
protected static function registerMacroForDriver(string $driver, string $macro): void
82+
{
83+
Builder::macro($driver.ucfirst($macro), app(static::macros()[$macro][$driver])());
84+
}
1385
}

src/SchemaMacrosCollection.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Envor\SchemaMacros;
4+
5+
use Illuminate\Support\Collection;
6+
7+
class SchemaMacrosCollection extends Collection
8+
{
9+
}

src/SchemaMacrosServiceProvider.php

Lines changed: 21 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22

33
namespace Envor\SchemaMacros;
44

5-
use Illuminate\Database\Schema\Builder;
6-
use Illuminate\Database\Schema\MySqlBuilder;
7-
use Illuminate\Database\Schema\SQLiteBuilder;
8-
use Illuminate\Support\Collection;
95
use Spatie\LaravelPackageTools\Package;
106
use Spatie\LaravelPackageTools\PackageServiceProvider;
11-
use Stringable;
127

138
class SchemaMacrosServiceProvider extends PackageServiceProvider
149
{
@@ -23,103 +18,35 @@ public function configurePackage(Package $package): void
2318
->name('laravel-schema-macros');
2419
}
2520

26-
public function packageBooted(): void
21+
public function packageRegistered(): void
2722
{
28-
Collection::make($this->sqliteBuilderMacros())
29-
->reject(fn ($class, $macro) => Builder::hasMacro($macro))
30-
->each(fn ($class, $macro) => Builder::macro($macro, app($class)()));
23+
SchemaMacros::registerMacrosUsing($this->macros());
3124

32-
Collection::make($this->mysqlBuilderMacros())
33-
->reject(fn ($class, $macro) => Builder::hasMacro($macro))
34-
->each(fn ($class, $macro) => Builder::macro($macro, app($class)()));
35-
36-
Collection::make($this->builderMacros())
37-
->reject(fn ($macro, $name) => Builder::hasMacro($name))
38-
->each(fn ($macro, $name) => Builder::macro($name, $macro));
39-
}
40-
41-
/**
42-
* @return array<string, callable>
43-
*/
44-
private function builderMacros(): array
45-
{
46-
return [
47-
'databaseExists' => function (string|Stringable $database) {
48-
$database = (string) $database;
49-
50-
if ($this instanceof MySqlBuilder) {
51-
return $this->mysqlDatabaseExists($database);
52-
}
53-
54-
if ($this instanceof SQLiteBuilder) {
55-
return $this->sqliteDatabaseExists($database);
56-
}
57-
58-
throw new \Exception('The databaseExists() macro does not support'.get_class($this));
59-
},
60-
'createDatabaseIfNotExists' => function (string|Stringable $database, bool $recursive = true) {
61-
$database = (string) $database;
62-
63-
if ($this instanceof MySqlBuilder) {
64-
return $this->mysqlCreateDatabaseIfNotExists($database);
65-
}
66-
67-
if ($this instanceof SQLiteBuilder) {
68-
return $this->sqliteCreateDatabaseIfNotExists($database, $recursive);
69-
}
70-
71-
throw new \Exception('The createDatabaseIfNotExists() macro does not support'.get_class($this));
72-
},
73-
'trashDatabase' => function (string|Stringable $database, string $trashDisk = 'local') {
74-
$database = (string) $database;
75-
76-
if ($this instanceof MySqlBuilder) {
77-
return $this->mysqlTrashDatabase($database);
78-
}
79-
80-
if ($this instanceof SQLiteBuilder) {
81-
return $this->sqliteTrashDatabase($database, $trashDisk);
82-
}
83-
84-
throw new \Exception('The trashDatabase() macro does not support'.get_class($this));
85-
},
86-
'emptyTrash' => function (int $daysOld = 0, string $trashDisk = 'local') {
87-
if ($this instanceof MySqlBuilder) {
88-
return $this->mysqlEmptyTrash($daysOld);
89-
}
90-
91-
if ($this instanceof SQLiteBuilder) {
92-
return $this->sqliteEmptyTrash($daysOld, $trashDisk);
93-
}
94-
95-
throw new \Exception('The emptyTrash() macro does not support'.get_class($this));
96-
},
97-
];
98-
}
99-
100-
/**
101-
* @return array<string, string>
102-
*/
103-
private function sqliteBuilderMacros(): array
104-
{
105-
return [
106-
'sqliteDatabaseExists' => \Envor\SchemaMacros\SQLite\SQLiteDatabaseExists::class,
107-
'sqliteCreateDatabaseIfNotExists' => \Envor\SchemaMacros\SQLite\SQLiteCreateDatabaseIfNotExists::class,
108-
'sqliteTrashDatabase' => \Envor\SchemaMacros\SQLite\SQLiteTrashDatabase::class,
109-
'sqliteEmptyTrash' => \Envor\SchemaMacros\SQLite\SQLiteEmptyTrash::class,
110-
];
25+
SchemaMacros::registerMacros();
11126
}
11227

11328
/**
114-
* @return array<string, string>
29+
* @return array<string, array<string, string>>
11530
*/
116-
private function mysqlBuilderMacros(): array
31+
private function macros(): array
11732
{
11833
return [
119-
'mysqlDatabaseExists' => \Envor\SchemaMacros\MySql\MySqlDatabaseExists::class,
120-
'mysqlCreateDatabaseIfNotExists' => \Envor\SchemaMacros\MySql\MySqlCreateDatabaseIfNotExists::class,
121-
'mysqlTrashDatabase' => \Envor\SchemaMacros\MySql\MySqlTrashDatabase::class,
122-
'mysqlEmptyTrash' => \Envor\SchemaMacros\MySql\MySqlEmptyTrash::class,
34+
'databaseExists' => [
35+
'sqlite' => \Envor\SchemaMacros\SQLite\SQLiteDatabaseExists::class,
36+
'mysql' => \Envor\SchemaMacros\MySql\MySqlDatabaseExists::class,
37+
],
38+
'createDatabaseIfNotExists' => [
39+
'sqlite' => \Envor\SchemaMacros\SQLite\SQLiteCreateDatabaseIfNotExists::class,
40+
'mysql' => \Envor\SchemaMacros\MySql\MySqlCreateDatabaseIfNotExists::class,
41+
],
42+
'trashDatabase' => [
43+
'sqlite' => \Envor\SchemaMacros\SQLite\SQLiteTrashDatabase::class,
44+
'mysql' => \Envor\SchemaMacros\MySql\MySqlTrashDatabase::class,
45+
],
46+
'emptyTrash' => [
47+
'sqlite' => \Envor\SchemaMacros\SQLite\SQLiteEmptyTrash::class,
48+
'mysql' => \Envor\SchemaMacros\MySql\MySqlEmptyTrash::class,
49+
],
12350
];
12451
}
12552
}

src/UnsupportedDriver.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Envor\SchemaMacros;
4+
5+
use Exception;
6+
7+
class UnsupportedDriver extends Exception
8+
{
9+
}

0 commit comments

Comments
 (0)