Skip to content

Commit

Permalink
fixes #304
Browse files Browse the repository at this point in the history
  • Loading branch information
bezhanSalleh committed Jan 12, 2024
1 parent 0de5069 commit 44c7567
Showing 1 changed file with 51 additions and 99 deletions.
150 changes: 51 additions & 99 deletions src/FilamentShield.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,28 +125,24 @@ public function getResources(): ?array
}

return collect($resources)
->unique()
->filter(function ($resource) {
->reject(function ($resource) {
if (Utils::isGeneralExcludeEnabled()) {
return ! in_array(
return in_array(
Str::of($resource)->afterLast('\\'),
Utils::getExcludedResouces()
);
}

return true;
})
->reduce(function ($resources, $resource) {
->mapWithKeys(function($resource) {
$name = $this->getPermissionIdentifier($resource);

$resources["{$name}"] = [
'resource' => "{$name}",
'model' => Str::of($resource::getModel())->afterLast('\\'),
'fqcn' => $resource,
return [
$name => [
'resource' => "{$name}",
'model' => Str::of($resource::getModel())->afterLast('\\'),
'fqcn' => $resource,
]
];

return $resources;
}, collect())
})
->sortKeys()
->toArray();
}
Expand Down Expand Up @@ -196,27 +192,26 @@ public static function getPages(): ?array
}

return collect($pages)
->filter(function ($page) {
->reject(function ($page) {
if (Utils::isGeneralExcludeEnabled()) {
return ! in_array(Str::afterLast($page, '\\'), Utils::getExcludedPages());
return in_array(Str::afterLast($page, '\\'), Utils::getExcludedPages());
}

return true;
})
->reduce(function ($pages, $page) {

$name = Str::of(class_basename($page))
->mapWithKeys(function ($page) {
$permission = Str::of(class_basename($page))
->prepend(
Str::of(Utils::getPagePermissionPrefix())
->append('_')
->toString()
)
->toString();

$pages["{$name}"] = "{$name}";

return $pages;
}, collect())
return [
$permission => [
'class' => $page,
'permission' => $permission
]
];
})
->toArray();
}

Expand All @@ -225,13 +220,11 @@ public static function getPages(): ?array
*/
public static function getLocalizedPageLabel(string $page): string
{
$object = static::transformClassString($page);
$pageInstance = app()->make($page);

$pageObject = new $object();

return $pageObject->getTitle()
?? $pageObject->getHeading()
?? $pageObject->getNavigationLabel()
return $pageInstance->getTitle()
?? $pageInstance->getHeading()
?? $pageInstance->getNavigationLabel()
?? '';
}

Expand All @@ -250,43 +243,34 @@ public static function getWidgets(): ?array
}

return collect($widgets)
->filter(function ($widget) {
->reject(function ($widget) {
if (Utils::isGeneralExcludeEnabled()) {
if ($widget instanceof WidgetConfiguration) {
$widget = $widget->widget;
}
return ! in_array(Str::afterLast($widget, '\\'), Utils::getExcludedWidgets());
return in_array(
needle: str(
static::getWidgetInstanceFromWidgetConfiguration($widget)
)
->afterLast('\\')
->toString(),
haystack: Utils::getExcludedWidgets()
);
}

return true;
})
->map(function($widget) {
$permission = Str::of(class_basename($widget))
->mapWithKeys(function($widget) {
$permission = Str::of(class_basename(static::getWidgetInstanceFromWidgetConfiguration($widget)))
->prepend(
Str::of(Utils::getWidgetPermissionPrefix())
->append('_')
->toString()
)
->toString();

return [
'class' => $widget,
'permission' => $permission
$permission => [
'class' => static::getWidgetInstanceFromWidgetConfiguration($widget),
'permission' => $permission
]
];
})
// ->reduce(function ($widgets, $widget) {

// $name = Str::of(class_basename($widget))
// ->prepend(
// Str::of(Utils::getWidgetPermissionPrefix())
// ->append('_')
// ->toString()
// )
// ->toString();

// $widgets["{$name}"] = "{$name}";

// return $widgets;
// }, collect())
->toArray();
}

Expand All @@ -295,57 +279,18 @@ public static function getWidgets(): ?array
*/
public static function getLocalizedWidgetLabel(string $widget): string
{
$classString = str($widget)
->after(
str(Utils::getWidgetPermissionPrefix())
->append('_')
)
->studly()
->toString();
$class = class_basename($classString);

$widgetInstance = app()->make($class);

if ($widgetInstance instanceof WidgetConfiguration) {
$widgetInstance = $widgetInstance->widget;
}
$widgetInstance = app()->make($widget);

return match (true) {
$widgetInstance instanceof TableWidget => (string) invade($widgetInstance)->makeTable()->getHeading(),
! ($widgetInstance instanceof TableWidget) && $widgetInstance instanceof Widget && method_exists($widgetInstance, 'getHeading') => (string) invade($widgetInstance)->getHeading(),
default => Str::of($widget)
->after(Utils::getWidgetPermissionPrefix() . '_')
default => str($widget)
->afterLast('\\')
->headline()
->toString(),
};
}

protected static function transformClassString(string $string, bool $isPageClass = true): string
{
$pages = Filament::getPages();
if (Utils::discoverAllPages()) {
$pages = [];
foreach (Filament::getPanels() as $panel) {
$pages = array_merge($pages, $panel->getPages());
}
$pages = array_unique($pages);
}

$widgets = Filament::getWidgets();
if (Utils::discoverAllWidgets()) {
$widgets = [];
foreach (Filament::getPanels() as $panel) {
$widgets = array_merge($widgets, $panel->getWidgets());
}
$widgets = array_unique($widgets);
}

$prefix = Str::of($isPageClass ? Utils::getPagePermissionPrefix() : Utils::getWidgetPermissionPrefix())->append('_');

return (string) collect($isPageClass ? $pages : $widgets)
->first(fn ($item) => class_basename($item) == Str::of($string)->after($prefix)->studly());
}

protected function getDefaultPermissionIdentifier(string $resource): string
{
return Str::of($resource)
Expand All @@ -355,4 +300,11 @@ protected function getDefaultPermissionIdentifier(string $resource): string
->snake()
->replace('_', '::');
}

protected static function getWidgetInstanceFromWidgetConfiguration(string|WidgetConfiguration $widget):string
{
return $widget instanceof WidgetConfiguration
? $widget->widget
: $widget;
}
}

0 comments on commit 44c7567

Please sign in to comment.