Skip to content

Commit 5166264

Browse files
committed
package fix
1 parent 9935f45 commit 5166264

10 files changed

+47
-221
lines changed

.github/ISSUE_TEMPLATE/config.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.

.github/dependabot.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

.github/workflows/dependabot-auto-merge.yml

Lines changed: 0 additions & 32 deletions
This file was deleted.

.github/workflows/phpstan.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

.github/workflows/run-tests.yml

Lines changed: 0 additions & 47 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Organize your migrations into subfolders.
22

3+
IF YOU DO NOT WANT TO USE PACKAGE FOR THIS FEATURE, YOU CAN USE SIMPLE PROVIDER GIST FROM [HERE](https://gist.github.com/yusufalper/25c0c7e5a347abc09349bbc16817c268)
4+
35
This package allows you to split database/migrations into subfolders. Especially in large projects, migrations folder may cause headaches.
46

57
Now you can turn this:

composer.json

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,12 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^8.1",
19+
"php": "^8.1|^9|^10",
2020
"spatie/laravel-package-tools": "^1.9.2",
21-
"illuminate/contracts": "^9.0"
21+
"illuminate/contracts": "^9.0|^10"
2222
},
2323
"require-dev": {
24-
"laravel/pint": "^1.0",
25-
"nunomaduro/collision": "^6.0",
26-
"nunomaduro/larastan": "^2.0.1",
27-
"orchestra/testbench": "^7.0",
28-
"pestphp/pest": "^1.21",
29-
"pestphp/pest-plugin-laravel": "^1.1",
30-
"phpstan/extension-installer": "^1.1",
31-
"phpstan/phpstan-deprecation-rules": "^1.0",
32-
"phpstan/phpstan-phpunit": "^1.0",
33-
"phpunit/phpunit": "^9.5"
24+
"nunomaduro/collision": "^6.0"
3425
},
3526
"autoload": {
3627
"psr-4": {
@@ -43,18 +34,8 @@
4334
"Yusufalper\\LaravelSubfolderMigrations\\Tests\\": "tests"
4435
}
4536
},
46-
"scripts": {
47-
"analyse": "vendor/bin/phpstan analyse",
48-
"test": "vendor/bin/pest",
49-
"test-coverage": "vendor/bin/pest --coverage",
50-
"format": "vendor/bin/pint"
51-
},
5237
"config": {
53-
"sort-packages": true,
54-
"allow-plugins": {
55-
"pestphp/pest-plugin": true,
56-
"phpstan/extension-installer": true
57-
}
38+
"sort-packages": true
5839
},
5940
"extra": {
6041
"laravel": {

phpunit.xml.dist

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/LaravelSubfolderMigrationsServiceProvider.php

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,58 @@
55
use Spatie\LaravelPackageTools\Package;
66
use Spatie\LaravelPackageTools\PackageServiceProvider;
77

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+
*/
830
class LaravelSubfolderMigrationsServiceProvider extends PackageServiceProvider
931
{
1032
public function configurePackage(Package $package): void
1133
{
1234
$package->name('laravel-subfolder-migrations');
1335

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+
);
2139
}
2240

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
2446
{
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) ?? [];
3253
}
33-
return array_merge($directories, $subs, $deepSubs);
34-
}
3554

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);
4158
}
42-
return array_merge(...$subDirectories);
59+
60+
return $paths;
4361
}
4462
}

tests/ExampleTest.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)