-
-
Notifications
You must be signed in to change notification settings - Fork 2
Feat/rewrote control registering #29
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1d5624d
♻️ rewrote control registering
GautierDele 34421d3
Apply fixes from StyleCI
StyleCIBot a2815d4
🐛 added class reference to controller make command
GautierDele 1a07202
✨ controls interactions
GautierDele 31a9952
Apply fixes from StyleCI
StyleCIBot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| <?php | ||
|
|
||
| namespace Lomkit\Access; | ||
|
|
||
| use Illuminate\Database\Eloquent\Model; | ||
| use Illuminate\Support\Collection; | ||
| use Illuminate\Support\Str; | ||
| use Lomkit\Access\Controls\Control; | ||
| use ReflectionClass; | ||
| use ReflectionException; | ||
| use SplFileInfo; | ||
| use Symfony\Component\Finder\Finder; | ||
|
|
||
| class Access | ||
| { | ||
| /** | ||
| * The default path where control reside. | ||
| * | ||
| * @var array | ||
| */ | ||
| public static array $controlDiscoveryPaths; | ||
|
|
||
| /** | ||
| * The registered controls. | ||
| * | ||
| * @var Control[] | ||
| */ | ||
| protected static array $controls; | ||
|
|
||
| /** | ||
| * Add multiple control to access. | ||
| * | ||
| * @param Control[] $controls | ||
| * | ||
| * @return Access | ||
| */ | ||
| public function addControls(array $controls): static | ||
| { | ||
| foreach ($controls as $control) { | ||
| $this->addControl($control); | ||
| } | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Add a control to access. | ||
| * | ||
| * @param Control $control | ||
| * | ||
| * @return Access | ||
| */ | ||
| public function addControl(Control $control): self | ||
| { | ||
| static::$controls[class_basename($control)] = $control; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Get the control instance for the given model. | ||
| * | ||
| * @param Model|class-string<Model> $model | ||
| * | ||
| * @return Control|null | ||
| */ | ||
| public static function controlForModel(Model|string $model): ?Control | ||
| { | ||
| if (!is_string($model)) { | ||
| $model = $model::class; | ||
| } | ||
|
|
||
| foreach (static::$controls as $control) { | ||
| if ($control->isModel($model)) { | ||
| return $control; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Discover controls for a given path. | ||
| * | ||
| * @var string[] | ||
| */ | ||
| public function discoverControls(array $paths): self | ||
| { | ||
| (new Collection($paths)) | ||
| ->flatMap(function ($directory) { | ||
| return glob($directory, GLOB_ONLYDIR); | ||
| }) | ||
| ->reject(function ($directory) { | ||
| return !is_dir($directory); | ||
| }) | ||
| ->each(function ($directory) { | ||
| $controls = Finder::create()->files()->in($directory); | ||
|
|
||
| foreach ($controls as $control) { | ||
| try { | ||
| $control = new ReflectionClass( | ||
| static::classFromFile($control, base_path()) | ||
| ); | ||
| } catch (ReflectionException) { | ||
| continue; | ||
| } | ||
|
|
||
| if (!$control->isInstantiable()) { | ||
| continue; | ||
| } | ||
|
|
||
| $this->addControl($control->newInstance()); | ||
| } | ||
| }); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Get the control directories that should be used to discover controls. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function discoverControlsWithin(): array | ||
| { | ||
| return static::$controlDiscoveryPaths ?? [ | ||
| app()->path('Access/Controls'), | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Extract the class name from the given file path. | ||
| * | ||
| * @param SplFileInfo $file | ||
| * @param string $basePath | ||
| * | ||
| * @return string | ||
| */ | ||
| protected function classFromFile(SplFileInfo $file, string $basePath) | ||
| { | ||
| $class = trim(Str::replaceFirst($basePath, '', $file->getRealPath()), DIRECTORY_SEPARATOR); | ||
|
|
||
| return ucfirst(Str::camel(str_replace( | ||
| [DIRECTORY_SEPARATOR, ucfirst(basename(app()->path())).'\\'], | ||
| ['\\', app()->getNamespace()], | ||
| ucfirst(Str::replaceLast('.php', '', $class)) | ||
| ))); | ||
GautierDele marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.