Skip to content

Commit

Permalink
performance improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
bezhanSalleh committed Jan 12, 2024
1 parent 44c7567 commit c9f8c25
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 64 deletions.
20 changes: 10 additions & 10 deletions src/Commands/MakeShieldGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ protected function generatablePages(): ?array
return collect(FilamentShield::getPages())
->filter(function ($page) {
if ($this->excludePages) {
return ! in_array($page, $this->pages);
return ! in_array($page['class'], $this->pages);
}

if ($this->onlyPages) {
return in_array($page, $this->pages);
return in_array($page['class'], $this->pages);
}

return true;
Expand All @@ -169,11 +169,11 @@ protected function generatableWidgets(): ?array
return collect(FilamentShield::getWidgets())
->filter(function ($widget) {
if ($this->excludeWidgets) {
return ! in_array($widget, $this->widgets);
return ! in_array($widget['class'], $this->widgets);
}

if ($this->onlyWidgets) {
return in_array($widget, $this->widgets);
return in_array($widget['class'], $this->widgets);
}

return true;
Expand Down Expand Up @@ -213,14 +213,14 @@ protected function generateForPages(array $pages): Collection
{
return collect($pages)
->values()
->each(fn (string $page) => FilamentShield::generateForPage($page));
->each(fn (array $page) => FilamentShield::generateForPage($page['permission']));
}

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

protected function resourceInfo(array $resources): void
Expand Down Expand Up @@ -261,8 +261,8 @@ protected function pageInfo(array $pages): void
collect($pages)->map(function ($page, $key) {
return [
'#' => $key + 1,
'Page' => Str::replace(config('filament-shield.permission_prefixes.page') . '_', '', $page),
'Permission' => $page,
'Page' => $page['class'],
'Permission' => $page['permission'],
];
})
);
Expand All @@ -280,8 +280,8 @@ protected function widgetInfo(array $widgets): void
collect($widgets)->map(function ($widget, $key) {
return [
'#' => $key + 1,
'Widget' => Str::replace(config('filament-shield.permission_prefixes.widget') . '_', '', $widget),
'Permission' => $widget,
'Widget' => $widget['class'],
'Permission' => $widget['permission'],
];
})
);
Expand Down
101 changes: 47 additions & 54 deletions src/Resources/RoleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,55 +319,21 @@ public static function canGloballySearch(): bool

public static function getResourceEntitiesSchema(): ?array
{
if (blank(static::$permissionsCollection)) {
static::$permissionsCollection = Utils::getPermissionModel()::all();
}
static::$permissionsCollection = static::$permissionsCollection ?: Utils::getPermissionModel()::all();

return collect(FilamentShield::getResources())->sortKeys()->reduce(function ($entities, $entity) {

$entities[] = Forms\Components\Section::make(FilamentShield::getLocalizedResourceLabel($entity['fqcn']))
->description(fn () => new HtmlString('<span style="word-break: break-word;">' . Utils::showModelPath($entity['fqcn']) . '</span>'))
->compact()
->schema([
Forms\Components\CheckboxList::make($entity['resource'])
->label('')
->options(fn (): array => static::getResourcePermissionOptions($entity))
->live()
->afterStateHydrated(function (Component $component, $livewire, string $operation, ?Model $record, Forms\Set $set) use ($entity) {
static::setPermissionStateForRecordPermissions(
component: $component,
operation: $operation,
permissions: static::getResourcePermissionOptions($entity),
record: $record
);

static::toggleSelectAllViaEntities($livewire, $set);
})
->afterStateUpdated(fn ($livewire, Forms\Set $set) => static::toggleSelectAllViaEntities($livewire, $set))
->selectAllAction(fn (FormAction $action, Component $component, $livewire, Forms\Set $set) => static::bulkToggleableAction(
action: $action,
component: $component,
livewire: $livewire,
set: $set
))
->deselectAllAction(fn (FormAction $action, Component $component, $livewire, Forms\Set $set) => static::bulkToggleableAction(
action: $action,
component: $component,
livewire: $livewire,
set: $set,
resetState: true
))
->dehydrated(fn ($state) => blank($state) ? false : true)
->bulkToggleable()
->gridDirection('row')
->columns(FilamentShieldPlugin::get()->getResourceCheckboxListColumns()),
])
->columnSpan(FilamentShieldPlugin::get()->getSectionColumnSpan())
->collapsible();

return $entities;
}, collect())
?->toArray() ?? [];
return collect(FilamentShield::getResources())
->sortKeys()
->map(function ($entity) {
return Forms\Components\Section::make(FilamentShield::getLocalizedResourceLabel($entity['fqcn']))
->description(fn () => new HtmlString('<span style="word-break: break-word;">' . Utils::showModelPath($entity['fqcn']) . '</span>'))
->compact()
->schema([
static::getCheckBoxListComponentForResource($entity),
])
->columnSpan(FilamentShieldPlugin::get()->getSectionColumnSpan())
->collapsible();
})
->toArray();
}

public static function getResourceTabBadgeCount(): ?int
Expand Down Expand Up @@ -445,17 +411,17 @@ public static function toggleSelectAllViaEntities($livewire, Forms\Set $set): vo
public static function getPageOptions(): array
{
return collect(FilamentShield::getPages())
->flatMap(fn ($pagePermission) => [
$pagePermission => FilamentShield::getLocalizedPageLabel($pagePermission),
->flatMap(fn ($page) => [
$page['permission'] => FilamentShield::getLocalizedPageLabel($page['class']),
])
->toArray();
}

public static function getWidgetOptions(): array
{
return collect(FilamentShield::getWidgets())
->flatMap(fn ($widgetPermission) => [
$widgetPermission => FilamentShield::getLocalizedWidgetLabel($widgetPermission),
->flatMap(fn ($widget) => [
$widget['permission'] => FilamentShield::getLocalizedWidgetLabel($widget['class']),
])
->toArray();
}
Expand All @@ -479,8 +445,8 @@ protected static function getCustomEntities(): ?Collection
});

$entitiesPermissions = $resourcePermissions
->merge(FilamentShield::getPages())
->merge(FilamentShield::getWidgets())
->merge(collect(FilamentShield::getPages())->map(fn($page) => $page['permission'])->values())
->merge(collect(FilamentShield::getWidgets())->map(fn ($widget) => $widget['permission'])->values())
->values();

return static::$permissionsCollection->whereNotIn('name', $entitiesPermissions)->pluck('name');
Expand All @@ -496,4 +462,31 @@ public static function bulkToggleableAction(FormAction $action, Component $compo
static::toggleSelectAllViaEntities($livewire, $set);
});
}

public static function getCheckBoxListComponentForResource(array $entity): Component
{
$permissionsArray = static::getResourcePermissionOptions($entity);

return Forms\Components\CheckboxList::make($entity['resource'])
->label('')
->options(fn (): array => $permissionsArray)
->live()
->afterStateHydrated(function (Component $component, $livewire, string $operation, ?Model $record, Forms\Set $set) use ($permissionsArray) {
static::setPermissionStateForRecordPermissions(
component: $component,
operation: $operation,
permissions: $permissionsArray,
record: $record
);

static::toggleSelectAllViaEntities($livewire, $set);
})
->afterStateUpdated(fn ($livewire, Forms\Set $set) => static::toggleSelectAllViaEntities($livewire, $set))
->selectAllAction(fn (FormAction $action, Component $component, $livewire, Forms\Set $set) => static::bulkToggleableAction($action, $component, $livewire, $set))
->deselectAllAction(fn (FormAction $action, Component $component, $livewire, Forms\Set $set) => static::bulkToggleableAction($action, $component, $livewire, $set, true))
->dehydrated(fn ($state) => !blank($state))
->bulkToggleable()
->gridDirection('row')
->columns(FilamentShieldPlugin::get()->getResourceCheckboxListColumns());
}
}

0 comments on commit c9f8c25

Please sign in to comment.