Skip to content

Commit

Permalink
Merge pull request #5 from envor/main
Browse files Browse the repository at this point in the history
cleanup code
  • Loading branch information
inmanturbo authored Feb 5, 2024
2 parents bd59aff + 83883a3 commit b456a43
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 125 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## v1.0.2 - 2024-02-04

**Full Changelog**: https://github.com/envor/laravel-schema-macros/compare/v1.0.0...v1.0.2

## v1.0.1 - 2024-02-04

### What's Changed
Expand Down
44 changes: 22 additions & 22 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ parameters:
count: 1
path: src/MySql/MySqlCreateDatabaseIfNotExists.php

-
message: "#^Call to method getConnection\\(\\) on an unknown class Illuminate\\\\Database\\\\MySqlBuilder\\.$#"
count: 1
path: src/MySql/MySqlDatabaseExists.php

-
message: "#^PHPDoc tag @var for variable \\$this contains unknown class Illuminate\\\\Database\\\\MySqlBuilder\\.$#"
count: 1
path: src/MySql/MySqlDatabaseExists.php

-
message: "#^Cannot cast mixed to string\\.$#"
count: 1
Expand All @@ -31,26 +21,36 @@ parameters:
path: src/MySql/MySqlTrashDatabase.php

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

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

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

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

-
message: "#^Trying to invoke mixed but it's not a callable\\.$#"
count: 2
path: src/SchemaMacrosServiceProvider.php
count: 1
path: src/SchemaMacros.php

-
message: "#^Undefined variable\\: \\$this$#"
count: 1
path: src/SchemaMacros.php

-
message: "#^Class Envor\\\\SchemaMacros\\\\SchemaMacrosCollection extends generic class Illuminate\\\\Support\\\\Collection but does not specify its types\\: TKey, TValue$#"
count: 1
path: src/SchemaMacrosCollection.php
2 changes: 1 addition & 1 deletion src/MySql/MySqlDatabaseExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __invoke(): callable
return function (string|Stringable $database): bool {
$database = (string) $database;

/** @var \Illuminate\Database\MySqlBuilder $this */
/** @var \Illuminate\Database\Schema\MySqlBuilder $this */
return (bool) $this->getConnection()->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{$database}'");
};
}
Expand Down
9 changes: 2 additions & 7 deletions src/SQLite/SQLiteCreateDatabaseIfNotExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@ public function __invoke(): callable
File::makeDirectory($directory, 0755, true);
}

if (! File::exists($database)) {

/** @var \Illuminate\Database\Schema\SQLiteBuilder $this */
return $this->createDatabase($database);
}

return false;
/** @var \Illuminate\Database\Schema\SQLiteBuilder $this */
return $this->createDatabase($database);
};
}
}
2 changes: 1 addition & 1 deletion src/SQLite/SQLiteEmptyTrash.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SQLiteEmptyTrash
{
public function __invoke(): callable
{
return function (int $daysOld, $trashDisk): int|bool {
return function (int $daysOld = 0, $trashDisk = 'local'): int|bool {
$trashPath = Storage::disk($trashDisk)->path('.trash');

if (! File::isDirectory($trashPath)) {
Expand Down
72 changes: 72 additions & 0 deletions src/SchemaMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Envor\SchemaMacros;

use Illuminate\Database\Schema\Builder;
use Illuminate\Support\Collection;

class SchemaMacros
{
/**
Expand All @@ -10,4 +13,73 @@ class SchemaMacros
* @var string
*/
public const TRASH_DATE_FORMAT = 'Y-m-d_H-i-s';

/**
* @return array<string, array<string, string>>
*/
public static function macros(): array
{
return app()->make(SchemaMacrosCollection::class)->toArray();
}

public static function registerMacros(): mixed
{
return Collection::make(static::macros())
->keys()
->reject(fn ($macro) => Builder::hasMacro($macro))
->each(fn ($macro) => Builder::macro($macro, fn (...$args) => SchemaMacros::registerMacro($macro, $this, ...$args)));
}

/**
* @param array<string, array<string, string>> $macros
*/
public static function registerMacrosUsing(array $macros): mixed
{
return app()->singleton(SchemaMacrosCollection::class, fn () => new SchemaMacrosCollection($macros));
}

/**
* @return array<string>
*/
public static function supportedDrivers(string $macro): array
{
return array_keys(static::macros()[$macro]);
}

protected static function validDriver(string $macro, string $driver): bool
{
return in_array($driver, static::supportedDrivers($macro));
}

protected static function ensureDriverIsSupported(string $macro, Builder $builder): bool
{
if (! static::validDriver($macro, $driver = $builder->getConnection()->getDriverName())) {
$supportedDrivers = implode(', ', static::supportedDrivers($macro));
throw new UnsupportedDriver("The {$macro}() macro does not support {$driver}. Supported drivers are: {$supportedDrivers}");
}

return true;
}

public static function registerMacro(string $macro, Builder $builder, ...$args): mixed
{
static::ensureDriverIsSupported($macro, $builder);

return Collection::make(static::supportedDrivers($macro))
->filter(fn ($supportedDriver) => $builder->getConnection()->getDriverName() === $supportedDriver)
->map(fn ($driver) => static::getMacroForDriver($driver, $macro, $builder, ...$args))
->first();
}

protected static function getMacroForDriver(string $driver, string $macro, Builder $builder, ...$args): mixed
{
static::registerMacroForDriver($driver, $macro);

return $builder->{$driver.ucfirst($macro)}(...$args);
}

protected static function registerMacroForDriver(string $driver, string $macro): void
{
Builder::macro($driver.ucfirst($macro), app(static::macros()[$macro][$driver])());
}
}
9 changes: 9 additions & 0 deletions src/SchemaMacrosCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Envor\SchemaMacros;

use Illuminate\Support\Collection;

class SchemaMacrosCollection extends Collection
{
}
115 changes: 21 additions & 94 deletions src/SchemaMacrosServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

namespace Envor\SchemaMacros;

use Illuminate\Database\Schema\Builder;
use Illuminate\Database\Schema\MySqlBuilder;
use Illuminate\Database\Schema\SQLiteBuilder;
use Illuminate\Support\Collection;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Stringable;

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

public function packageBooted(): void
public function packageRegistered(): void
{
Collection::make($this->sqliteBuilderMacros())
->reject(fn ($class, $macro) => Builder::hasMacro($macro))
->each(fn ($class, $macro) => Builder::macro($macro, app($class)()));
SchemaMacros::registerMacrosUsing($this->macros());

Collection::make($this->mysqlBuilderMacros())
->reject(fn ($class, $macro) => Builder::hasMacro($macro))
->each(fn ($class, $macro) => Builder::macro($macro, app($class)()));

Collection::make($this->builderMacros())
->reject(fn ($macro, $name) => Builder::hasMacro($name))
->each(fn ($macro, $name) => Builder::macro($name, $macro));
}

/**
* @return array<string, callable>
*/
private function builderMacros(): array
{
return [
'databaseExists' => function (string|Stringable $database) {
$database = (string) $database;

if ($this instanceof MySqlBuilder) {
return $this->mysqlDatabaseExists($database);
}

if ($this instanceof SQLiteBuilder) {
return $this->sqliteDatabaseExists($database);
}

throw new \Exception('The databaseExists() macro does not support'.get_class($this));
},
'createDatabaseIfNotExists' => function (string|Stringable $database, bool $recursive = true) {
$database = (string) $database;

if ($this instanceof MySqlBuilder) {
return $this->mysqlCreateDatabaseIfNotExists($database);
}

if ($this instanceof SQLiteBuilder) {
return $this->sqliteCreateDatabaseIfNotExists($database, $recursive);
}

throw new \Exception('The createDatabaseIfNotExists() macro does not support'.get_class($this));
},
'trashDatabase' => function (string|Stringable $database, string $trashDisk = 'local') {
$database = (string) $database;

if ($this instanceof MySqlBuilder) {
return $this->mysqlTrashDatabase($database);
}

if ($this instanceof SQLiteBuilder) {
return $this->sqliteTrashDatabase($database, $trashDisk);
}

throw new \Exception('The trashDatabase() macro does not support'.get_class($this));
},
'emptyTrash' => function (int $daysOld = 0, string $trashDisk = 'local') {
if ($this instanceof MySqlBuilder) {
return $this->mysqlEmptyTrash($daysOld);
}

if ($this instanceof SQLiteBuilder) {
return $this->sqliteEmptyTrash($daysOld, $trashDisk);
}

throw new \Exception('The emptyTrash() macro does not support'.get_class($this));
},
];
}

/**
* @return array<string, string>
*/
private function sqliteBuilderMacros(): array
{
return [
'sqliteDatabaseExists' => \Envor\SchemaMacros\SQLite\SQLiteDatabaseExists::class,
'sqliteCreateDatabaseIfNotExists' => \Envor\SchemaMacros\SQLite\SQLiteCreateDatabaseIfNotExists::class,
'sqliteTrashDatabase' => \Envor\SchemaMacros\SQLite\SQLiteTrashDatabase::class,
'sqliteEmptyTrash' => \Envor\SchemaMacros\SQLite\SQLiteEmptyTrash::class,
];
SchemaMacros::registerMacros();
}

/**
* @return array<string, string>
* @return array<string, array<string, string>>
*/
private function mysqlBuilderMacros(): array
private function macros(): array
{
return [
'mysqlDatabaseExists' => \Envor\SchemaMacros\MySql\MySqlDatabaseExists::class,
'mysqlCreateDatabaseIfNotExists' => \Envor\SchemaMacros\MySql\MySqlCreateDatabaseIfNotExists::class,
'mysqlTrashDatabase' => \Envor\SchemaMacros\MySql\MySqlTrashDatabase::class,
'mysqlEmptyTrash' => \Envor\SchemaMacros\MySql\MySqlEmptyTrash::class,
'databaseExists' => [
'sqlite' => \Envor\SchemaMacros\SQLite\SQLiteDatabaseExists::class,
'mysql' => \Envor\SchemaMacros\MySql\MySqlDatabaseExists::class,
],
'createDatabaseIfNotExists' => [
'sqlite' => \Envor\SchemaMacros\SQLite\SQLiteCreateDatabaseIfNotExists::class,
'mysql' => \Envor\SchemaMacros\MySql\MySqlCreateDatabaseIfNotExists::class,
],
'trashDatabase' => [
'sqlite' => \Envor\SchemaMacros\SQLite\SQLiteTrashDatabase::class,
'mysql' => \Envor\SchemaMacros\MySql\MySqlTrashDatabase::class,
],
'emptyTrash' => [
'sqlite' => \Envor\SchemaMacros\SQLite\SQLiteEmptyTrash::class,
'mysql' => \Envor\SchemaMacros\MySql\MySqlEmptyTrash::class,
],
];
}
}
9 changes: 9 additions & 0 deletions src/UnsupportedDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Envor\SchemaMacros;

use Exception;

class UnsupportedDriver extends Exception
{
}

0 comments on commit b456a43

Please sign in to comment.