Commit 02e071c5 authored by Thibaud Dauce's avatar Thibaud Dauce

Initial commit

parents
vendor/
.idea/
\ No newline at end of file
# Laravel Recursive Migration
This package allows to put Laravel migrations into subdirectories.
Check [my blog post](https://thibaud.dauce.fr/posts/2016-12-15-laravel-recursive-migrations.html)!
## Installation
Download the package with Composer:
```bash
composer require thibaud-dauce/laravel-recursive-migration
```
Then add the Service Provider in you `config/app.php`:
```
ThibaudDauce\LaravelRecursiveMigrations\LaravelRecursiveMigrationsServiceProvider::class,
```
## Usage
All migrations' commands are available. Just add the `--recursive` or the `-r` flag.
```bash
php artisan migrate --recursive
php artisan migrate:rollback --recursive
php artisan migrate:refresh --recursive
php artisan migrate:reset --recursive
php artisan migrate:status --recursive
```
{
"name": "thibaud-dauce/laravel-recursive-migrations",
"description": "This package allows to put Laravel migrations into subdirectories",
"type": "library",
"require": {
"php": ">=7.0.0",
"illuminate/support": "^5.3",
"illuminate/console": "^5.3",
"symfony/finder": "^3.1",
"symfony/console": "^3.1",
"illuminate/database": "^5.3"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
},
"autoload": {
"psr-4": {
"ThibaudDauce\\LaravelRecursiveMigrations\\": "src/"
}
},
"authors": [
{
"name": "Thibaud Dauce",
"email": "thibaud@dauce.fr"
}
]
}
This diff is collapsed.
<?php declare(strict_types = 1);
namespace ThibaudDauce\LaravelRecursiveMigrations\Commands;
use ThibaudDauce\LaravelRecursiveMigrations\RecursiveMigrationCommand;
use Illuminate\Database\Console\Migrations\MigrateCommand as BaseMigrateCommand;
class MigrateCommand extends BaseMigrateCommand
{
use RecursiveMigrationCommand;
}
<?php declare(strict_types = 1);
namespace ThibaudDauce\LaravelRecursiveMigrations\Commands;
use ThibaudDauce\LaravelRecursiveMigrations\RecursiveMigrationCommand;
use Illuminate\Database\Console\Migrations\RefreshCommand as BaseRefreshCommand;
class RefreshCommand extends BaseRefreshCommand
{
use RecursiveMigrationCommand;
}
\ No newline at end of file
<?php declare(strict_types = 1);
namespace ThibaudDauce\LaravelRecursiveMigrations\Commands;
use ThibaudDauce\LaravelRecursiveMigrations\RecursiveMigrationCommand;
use Illuminate\Database\Console\Migrations\ResetCommand as BaseResetCommand;
class ResetCommand extends BaseResetCommand
{
use RecursiveMigrationCommand;
}
\ No newline at end of file
<?php
namespace ThibaudDauce\LaravelRecursiveMigrations\Commands;
use ThibaudDauce\LaravelRecursiveMigrations\RecursiveMigrationCommand;
use Illuminate\Database\Console\Migrations\RollbackCommand as BaseRollbackCommand;
class RollbackCommand extends BaseRollbackCommand
{
use RecursiveMigrationCommand;
}
<?php declare(strict_types = 1);
namespace ThibaudDauce\LaravelRecursiveMigrations\Commands;
use ThibaudDauce\LaravelRecursiveMigrations\RecursiveMigrationCommand;
use Illuminate\Database\Console\Migrations\StatusCommand as BaseStatusCommand;
class StatusCommand extends BaseStatusCommand
{
use RecursiveMigrationCommand;
}
\ No newline at end of file
<?php declare(strict_types = 1);
namespace ThibaudDauce\LaravelRecursiveMigrations;
use ThibaudDauce\LaravelRecursiveMigrations\Commands\MigrateCommand;
use ThibaudDauce\LaravelRecursiveMigrations\Commands\RollbackCommand;
use Illuminate\Support\ServiceProvider;
use ThibaudDauce\LaravelRecursiveMigrations\Commands\RefreshCommand;
use ThibaudDauce\LaravelRecursiveMigrations\Commands\ResetCommand;
use ThibaudDauce\LaravelRecursiveMigrations\Commands\StatusCommand;
class LaravelRecursiveMigrationsServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->extend('command.migrate', function () {
return new MigrateCommand($this->app['migrator']);
});
$this->app->extend('command.migrate.rollback', function () {
return new RollbackCommand($this->app['migrator']);
});
$this->app->extend('command.migrate.refresh', function () {
return new RefreshCommand($this->app['migrator']);
});
$this->app->extend('command.migrate.reset', function () {
return new ResetCommand($this->app['migrator']);
});
$this->app->extend('command.migrate.status', function () {
return new StatusCommand($this->app['migrator']);
});
}
}
\ No newline at end of file
<?php declare(strict_types = 1);
namespace ThibaudDauce\LaravelRecursiveMigrations;
use Symfony\Component\Console\Input\InputOption;
/**
* @see \Illuminate\Database\Console\Migrations\BaseCommand
*/
trait RecursiveMigrationCommand
{
use Subdirectories;
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array_merge(parent::getOptions(), [
['recursive', 'r', InputOption::VALUE_NONE, 'Indicates if the migration folders should be flattened.']
]);
}
/**
* Get all of the migration paths.
*
* @return array
*/
protected function getMigrationPaths()
{
$paths = parent::getMigrationPaths();
return $this->option('recursive') ? $this->allSubdirectories($paths) : $paths;
}
/**
* Call another console command.
*
* @param string $command
* @param array $arguments
* @return int
*/
public function call($command, array $arguments = [])
{
if (starts_with($command, 'migrate')) {
$arguments['--recursive'] = $this->option('recursive');
}
parent::call($command, $arguments);
}
}
\ No newline at end of file
<?php declare(strict_types = 1);
namespace ThibaudDauce\LaravelRecursiveMigrations;
use Illuminate\Support\Collection;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
trait Subdirectories
{
/**
* Fetch all subdirectories from a path.
* The original path is included in the array.
*
* @param string $path
* @return string[]
*/
protected function subdirectories($path)
{
return Collection::make(Finder::create()->in($path)->directories())
->map(function (SplFileInfo $directory) {
return $directory->getPathname();
})
->prepend($path)
->values()
->toArray();
}
/**
* Fetch all subdirectories from an array of base paths.
* The original paths are included in the array.
*
* @param string[] $paths
* @return string[]
*/
protected function allSubdirectories($paths)
{
return Collection::make($paths)
->flatMap(function ($path) {
return $this->subdirectories($path);
})
->toArray();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment