Skip to content

Commit

Permalink
Make the model:prune command easier to extend (#43919)
Browse files Browse the repository at this point in the history
* Split up model pruning to a separate method

This improves the experience when extending/overwriting the prune command.

* Make typehinting less strict on the prune command

The `getDefaultPath()` method can also give back a list of paths for it to still work. This commit will reflect that in the PHPDocs of the method.

* Fix codestyle
  • Loading branch information
markwalet authored Aug 30, 2022
1 parent a3c3ed5 commit eb424de
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/Illuminate/Database/Console/PruneCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,33 @@ public function handle(Dispatcher $events)
});

$models->each(function ($model) {
$instance = new $model;
$this->pruneModel($model);
});

$events->forget(ModelsPruned::class);
}

$chunkSize = property_exists($instance, 'prunableChunkSize')
? $instance->prunableChunkSize
: $this->option('chunk');
/**
* Prune the given model.
*
* @param string $model
* @return void
*/
protected function pruneModel(string $model)
{
$instance = new $model;

$total = $this->isPrunable($model)
? $instance->pruneAll($chunkSize)
: 0;
$chunkSize = property_exists($instance, 'prunableChunkSize')
? $instance->prunableChunkSize
: $this->option('chunk');

if ($total == 0) {
$this->components->info("No prunable [$model] records found.");
}
});
$total = $this->isPrunable($model)
? $instance->pruneAll($chunkSize)
: 0;

$events->forget(ModelsPruned::class);
if ($total == 0) {
$this->components->info("No prunable [$model] records found.");
}
}

/**
Expand Down Expand Up @@ -131,7 +142,7 @@ protected function models()
/**
* Get the default path where models are located.
*
* @return string
* @return string|string[]
*/
protected function getDefaultPath()
{
Expand Down

0 comments on commit eb424de

Please sign in to comment.