|
9 | 9 | class RegisterMenu extends Facade |
10 | 10 | { |
11 | 11 | protected static array $menus = []; |
12 | | - protected static string|null $currentGroup = null; |
| 12 | + protected static array $groups = ['index' => 0, 'group_name' => '']; |
13 | 13 |
|
14 | | - public static function groupping(string $label): void |
| 14 | + protected static function getFacadeAccessor() |
15 | 15 | { |
16 | | - static::$currentGroup = $label; |
17 | | - |
18 | | - if (!isset(static::$menus[$label])) { |
19 | | - static::$menus[$label] = [ |
20 | | - 'label' => $label, |
21 | | - 'children' => [], |
22 | | - ]; |
23 | | - } |
| 16 | + return 'sukristyan.menu'; |
24 | 17 | } |
25 | 18 |
|
26 | | - public static function endGroup(): void |
| 19 | + public static function all(): array |
27 | 20 | { |
28 | | - static::$currentGroup = null; |
| 21 | + return static::$menus; |
29 | 22 | } |
30 | 23 |
|
31 | | - public static function add(Route $route, string $label): void |
| 24 | + protected static function add(Route $route, string $label, string $groupLabel = '') |
32 | 25 | { |
33 | | - if (static::$currentGroup) { |
34 | | - static::$menus[static::$currentGroup]['children'][] = [ |
35 | | - 'route_name' => $route->getName(), |
36 | | - 'uri' => $route->uri(), |
37 | | - 'label' => $label, |
38 | | - ]; |
| 26 | + if (!empty($groupLabel)) { |
| 27 | + self::createAsGroup($route, $label, $groupLabel); |
39 | 28 | } else { |
40 | | - static::$menus[] = [ |
41 | | - 'route_name' => $route->getName(), |
42 | | - 'uri' => $route->uri(), |
43 | | - 'label' => $label, |
44 | | - ]; |
| 29 | + static::$menus[] = array_merge( |
| 30 | + ['label' => $label], |
| 31 | + config('laravel-menu-wrapper.populate_items')($route) |
| 32 | + ); |
45 | 33 | } |
46 | 34 | } |
47 | 35 |
|
48 | | - public static function all(): array |
| 36 | + private static function createAsGroup(Route $route, string $label, string $groupLabel): array |
49 | 37 | { |
50 | | - return static::$menus; |
| 38 | + if ( |
| 39 | + !in_array( |
| 40 | + $given = config('laravel-menu-wrapper.group_as', 'key'), |
| 41 | + $expect = ['item', 'key'], |
| 42 | + true |
| 43 | + ) |
| 44 | + ) { |
| 45 | + throw InvalidGroupException::create($given, $expect); |
| 46 | + } |
| 47 | + |
| 48 | + foreach (static::$menus as $id => $menu) { |
| 49 | + if (is_array($menu) && ($menu['group_name'] ?? null) === $groupLabel) { |
| 50 | + static::$groups = ['id' => $id, 'group_name' => $groupLabel]; |
| 51 | + return match ($given) { |
| 52 | + 'key' => self::groupAsKey($route, $label, $groupLabel), |
| 53 | + 'item' => self::groupAsItem($route, $label, $groupLabel), |
| 54 | + }; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + $newId = count(static::$menus); |
| 59 | + static::$groups = ['id' => $newId, 'group_name' => $groupLabel]; |
| 60 | + |
| 61 | + return match ($given) { |
| 62 | + 'key' => self::groupAsKey($route, $label, $groupLabel), |
| 63 | + 'item' => self::groupAsItem($route, $label, $groupLabel), |
| 64 | + }; |
51 | 65 | } |
52 | 66 |
|
53 | | - private static function groupAs() |
| 67 | + private static function groupAsKey(Route $route, string $label, string $groupLabel): array |
54 | 68 | { |
55 | | - if (!in_array($given = config('laravel-menu-wrapper.group_as'), $expect = ['item', 'key'])) { |
56 | | - throw InvalidGroupException::create($given, $expect); |
| 69 | + return static::$menus[$groupLabel][] = array_merge( |
| 70 | + ['label' => $label], |
| 71 | + config('laravel-menu-wrapper.populate_items')($route) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + private static function groupAsItem(Route $route, string $label, string $groupLabel): array |
| 76 | + { |
| 77 | + $id = static::$groups['id']; |
| 78 | + |
| 79 | + if (!isset(static::$menus[$id]['childs'])) { |
| 80 | + static::$menus[$id] = [ |
| 81 | + 'group_name' => $groupLabel, |
| 82 | + 'childs' => [] |
| 83 | + ]; |
57 | 84 | } |
58 | 85 |
|
59 | | - return config('laravel-menu-wrapper.group_as'); |
| 86 | + static::$menus[$id]['childs'][] = array_merge( |
| 87 | + ['label' => $label], |
| 88 | + config('laravel-menu-wrapper.populate_items')($route) |
| 89 | + ); |
| 90 | + return static::$menus[$id]; |
60 | 91 | } |
61 | 92 | } |
0 commit comments