-
-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add model:prune console command #1263
Open
mjauvin
wants to merge
6
commits into
develop
Choose a base branch
from
model-prune
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2437239
helper class to find models
mjauvin 51b197d
override Laravel PruneCommand
mjauvin 0676dcb
enable model:prune console command
mjauvin 05d5f6f
event example must return array, not collection
mjauvin b925736
combine both filters
mjauvin 7ee9cd1
apply review suggestion
mjauvin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace System\Console; | ||
|
||
use Exception; | ||
use Illuminate\Database\Console\PruneCommand as BasePruneCommand; | ||
use Illuminate\Database\Eloquent\MassPrunable; | ||
use Illuminate\Database\Eloquent\Prunable; | ||
use Illuminate\Support\Collection; | ||
use Winter\Storm\Support\Facades\Event; | ||
use System\Helpers\ModelFinder; | ||
|
||
class PruneCommand extends BasePruneCommand | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function models(): Collection | ||
{ | ||
if (! empty($models = $this->option('model'))) { | ||
return collect($models)->filter(function ($model) { | ||
return class_exists($model); | ||
})->values(); | ||
} | ||
|
||
$except = $this->option('except'); | ||
|
||
return collect($this->findModels()) | ||
->when(! empty($except), function ($models) use ($except) { | ||
return $models->reject(function ($model) use ($except) { | ||
return in_array($model, $except); | ||
}); | ||
})->filter(function ($model) { | ||
return class_exists($model) && $this->isPrunable($model); | ||
})->values(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function isPrunable($model): bool | ||
{ | ||
try { | ||
$uses = class_uses_recursive($model); | ||
} catch (Exception $e) { | ||
return false; | ||
} | ||
|
||
return in_array(Prunable::class, $uses) || in_array(MassPrunable::class, $uses); | ||
} | ||
|
||
/** | ||
* Find all models. | ||
*/ | ||
protected function findModels(): array | ||
{ | ||
/** | ||
* @event system.console.model.prune.findModels | ||
* Give the opportunity to return an array of Models to prune. | ||
* | ||
* Example usage: | ||
* | ||
* Event::listen('system.console.model.prune.findModels', function () { | ||
* return ['example model' => '\System\Models\File']; | ||
* }); | ||
* | ||
*/ | ||
$models = Event::fire('system.console.model.prune.findModels', [$this], true); | ||
if (is_array($models)) { | ||
return $models; | ||
} | ||
|
||
return ModelFinder::findModels(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace System\Helpers; | ||
|
||
use Winter\Storm\Support\Facades\File; | ||
use Illuminate\Support\Str; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
class ModelFinder | ||
{ | ||
/** | ||
* Find all models in core and active plugins. | ||
* | ||
* @return Collection | ||
*/ | ||
public static function findModels(): array | ||
{ | ||
$models = []; | ||
$models[] = static::findModuleModels(); | ||
$models[] = static::findActivePluginsModels(); | ||
|
||
return collect($models)->flatten()->all(); | ||
} | ||
|
||
public static function findModuleModels(): array | ||
{ | ||
$modulesPath = base_path() . '/modules'; | ||
|
||
$models = collect(Finder::create()->in($modulesPath)->notPath('/tests/')->files()->name('/^[A-Z]{1}.+\.php$/')) | ||
->map(function ($model) use ($modulesPath) { | ||
$modelPath = str_replace(['/', '.php'], ['\\', ''], Str::after($model->getRealPath(), realpath($modulesPath).DIRECTORY_SEPARATOR)); | ||
return ucwords($modelPath, '\\'); | ||
}); | ||
|
||
return $models->values()->all(); | ||
} | ||
|
||
public static function findActivePluginsModels(): array | ||
{ | ||
$models = []; | ||
$pm = \System\Classes\PluginManager::instance(); | ||
|
||
$pluginsPaths = collect($pm->getPlugins())->map(function ($plugin) use ($pm) { | ||
return $pm->getPluginPath($plugin); | ||
})->filter(function ($path) { | ||
return File::exists($path . '/models'); | ||
})->each(function ($path) use (&$models) { | ||
$modelPaths = Finder::create()->in($path . '/models')->files()->name('/^[A-Z]{1}.+\.php$/'); | ||
$models[] = collect($modelPaths)->map(function ($model) { | ||
$modelPath = str_replace(['/', '.php'], ['\\', ''], Str::after($model->getRealPath(), plugins_path().DIRECTORY_SEPARATOR)); | ||
return ucwords($modelPath, '\\'); | ||
})->all(); | ||
}); | ||
|
||
return collect($models)->flatten()->all(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we doing any sort of checking to make sure the classes are actual model classes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really, no.