models(); if ($models->isEmpty()) { $this->info('No prunable models found.'); return; } $events->listen(ModelsPruned::class, function ($event) { $this->info("{$event->count} [{$event->model}] records have been pruned."); }); $models->each(function ($model) { $instance = new $model; $chunkSize = property_exists($instance, 'prunableChunkSize') ? $instance->prunableChunkSize : $this->option('chunk'); $total = $this->isPrunable($model) ? $instance->pruneAll($chunkSize) : 0; if ($total == 0) { $this->info("No prunable [$model] records found."); } }); $events->forget(ModelsPruned::class); } /** * Determine the models that should be pruned. * * @return \Illuminate\Support\Collection */ protected function models() { if (! empty($models = $this->option('model'))) { return collect($models); } return collect((new Finder)->in(app_path('Models'))->files()) ->map(function ($model) { $namespace = $this->laravel->getNamespace(); return $namespace.str_replace( ['/', '.php'], ['\\', ''], Str::after($model->getRealPath(), realpath(app_path()).DIRECTORY_SEPARATOR) ); })->filter(function ($model) { return $this->isPrunable($model); })->values(); } /** * Determine if the given model class is prunable. * * @param string $model * @return bool */ protected function isPrunable($model) { $uses = class_uses_recursive($model); return in_array(Prunable::class, $uses) || in_array(MassPrunable::class, $uses); } }