chuckcms /
addresses
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Chuckcms\Addresses; |
||
| 4 | |||
| 5 | use Chuckcms\Addresses\Contracts\Address as AddressContract; |
||
| 6 | use Illuminate\Filesystem\Filesystem; |
||
| 7 | use Illuminate\Support\Collection; |
||
| 8 | use Illuminate\Support\ServiceProvider; |
||
| 9 | |||
| 10 | class AddressesServiceProvider extends ServiceProvider |
||
| 11 | { |
||
| 12 | public function boot() |
||
| 13 | { |
||
| 14 | $this->doPublishing(); |
||
| 15 | |||
| 16 | $this->registerModelBindings(); |
||
| 17 | } |
||
| 18 | |||
| 19 | public function register() |
||
| 20 | { |
||
| 21 | $this->mergeConfigFrom( |
||
| 22 | __DIR__.'/../config/addresses.php', |
||
| 23 | 'addresses' |
||
| 24 | ); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function doPublishing() |
||
| 28 | { |
||
| 29 | if (!function_exists('config_path')) { |
||
| 30 | // function not available and 'publish' not relevant in Lumen (credit: Spatie) |
||
| 31 | return; |
||
| 32 | } |
||
| 33 | |||
| 34 | $this->publishes([ |
||
| 35 | __DIR__.'/../config/addresses.php' => config_path('addresses.php'), |
||
| 36 | ], 'config'); |
||
| 37 | |||
| 38 | $this->publishes([ |
||
| 39 | __DIR__.'/../database/migrations/create_addresses_tables.php.stub' => $this->getMigrationFileName('create_addresses_tables.php'), |
||
| 40 | ], 'migrations'); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function registerModelBindings() |
||
| 44 | { |
||
| 45 | $config = $this->app->config['addresses.models']; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 46 | |||
| 47 | $this->app->bind(AddressContract::class, $config['address']); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Returns existing migration file if found, else uses the current timestamp. |
||
| 52 | * |
||
| 53 | * @return string |
||
| 54 | */ |
||
| 55 | public function getMigrationFileName($migrationFileName): string |
||
| 56 | { |
||
| 57 | $timestamp = date('Y_m_d_His'); |
||
| 58 | |||
| 59 | $filesystem = $this->app->make(Filesystem::class); |
||
| 60 | |||
| 61 | return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR) |
||
| 62 | ->flatMap(function ($path) use ($filesystem, $migrationFileName) { |
||
| 63 | return $filesystem->glob($path.'*_'.$migrationFileName); |
||
| 64 | }) |
||
| 65 | ->push($this->app->databasePath()."/migrations/{$timestamp}_{$migrationFileName}") |
||
| 66 | ->first(); |
||
| 67 | } |
||
| 68 | } |
||
| 69 |