Skip to content

Commit b5a9d6a

Browse files
committed
initial commit
1 parent a1c98a3 commit b5a9d6a

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
.DS_Store

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "sukristyan/laravel-menu-wrapper",
3+
"description": "A lightweight Laravel package to simplify building dynamic and structured menus in your applications.",
4+
"type": "library",
5+
"keywords": [
6+
"laravel",
7+
"menu",
8+
"dynamic menu",
9+
"laravel-menu",
10+
"group menu"
11+
],
12+
"license": "MIT",
13+
"autoload": {
14+
"psr-4": {
15+
"Sukristyan\\LaravelMenuWrapper\\": "src/"
16+
}
17+
},
18+
"authors": [
19+
{
20+
"name": "sukristyan",
21+
"email": "hi@galih.me"
22+
}
23+
],
24+
"require": {
25+
"php": "^8.1"
26+
},
27+
"minimum-stability": "dev",
28+
"prefer-stable": true,
29+
"config": {
30+
"sort-packages": true
31+
},
32+
"extra": {
33+
"laravel": {
34+
"providers": [
35+
"Sukristyan\\LaravelMenuWrapper\\CustomRouteProvider"
36+
]
37+
}
38+
}
39+
}

src/CustomRouteProvider.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Sukristyan\LaravelMenuWrapper;
4+
5+
use Illuminate\Routing\Route;
6+
use Illuminate\Support\ServiceProvider;
7+
use Sukristyan\LaravelMenuWrapper\Facade\RegisterMenu;
8+
9+
final class CustomRouteProvider extends ServiceProvider
10+
{
11+
/**
12+
* Register any application services.
13+
*/
14+
public function register(): void
15+
{
16+
$this->app->singleton('navigation-menu', function () {
17+
return new RegisterMenu();
18+
});
19+
}
20+
21+
/**
22+
* Bootstrap any application services.
23+
*/
24+
public function boot(): void
25+
{
26+
Route::macro('groupMenu', function (string $label) {
27+
RegisterMenu::groupping($label);
28+
return $this;
29+
});
30+
31+
Route::macro('menu', function (string $label) {
32+
RegisterMenu::add($this->uri(), $label);
33+
return $this;
34+
});
35+
36+
Route::macro('group', function ($attributes, $routes) {
37+
$result = \Illuminate\Support\Facades\Route::buildGroup($attributes, $routes);
38+
RegisterMenu::endGroupping();
39+
return $result;
40+
});
41+
}
42+
}

src/Facade/RegisterMenu.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Sukristyan\LaravelMenuWrapper\Facade;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class RegisterMenu extends Facade
8+
{
9+
protected static $menus = [];
10+
protected static $currentGroup = null;
11+
12+
public static function groupping($label)
13+
{
14+
self::$currentGroup = $label;
15+
self::$menus[$label] = [];
16+
}
17+
18+
public static function endGroupping()
19+
{
20+
self::$currentGroup = null;
21+
}
22+
23+
public static function add($uri, $label)
24+
{
25+
if (self::$currentGroup) {
26+
self::$menus[self::$currentGroup][$uri] = $label;
27+
}
28+
}
29+
30+
public static function all()
31+
{
32+
return self::$menus;
33+
}
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
/**
5+
* ----------------------------------------------------------------------
6+
* Determine the grouping method that will be used to group your menus
7+
* this will only apply if you are using groupMenu.
8+
* ----------------------------------------------------------------------
9+
* Refer to the README.md
10+
* Possible value: key, item
11+
*/
12+
'group_by' => 'key'
13+
];

0 commit comments

Comments
 (0)