|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Netsells\LaravelMutexMigrations; |
| 6 | + |
| 7 | +use Illuminate\Contracts\Support\DeferrableProvider; |
| 8 | +use Illuminate\Database\Console\Migrations\MigrateCommand; |
| 9 | +use Illuminate\Database\Migrations\Migrator; |
| 10 | +use Illuminate\Support\Facades\Cache; |
| 11 | +use Illuminate\Support\Facades\Config; |
| 12 | +use Illuminate\Support\ServiceProvider as BaseServiceProvider; |
| 13 | +use Netsells\LaravelMutexMigrations\Commands; |
| 14 | +use Netsells\LaravelMutexMigrations\Mutex; |
| 15 | + |
| 16 | +class DependencyBindingProvider extends BaseServiceProvider implements DeferrableProvider |
| 17 | +{ |
| 18 | + /** |
| 19 | + * Get the services provided by the provider. |
| 20 | + * |
| 21 | + * @return list<class-string> |
| 22 | + */ |
| 23 | + public function provides(): array |
| 24 | + { |
| 25 | + return [ |
| 26 | + MigrateCommand::class, |
| 27 | + Commands\MutexMigrateCommand::class, |
| 28 | + Mutex\MutexRelay::class, |
| 29 | + ]; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Register any application services. |
| 34 | + */ |
| 35 | + public function register(): void |
| 36 | + { |
| 37 | + $this->app->bind(MigrateCommand::class, Commands\MigrateCommandExtension::class); |
| 38 | + |
| 39 | + $this->app->when([Commands\MigrateCommandExtension::class, Commands\MutexMigrateCommand::class]) |
| 40 | + ->needs(Migrator::class) |
| 41 | + ->give(function ($app) { |
| 42 | + return $app['migrator']; |
| 43 | + }); |
| 44 | + |
| 45 | + $this->app->bind(Mutex\MutexRelay::class, function ($app) { |
| 46 | + $store = Config::get('mutex-migrations.lock.store'); |
| 47 | + |
| 48 | + return new Mutex\MutexRelay( |
| 49 | + cache: Cache::store($store), |
| 50 | + lockDurationSeconds: Config::get('mutex-migrations.lock.ttl_seconds') |
| 51 | + ?? Mutex\MutexRelay::DEFAULT_TTL_SECONDS, |
| 52 | + lockTable: Config::get("cache.stores.{$store}.lock_table") |
| 53 | + ?? Mutex\MutexRelay::DEFAULT_LOCK_TABLE, |
| 54 | + ); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Bootstrap any package services. |
| 60 | + */ |
| 61 | + public function boot(): void |
| 62 | + { |
| 63 | + // |
| 64 | + } |
| 65 | +} |
0 commit comments