Skip to content

Commit

Permalink
command can discover revisionable classes automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
a-drew committed Nov 18, 2020
1 parent fb8eb3c commit 39dfbab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 44 deletions.
54 changes: 17 additions & 37 deletions src/CheckpointServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
24 changes: 17 additions & 7 deletions src/Commands/StartRevisioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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}';

Expand All @@ -37,7 +39,7 @@ public function __construct()
/**
* Execute the console command.
*
* @return int
* @return mixed
*/
public function handle()
{
Expand All @@ -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();
Expand All @@ -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;
}
}

0 comments on commit 39dfbab

Please sign in to comment.