|
4 | 4 |
|
5 | 5 | use Spatie\LaravelPackageTools\Package;
|
6 | 6 | use Spatie\LaravelPackageTools\PackageServiceProvider;
|
7 |
| -use Yusufalper\LaravelSubfolderMigrations\Commands\LaravelSubfolderMigrationsCommand; |
8 | 7 |
|
9 | 8 | class LaravelSubfolderMigrationsServiceProvider extends PackageServiceProvider
|
10 | 9 | {
|
11 | 10 | public function configurePackage(Package $package): void
|
12 | 11 | {
|
13 |
| - /* |
14 |
| - * This class is a Package Service Provider |
15 |
| - * |
16 |
| - * More info: https://github.com/spatie/laravel-package-tools |
17 |
| - */ |
18 |
| - $package |
19 |
| - ->name('laravel-subfolder-migrations') |
20 |
| - ->hasConfigFile() |
21 |
| - ->hasViews() |
22 |
| - ->hasMigration('create_laravel-subfolder-migrations_table') |
23 |
| - ->hasCommand(LaravelSubfolderMigrationsCommand::class); |
| 12 | + $package->name('laravel-subfolder-migrations'); |
| 13 | + |
| 14 | + $mainMigrationPath = database_path('migrations'); |
| 15 | + $directories = glob($mainMigrationPath . '/*' , GLOB_ONLYDIR); |
| 16 | + $allSubDirs = self::recursiveSearch($directories); |
| 17 | + |
| 18 | + $allMigrationPaths = array_merge([$mainMigrationPath], $allSubDirs); |
| 19 | + |
| 20 | + $this->loadMigrationsFrom($allMigrationPaths); |
| 21 | + } |
| 22 | + |
| 23 | + protected static function recursiveSearch(array $directories): array |
| 24 | + { |
| 25 | + $subs = []; |
| 26 | + $deepSubs = []; |
| 27 | + if (count($directories) > 0){ |
| 28 | + $subs = self::getSubDirectories($directories); |
| 29 | + } |
| 30 | + if (count($subs) > 0){ |
| 31 | + $deepSubs = self::recursiveSearch($subs); |
| 32 | + } |
| 33 | + return array_merge($directories, $subs, $deepSubs); |
| 34 | + } |
| 35 | + |
| 36 | + protected static function getSubDirectories(array $directories): array |
| 37 | + { |
| 38 | + $subDirectories = []; |
| 39 | + foreach ($directories as $directory){ |
| 40 | + $subDirectories[] = glob($directory . '/*' , GLOB_ONLYDIR); |
| 41 | + } |
| 42 | + return array_merge(...$subDirectories); |
24 | 43 | }
|
25 | 44 | }
|
0 commit comments