-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathLaravelModelUuidServiceProvider.php
42 lines (37 loc) · 1.42 KB
/
LaravelModelUuidServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace Dyrynda\Database\Support;
use Dyrynda\Database\Support\Exceptions\UnknownGrammarClass;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\ColumnDefinition;
use Illuminate\Database\Schema\Grammars\Grammar;
use Illuminate\Support\Fluent;
use Spatie\LaravelPackageTools\Commands\InstallCommand;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
class LaravelModelUuidServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
$package
->name('model-uuid')
->hasConfigFile()
->hasInstallCommand(function (InstallCommand $command) {
$command->publishConfigFile();
});
}
public function packageRegistered()
{
Grammar::macro('typeEfficientUuid', function (Fluent $column) {
return match (class_basename(static::class)) {
'MySqlGrammar' => sprintf('binary(%d)', $column->length ?? 16),
'PostgresGrammar' => 'bytea',
'SQLiteGrammar' => 'blob(256)',
default => throw new UnknownGrammarClass
};
});
Blueprint::macro('efficientUuid', function ($column): ColumnDefinition {
/** @var \Illuminate\Database\Schema\Blueprint $this */
return $this->addColumn('efficientUuid', $column);
});
}
}