-
-
Notifications
You must be signed in to change notification settings - Fork 2
✨ commands #10
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
Merged
Merged
✨ commands #10
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ce5189b
✨ commands
GautierDele 5c67be9
Apply fixes from StyleCI
StyleCIBot 6611afc
Update StubsTest.php
GautierDele 7203efa
Apply fixes from StyleCI
StyleCIBot 4c496f7
♻️ fixes
GautierDele 16b4483
Apply fixes from StyleCI
StyleCIBot 83f14ba
✅ small fixes
GautierDele 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 hidden or 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 hidden or 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,195 @@ | ||
| <?php | ||
|
|
||
| namespace Lomkit\Access\Console; | ||
|
|
||
| use Illuminate\Console\GeneratorCommand; | ||
| use Illuminate\Support\Collection; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Finder\Finder; | ||
|
|
||
| use function Laravel\Prompts\multiselect; | ||
|
|
||
| #[AsCommand(name: 'make:control')] | ||
| class ControlMakeCommand extends GeneratorCommand | ||
| { | ||
| /** | ||
| * The console command name. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $name = 'make:control'; | ||
|
|
||
| /** | ||
| * The console command description. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $description = 'Create a new control class'; | ||
|
|
||
| /** | ||
| * The type of class being generated. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $type = 'Control'; | ||
|
|
||
| /** | ||
| * Get the stub file for the generator. | ||
| * | ||
| * @return string | ||
| */ | ||
| protected function getStub() | ||
| { | ||
| return $this->resolveStubPath('/stubs/control.stub'); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the fully-qualified path to the stub. | ||
| * | ||
| * @param string $stub | ||
| * | ||
| * @return string | ||
| */ | ||
| protected function resolveStubPath($stub) | ||
| { | ||
| return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) | ||
| ? $customPath | ||
| : __DIR__.$stub; | ||
| } | ||
|
|
||
| /** | ||
| * Get the default namespace for the class. | ||
| * | ||
| * @param string $rootNamespace | ||
| * | ||
| * @return string | ||
| */ | ||
| protected function getDefaultNamespace($rootNamespace) | ||
| { | ||
| return $rootNamespace.'\Access\Controls'; | ||
| } | ||
|
|
||
| /** | ||
| * Build the class with the given name. | ||
| * | ||
| * Remove the base controller import if we are already in the base namespace. | ||
| * | ||
| * @param string $name | ||
| * | ||
| * @return string | ||
| */ | ||
| protected function buildClass($name) | ||
| { | ||
| $rootNamespace = $this->rootNamespace(); | ||
| $controlNamespace = $this->getNamespace($name); | ||
|
|
||
| $replace = []; | ||
|
|
||
| $baseControlExists = file_exists($this->getPath("{$rootNamespace}Access\Controls\Control")); | ||
|
|
||
| $replace = $this->buildPerimetersReplacements($replace, $this->option('perimeters')); | ||
|
|
||
| if ($baseControlExists) { | ||
| $replace["use {$controlNamespace}\Control;\n"] = ''; | ||
| } else { | ||
| $replace[' extends Control'] = ''; | ||
| $replace["use {$rootNamespace}Access\Controls\Control;\n"] = ''; | ||
| } | ||
|
|
||
| return str_replace( | ||
| array_keys($replace), | ||
| array_values($replace), | ||
| parent::buildClass($name) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Build the model replacement values. | ||
| * | ||
| * @param array $replace | ||
| * @param array $perimeters | ||
| * | ||
| * @return array | ||
| */ | ||
| protected function buildPerimetersReplacements(array $replace, array $perimeters) | ||
| { | ||
| $perimetersImplementation = ''; | ||
|
|
||
| foreach ($perimeters as $perimeter) { | ||
| $perimeterClass = $this->rootNamespace().'Access\\Perimeters\\'.$perimeter; | ||
|
|
||
| $perimetersImplementation .= <<<PERIMETER | ||
| \\n | ||
| $perimeterClass::new() | ||
| ->should(function (Model \$user, string \$method, Model \$model) { | ||
| return true; | ||
| }) | ||
| ->allowed(function (Model \$user) { | ||
| return true; | ||
| }) | ||
| ->query(function (Builder \$query, Model \$user) { | ||
| return \$query; | ||
| }),\\n | ||
| PERIMETER; | ||
| } | ||
|
|
||
| return array_merge($replace, [ | ||
| '{{ perimeters }}' => $perimetersImplementation, | ||
| '{{perimeters}}' => $perimetersImplementation, | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Get the console command options. | ||
| * | ||
| * @return array | ||
| */ | ||
| protected function getOptions() | ||
| { | ||
| return [ | ||
| ['perimeters', 'p', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The perimeters that the control relies on'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Interact further with the user if they were prompted for missing arguments. | ||
| * | ||
| * @param \Symfony\Component\Console\Input\InputInterface $input | ||
| * @param \Symfony\Component\Console\Output\OutputInterface $output | ||
| * | ||
| * @return void | ||
| */ | ||
| protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) | ||
| { | ||
| if ($this->didReceiveOptions($input)) { | ||
| return; | ||
| } | ||
| $perimeters = multiselect( | ||
| 'What perimeters should this control apply to? (Optional)', | ||
| $this->possiblePerimeters(), | ||
| ); | ||
|
|
||
| if ($perimeters) { | ||
| $input->setOption('perimeters', $perimeters); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get a list of possible model names. | ||
| * | ||
| * @return array<int, string> | ||
| */ | ||
| protected function possiblePerimeters() | ||
| { | ||
| $perimetersPath = is_dir(app_path('Access/Perimeters')) ? app_path('Access/Perimeters') : app_path(); | ||
|
|
||
| return (new Collection(Finder::create()->files()->depth(0)->in($perimetersPath))) | ||
| ->map(fn ($file) => $file->getBasename('.php')) | ||
| ->sort() | ||
| ->values() | ||
| ->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.
🛠️ Refactor suggestion
Limited approach to finding perimeters.
The
possiblePerimetersmethod only searches for perimeters in a fixed location, which may not work for all project structures.Consider a more flexible approach:
protected function possiblePerimeters() { - $perimetersPath = is_dir(app_path('Access/Perimeters')) ? app_path('Access/Perimeters') : app_path(); + // Try multiple potential locations + $rootNamespace = $this->rootNamespace(); + $possiblePaths = [ + app_path('Access/Perimeters'), + app_path('Access'), + base_path('src/Access/Perimeters'), + base_path('src/Access'), + ]; + + $perimetersPath = app_path(); + foreach ($possiblePaths as $path) { + if (is_dir($path)) { + $perimetersPath = $path; + break; + } + } return (new Collection(Finder::create()->files()->depth(0)->in($perimetersPath))) ->map(fn ($file) => $file->getBasename('.php')) + ->filter(function ($filename) use ($perimetersPath) { + // Only include files that are likely to be perimeter classes + $content = file_get_contents($perimetersPath.'/'.$filename.'.php'); + return strpos($content, 'Perimeter') !== false; + }) ->sort() ->values() ->all(); }📝 Committable suggestion