|
5 | 5 | use Spatie\LaravelPackageTools\Package;
|
6 | 6 | use Spatie\LaravelPackageTools\PackageServiceProvider;
|
7 | 7 |
|
| 8 | +/*The provided service provider is a useful addition for the Laravel framework, |
| 9 | +specifically for handling migrations. By default, Laravel expects all migration |
| 10 | +files to be placed directly in the migrations directory without any subdirectories. |
| 11 | +However, this service provider allows you to create subdirectories within the |
| 12 | +migrations directory, providing a more organized structure for your migration files. |
| 13 | +With this service provider, you can organize your migration files into subdirectories |
| 14 | +based on your application's needs. The code recursively searches for subdirectories |
| 15 | +within the migrations directory and retrieves all migration files, regardless |
| 16 | +of their depth in the directory structure. |
| 17 | +The migration files within the subdirectories are ordered based on their timestamp, |
| 18 | +just like the migration files in the main migrations directory. |
| 19 | +This ensures that the migrations are executed in the correct order |
| 20 | +when running the migration command. |
| 21 | +Once the service provider is registered in your Laravel application, it automatically |
| 22 | +loads the migration files from the main migrations directory as well as any subdirectories. |
| 23 | +This enables you to take advantage of a more organized structure while still |
| 24 | +maintaining the proper execution order of your migrations. |
| 25 | +In summary, this service provider enhances the Laravel framework's migration functionality |
| 26 | +by allowing the creation of subdirectories within the migrations directory. |
| 27 | +It ensures that all migration files, regardless of their location within the subdirectories, |
| 28 | +are ordered based on their timestamp and properly executed when running the migration command. |
| 29 | +*/ |
8 | 30 | class LaravelSubfolderMigrationsServiceProvider extends PackageServiceProvider
|
9 | 31 | {
|
10 | 32 | public function configurePackage(Package $package): void
|
11 | 33 | {
|
12 | 34 | $package->name('laravel-subfolder-migrations');
|
13 | 35 |
|
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); |
| 36 | + $this->loadMigrationsFrom( |
| 37 | + $this->retrieveSubdirectories([database_path('migrations')]) |
| 38 | + ); |
21 | 39 | }
|
22 | 40 |
|
23 |
| - protected static function recursiveSearch(array $directories): array |
| 41 | + /** |
| 42 | + * Recursively returns subdirectories within the given directory path(s), |
| 43 | + * including child subdirectories. |
| 44 | + */ |
| 45 | + private function retrieveSubdirectories(array|string $paths): array |
24 | 46 | {
|
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); |
| 47 | + $paths = is_string($paths) ? [$paths] : $paths; |
| 48 | + |
| 49 | + $subdirs = []; |
| 50 | + |
| 51 | + foreach ($paths as $path) { |
| 52 | + $subdirs += glob($path . '/*', GLOB_ONLYDIR) ?? []; |
32 | 53 | }
|
33 |
| - return array_merge($directories, $subs, $deepSubs); |
34 |
| - } |
35 | 54 |
|
36 |
| - protected static function getSubDirectories(array $directories): array |
37 |
| - { |
38 |
| - $subDirectories = []; |
39 |
| - foreach ($directories as $directory){ |
40 |
| - $subDirectories[] = glob($directory . '/*' , GLOB_ONLYDIR); |
| 55 | + if (!empty($subdirs)) { |
| 56 | + $subdirs = $this->retrieveSubdirectories($subdirs); |
| 57 | + return array_merge($paths, $subdirs); |
41 | 58 | }
|
42 |
| - return array_merge(...$subDirectories); |
| 59 | + |
| 60 | + return $paths; |
43 | 61 | }
|
44 | 62 | }
|
0 commit comments