Skip to content
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

Introduce new fresh look for Artisan command #233

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Commands/Concerns/CanManipulateFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ protected function checkForCollision(array $paths): bool
{
foreach ($paths as $path) {
if ($this->fileExists($path)) {
$this->error("$path already exists, aborting.");
$this->components->error("$path already exists, aborting.");

return true;
}
Expand Down
32 changes: 0 additions & 32 deletions src/Commands/Concerns/CanValidateInput.php

This file was deleted.

30 changes: 14 additions & 16 deletions src/Commands/MakeShieldDoctorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,28 @@ class MakeShieldDoctorCommand extends Command

public $description = 'Show useful info about Filament Shield';

public function handle(): int
public function handle(): void
{
if (class_exists(AboutCommand::class)) {
AboutCommand::add('Filament Shield', [
'Auth Provider' => Utils::getAuthProviderFQCN() . '|' . static::authProviderConfigured(),
'Resource' => Utils::isResourcePublished() ? '<fg=red;options=bold>PUBLISHED</>' : '<fg=green;options=bold>NOT PUBLISHED</>',
'Resource Slug' => Utils::getResourceSlug(),
'Resource Sort' => Utils::getResourceNavigationSort(),
'Resource Badge' => Utils::isResourceNavigationBadgeEnabled() ? '<fg=green;options=bold>ENABLED</>' : '<fg=red;options=bold>DISABLED</>',
'Resource Group' => Utils::isResourceNavigationGroupEnabled() ? '<fg=green;options=bold>ENABLED</>' : '<fg=red;options=bold>DISABLED</>',
'Translations' => is_dir(resource_path('resource/lang/vendor/filament-shield')) ? '<fg=red;options=bold>PUBLISHED</>' : '<fg=green;options=bold>NOT PUBLISHED</>',
'Views' => is_dir(resource_path('views/vendor/filament-shield')) ? '<fg=red;options=bold>PUBLISHED</>' : '<fg=green;options=bold>NOT PUBLISHED</>',
'Version' => InstalledVersions::getPrettyVersion('bezhansalleh/filament-shield'),
]);
}
AboutCommand::add('Filament Shield', [
'Auth Provider' => Utils::getAuthProviderFQCN() . '|' . static::authProviderConfigured(),
'Resource' => Utils::isResourcePublished() ? '<fg=red;options=bold>PUBLISHED</>' : '<fg=green;options=bold>NOT PUBLISHED</>',
'Resource Slug' => Utils::getResourceSlug(),
'Resource Sort' => Utils::getResourceNavigationSort(),
'Resource Badge' => Utils::isResourceNavigationBadgeEnabled() ? '<fg=green;options=bold>ENABLED</>' : '<fg=red;options=bold>DISABLED</>',
'Resource Group' => Utils::isResourceNavigationGroupEnabled() ? '<fg=green;options=bold>ENABLED</>' : '<fg=red;options=bold>DISABLED</>',
'Translations' => is_dir(resource_path('resource/lang/vendor/filament-shield')) ? '<fg=red;options=bold>PUBLISHED</>' : '<fg=green;options=bold>NOT PUBLISHED</>',
'Views' => is_dir(resource_path('views/vendor/filament-shield')) ? '<fg=red;options=bold>PUBLISHED</>' : '<fg=green;options=bold>NOT PUBLISHED</>',
'Version' => InstalledVersions::getPrettyVersion('bezhansalleh/filament-shield'),
]);

$this->call('about', [
'--only' => 'filament_shield',
]);

return self::SUCCESS;
exit(self::SUCCESS);
}

protected static function authProviderConfigured()
protected static function authProviderConfigured(): string
{
if (class_exists(Utils::getAuthProviderFQCN())) {
return Utils::isAuthProviderConfigured()
Expand Down
79 changes: 35 additions & 44 deletions src/Commands/MakeShieldGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use BezhanSalleh\FilamentShield\Support\Utils;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;

Expand All @@ -17,38 +18,32 @@ class MakeShieldGenerateCommand extends Command

/**
* The resources to generate permissions or policies for, or should be exclude.
*
* @var array
*/
protected $resources = [];
protected array $resources = [];

/**
* The pages to generate permissions for, or should be excluded.
*
* @var array
*/
protected $pages = [];
protected array $pages = [];

/**
* The widgets to generate permissions for, or should be excluded.
*
* @var array
*/
protected $widgets = [];
protected array $widgets = [];

protected $generatorOption;
protected string $generatorOption;

protected $excludeResources = false;
protected bool $excludeResources = false;

protected $excludePages = false;
protected bool $excludePages = false;

protected $excludeWidgets = false;
protected bool $excludeWidgets = false;

protected $onlyResources = false;
protected bool $onlyResources = false;

protected $onlyPages = false;
protected bool $onlyPages = false;

protected $onlyWidgets = false;
protected bool $onlyWidgets = false;

/**
* The console command signature.
Expand Down Expand Up @@ -76,15 +71,15 @@ class MakeShieldGenerateCommand extends Command
*/
public $description = 'Generate Permissions and/or Policies for Filament entities.';

public function handle(): int
public function handle(): void
{
$this->determinGeneratorOptionAndEntities();

if ($this->option('exclude') && blank($this->option('resource')) && blank($this->option('page')) && blank($this->option('widget'))) {
$this->comment('<fg=red;>No entites provided for the generators ...</>');
$this->comment('<fg=yellow;options=bold>... generation SKIPPED</>');
$this->components->error('No entites provided for the generators ...');
$this->components->alert('Generation skipped');

return self::INVALID;
exit(self::INVALID);
}

if (filled($this->option('resource')) || $this->option('all')) {
Expand All @@ -102,29 +97,29 @@ public function handle(): int
$this->widgetInfo($widgets->toArray());
}

$this->comment('Permission & Policies are generated according to your config or passed options.');
$this->info('Enjoy!');
$this->components->info('Permission & Policies are generated according to your config or passed options.');
$this->components->info('Enjoy!');

if (cache()->has('shield_general_exclude')) {
if (Cache::has('shield_general_exclude')) {
Utils::enableGeneralExclude();
cache()->forget('shield_general_exclude');
Cache::forget('shield_general_exclude');
}

return self::SUCCESS;
exit(self::SUCCESS);
}

protected function determinGeneratorOptionAndEntities(): void
{
$this->generatorOption = $this->option('option') ?? Utils::getGeneratorOption();

if ($this->option('ignore-config-exclude') && Utils::isGeneralExcludeEnabled()) {
cache()->add('shield_general_exclude', true, 3600);
Cache::add('shield_general_exclude', true, 3600);
Utils::disableGeneralExclude();
}

$this->resources = (array) explode(',', $this->option('resource'));
$this->pages = (array) explode(',', $this->option('page'));
$this->widgets = (array) explode(',', $this->option('widget'));
$this->resources = explode(',', $this->option('resource'));
$this->pages = explode(',', $this->option('page'));
$this->widgets = explode(',', $this->option('widget'));

$this->excludeResources = $this->option('exclude') && filled($this->option('resource'));
$this->excludePages = $this->option('exclude') && filled($this->option('page'));
Expand Down Expand Up @@ -218,26 +213,22 @@ protected function generateForPages(array $pages): Collection
{
return collect($pages)
->values()
->each(function ($page) {
FilamentShield::generateForPage($page);
});
->each(fn (string $page) => FilamentShield::generateForPage($page));
}

protected function generateForWidgets(array $widgets): Collection
{
return collect($widgets)
->values()
->each(function ($widget) {
FilamentShield::generateForWidget($widget);
});
->each(fn (string $widget) => FilamentShield::generateForWidget($widget));
}

protected function resourceInfo(array $resources): void
{
if ($this->option('minimal')) {
$this->info('Successfully generated Permissions & Policies.');
$this->components->info('Successfully generated Permissions & Policies.');
} else {
$this->info('Successfully generated Permissions & Policies for:');
$this->components->info('Successfully generated Permissions & Policies for:');
$this->table(
['#', 'Resource', 'Policy', 'Permissions'],
collect($resources)->map(function ($resource, $key) {
Expand All @@ -249,7 +240,7 @@ protected function resourceInfo(array $resources): void
',' . PHP_EOL,
collect(
Utils::getResourcePermissionPrefixes($resource['fqcn'])
)->map(function ($permission, $key) use ($resource) {
)->map(function ($permission) use ($resource) {
return $permission . '_' . $resource['resource'];
})->toArray()
) . ($this->generatorOption !== 'policies' ? ' ✅' : ' ❌'),
Expand All @@ -262,9 +253,9 @@ protected function resourceInfo(array $resources): void
protected function pageInfo(array $pages): void
{
if ($this->option('minimal')) {
$this->info('Successfully generated Page Permissions.');
$this->components->info('Successfully generated Page Permissions.');
} else {
$this->info('Successfully generated Page Permissions for:');
$this->components->info('Successfully generated Page Permissions for:');
$this->table(
['#', 'Page', 'Permission'],
collect($pages)->map(function ($page, $key) {
Expand All @@ -281,9 +272,9 @@ protected function pageInfo(array $pages): void
protected function widgetInfo(array $widgets): void
{
if ($this->option('minimal')) {
$this->info('Successfully generated Widget Permissions.');
$this->components->info('Successfully generated Widget Permissions.');
} else {
$this->info('Successfully generated Widget Permissions for:');
$this->components->info('Successfully generated Widget Permissions for:');
$this->table(
['#', 'Widget', 'Permission'],
collect($widgets)->map(function ($widget, $key) {
Expand All @@ -300,9 +291,9 @@ protected function widgetInfo(array $widgets): void
protected static function getPolicyStub(string $model): string
{
if (Str::is(Str::of(Utils::getAuthProviderFQCN())->afterLast('\\'), $model)) {
return (string) 'UserPolicy';
return 'UserPolicy';
}

return (string) 'DefaultPolicy';
return 'DefaultPolicy';
}
}
Loading