Skip to content

Commit eb424de

Browse files
authored
Make the model:prune command easier to extend (#43919)
* 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
1 parent a3c3ed5 commit eb424de

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/Illuminate/Database/Console/PruneCommand.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,33 @@ public function handle(Dispatcher $events)
7171
});
7272

7373
$models->each(function ($model) {
74-
$instance = new $model;
74+
$this->pruneModel($model);
75+
});
76+
77+
$events->forget(ModelsPruned::class);
78+
}
7579

76-
$chunkSize = property_exists($instance, 'prunableChunkSize')
77-
? $instance->prunableChunkSize
78-
: $this->option('chunk');
80+
/**
81+
* Prune the given model.
82+
*
83+
* @param string $model
84+
* @return void
85+
*/
86+
protected function pruneModel(string $model)
87+
{
88+
$instance = new $model;
7989

80-
$total = $this->isPrunable($model)
81-
? $instance->pruneAll($chunkSize)
82-
: 0;
90+
$chunkSize = property_exists($instance, 'prunableChunkSize')
91+
? $instance->prunableChunkSize
92+
: $this->option('chunk');
8393

84-
if ($total == 0) {
85-
$this->components->info("No prunable [$model] records found.");
86-
}
87-
});
94+
$total = $this->isPrunable($model)
95+
? $instance->pruneAll($chunkSize)
96+
: 0;
8897

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

92103
/**
@@ -131,7 +142,7 @@ protected function models()
131142
/**
132143
* Get the default path where models are located.
133144
*
134-
* @return string
145+
* @return string|string[]
135146
*/
136147
protected function getDefaultPath()
137148
{

0 commit comments

Comments
 (0)