diff --git a/src/CheckpointServiceProvider.php b/src/CheckpointServiceProvider.php index c6fb224..d32f9f3 100755 --- a/src/CheckpointServiceProvider.php +++ b/src/CheckpointServiceProvider.php @@ -13,56 +13,36 @@ class CheckpointServiceProvider extends ServiceProvider */ public function boot() { - /* - * Optional methods to load your package assets - */ - // $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'checkpoint'); - // $this->loadViewsFrom(__DIR__.'/../resources/views', 'checkpoint'); - //$this->loadMigrationsFrom(__DIR__.'/../database/migrations'); - // $this->loadRoutesFrom(__DIR__.'/routes.php'); - if ($this->app->runningInConsole()) { - // Publish configuration files $this->publishes([ __DIR__.'/../config/checkpoint.php' => config_path('checkpoint.php'), ], 'config'); + // dynamically publish all checkpoint migrations + foreach (File::glob(__DIR__ . '/../database/migrations/*') as $migration) { + $basename = strstr($migration, 'create'); + if (empty(File::glob(database_path('migrations/*' . $basename)))) { + config()->set('checkpoint.runs_migrations', true); + $this->publishes([ + $migration => database_path("migrations/". date('Y_m_d_His') . "_" . $basename) + ], 'migrations'); + } + } + + // Load default migrations if the runs_migrations toggle is true in the config (or if any of the expected files is missing) + if (config('checkpoint.runs_migrations', false)) { + $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); + } + // Publish extendable models $this->publishes([ __DIR__.'/../src/Models/Checkpoint.php' => base_path('app/Checkpoint.php'), __DIR__.'/../src/Models/Revision.php' => base_path('app/Revision.php'), ], 'models'); - // Publishing the views. - /*$this->publishes([ - __DIR__.'/../resources/views' => resource_path('views/vendor/checkpoint'), - ], 'views');*/ - - // Publishing assets. - /*$this->publishes([ - __DIR__.'/../resources/assets' => public_path('vendor/checkpoint'), - ], 'assets');*/ - - // Publishing the translation files. - /*$this->publishes([ - __DIR__.'/../resources/lang' => resource_path('lang/vendor/checkpoint'), - ], 'lang');*/ - // Registering package commands. - $this->commands([ - StartRevisioning::class, - ]); - } - - foreach (File::glob(__DIR__ . '/../database/migrations/*') as $migration) { - $basename = strstr($migration, 'create'); - if (empty(File::glob(database_path('migrations/*' . $basename)))) { - $timestamp = date('Y_m_d_His'); - $publish = database_path("migrations/{$timestamp}_{$basename}"); - - $this->publishes([$migration => $publish], 'migrations'); - } + $this->commands(StartRevisioning::class); } } diff --git a/src/Commands/StartRevisioning.php b/src/Commands/StartRevisioning.php index fa1b69a..754b45f 100644 --- a/src/Commands/StartRevisioning.php +++ b/src/Commands/StartRevisioning.php @@ -4,6 +4,8 @@ namespace Plank\Checkpoint\Commands; use Illuminate\Console\Command; +use Illuminate\Container\Container; +use Illuminate\Support\Facades\File; class StartRevisioning extends Command { @@ -13,7 +15,7 @@ class StartRevisioning extends Command * @var string */ protected $signature = 'checkpoint:start - {class? : a specified class to start revisioning on} + {class? : specify one or more classes to start revisions on} {--on= : The checkpoint ID that all revisions should be attached to} {--C|with-checkpoint : also create a starting checkpoint to attach all revisions to}'; @@ -37,7 +39,7 @@ public function __construct() /** * Execute the console command. * - * @return int + * @return mixed */ public function handle() { @@ -50,6 +52,19 @@ public function handle() } if ($class = $this->argument('class')) { + $models = explode(',', str_replace(' ', '', $class)); + } else { + // TODO: maybe pull base paths from composer psr-4, to support modular laravel codebases?? + $models = collect(File::allFiles(app_path()))->map(function ($item) { + $path = $item->getRelativePathName(); + return sprintf('\%s%s', Container::getInstance()->getNamespace(), + str_replace('/', '\\', substr($path, 0, strrpos($path, '.')))); + })->filter(function ($model) { + return method_exists($model, 'bootHasRevisions'); + }); + } + + foreach ($models as $class) { $records = $class::withoutGlobalScopes()->chunk(100, function ($results) use ($checkpoint, &$timeDelta) { foreach ($results as $item) { $item->updateOrCreateRevision(); @@ -60,11 +75,6 @@ public function handle() $revision->save(); } }); - } else { - // TODO: make this discover revisionable models, and boot them one by one. - $this->error('Please pass in the FQCN of a revisionable model.'); - return 1; } - return 0; } }