|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Database\Console; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Contracts\Events\Dispatcher; |
| 7 | +use Illuminate\Database\Eloquent\MassPrunable; |
| 8 | +use Illuminate\Database\Eloquent\Prunable; |
| 9 | +use Illuminate\Database\Events\ModelsPruned; |
| 10 | +use Illuminate\Support\Str; |
| 11 | +use Symfony\Component\Finder\Finder; |
| 12 | + |
| 13 | +class PruneCommand extends Command |
| 14 | +{ |
| 15 | + /** |
| 16 | + * The console command name. |
| 17 | + * |
| 18 | + * @var string |
| 19 | + */ |
| 20 | + protected $signature = 'model:prune |
| 21 | + {--model=* : Class names of the models to be pruned} |
| 22 | + {--chunk=1000 : The number of models to retrieve per chunk of models to be deleted}'; |
| 23 | + |
| 24 | + /** |
| 25 | + * The console command description. |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + protected $description = 'Prune models that are no longer needed'; |
| 30 | + |
| 31 | + /** |
| 32 | + * Execute the console command. |
| 33 | + * |
| 34 | + * @param \Illuminate\Contracts\Events\Dispatcher $events |
| 35 | + * @return void |
| 36 | + */ |
| 37 | + public function handle(Dispatcher $events) |
| 38 | + { |
| 39 | + $events->listen(ModelsPruned::class, function ($event) { |
| 40 | + $this->info("{$event->count} [{$event->model}] records have been pruned."); |
| 41 | + }); |
| 42 | + |
| 43 | + $this->models()->each(function ($model) { |
| 44 | + $instance = new $model; |
| 45 | + |
| 46 | + $chunkSize = property_exists($instance, 'prunableChunkSize') |
| 47 | + ? $instance->prunableChunkSize |
| 48 | + : $this->option('chunk'); |
| 49 | + |
| 50 | + $total = $this->isPrunable($model) |
| 51 | + ? $instance->pruneAll($chunkSize) |
| 52 | + : 0; |
| 53 | + |
| 54 | + if ($total == 0) { |
| 55 | + $this->info("No prunable [$model] records found."); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + $events->forget(ModelsPruned::class); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Determine the models that should be pruned. |
| 64 | + * |
| 65 | + * @return array |
| 66 | + */ |
| 67 | + protected function models() |
| 68 | + { |
| 69 | + if (! empty($models = $this->option('model'))) { |
| 70 | + return collect($models); |
| 71 | + } |
| 72 | + |
| 73 | + return collect((new Finder)->in(app_path('Models'))->files()) |
| 74 | + ->map(function ($model) { |
| 75 | + $namespace = $this->laravel->getNamespace(); |
| 76 | + |
| 77 | + return $namespace.str_replace( |
| 78 | + ['/', '.php'], |
| 79 | + ['\\', ''], |
| 80 | + Str::after($model->getRealPath(), realpath(app_path()).DIRECTORY_SEPARATOR) |
| 81 | + ); |
| 82 | + })->filter(function ($model) { |
| 83 | + return $this->isPrunable($model); |
| 84 | + })->values(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Determine if the given model class is prunable. |
| 89 | + * |
| 90 | + * @param string $model |
| 91 | + * @return bool |
| 92 | + */ |
| 93 | + protected function isPrunable($model) |
| 94 | + { |
| 95 | + $uses = class_uses_recursive($model); |
| 96 | + |
| 97 | + return in_array(Prunable::class, $uses) || in_array(MassPrunable::class, $uses); |
| 98 | + } |
| 99 | +} |
0 commit comments