Skip to content

Commit 24af033

Browse files
committed
optimize and refactor.
1 parent 87151dc commit 24af033

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

packages/Webkul/Admin/src/Resources/views/settings/index.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<div class="page-content settings-container">
2222
@php
23-
$settings = menu()->getItems('admin')->first(fn ($item) => $item->getKey() == 'settings')->getChildren();
23+
$settings = menu()->getAdminMenuByKey('settings')->getChildren();
2424
@endphp
2525

2626
@foreach ($settings as $setting)

packages/Webkul/Core/src/Menu.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,18 @@ public function addItem(MenuItem $menuItem): void
4444
/**
4545
* Get all menu items.
4646
*/
47-
public function getItems(?string $area = null): Collection
47+
public function getItems(?string $area = null, string $key = ''): Collection
4848
{
4949
if (! $area) {
5050
throw new \Exception('Area must be provided to get menu items.');
5151
}
5252

53+
static $items;
54+
55+
if ($items) {
56+
return $items;
57+
}
58+
5359
$configMenu = collect(config("menu.$area"))->map(function ($item) {
5460
return Arr::except([
5561
...$item,
@@ -74,7 +80,23 @@ public function getItems(?string $area = null): Collection
7480
$this->prepareMenuItems();
7581
}
7682

77-
return collect($this->items)->sortBy(fn($item) => $item->getPosition());
83+
$items = collect($this->items)->sortBy(fn($item) => $item->getPosition());
84+
85+
return $items;
86+
}
87+
88+
/**
89+
* Get admin menu by key or keys.
90+
*/
91+
public function getAdminMenuByKey(array|string $keys): mixed
92+
{
93+
$items = $this->getItems('admin');
94+
95+
$keysArray = (array) $keys;
96+
97+
$filteredItems = $items->filter(fn($item) => in_array($item->getKey(), $keysArray));
98+
99+
return is_array($keys) ? $filteredItems : $filteredItems->first();
78100
}
79101

80102
/**

0 commit comments

Comments
 (0)