Skip to content

Commit

Permalink
Early stop applying filters on a restricted item (#719)
Browse files Browse the repository at this point in the history
* [src/Menu]: Improve menu builder to early stop applying filters on a restricted menu item.

* [src/Menu]: Fix code style.
  • Loading branch information
dfsmania authored Sep 26, 2020
1 parent 3332391 commit baa10b8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion config/adminlte.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,11 @@
*/

'filters' => [
JeroenNoten\LaravelAdminLte\Menu\Filters\GateFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\HrefFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\SearchFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\ActiveFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\ClassesFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\GateFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\LangFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\DataFilter::class,
],
Expand Down
20 changes: 14 additions & 6 deletions src/Menu/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,26 @@ protected function applyFilters($item)
return $item;
}

// If the item is a submenu, transform all submenu items first.

if (MenuItemHelper::isSubmenu($item)) {
$item['submenu'] = $this->transformItems($item['submenu']);
}

// Now, apply all the filters on the item.

foreach ($this->filters as $filter) {

// If the item is not allowed to be shown, there is no sense to
// continue applying the filters.

if (! MenuItemHelper::isAllowed($item)) {
return $item;
}

$item = $filter->transform($item);
}

// If the item is a submenu, transform all submenu items too.

if (MenuItemHelper::isSubmenu($item)) {
$item['submenu'] = $this->transformItems($item['submenu']);
}

return $item;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class TestCase extends BaseTestCase
protected function makeMenuBuilder($uri = 'http://example.com', GateContract $gate = null, $locale = 'en')
{
return new Builder([
new GateFilter($gate ?: $this->makeGate()),
new HrefFilter($this->makeUrlGenerator($uri)),
new ActiveFilter($this->makeActiveChecker($uri)),
new ClassesFilter(),
new DataFilter(),
new GateFilter($gate ?: $this->makeGate()),
new LangFilter($this->makeTranslator($locale)),
new SearchFilter(),
]);
Expand Down

0 comments on commit baa10b8

Please sign in to comment.