From 3668592d48e46284be77394b267c8125d6921202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rube=CC=81n=20Robles?= Date: Wed, 17 Feb 2021 15:25:57 +0100 Subject: [PATCH] wip --- config/menus.php | 2 +- src/Manager.php | 30 ++++++++++++++++++------------ src/Menu.php | 10 ++++++---- src/ServiceProvider.php | 2 ++ 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/config/menus.php b/config/menus.php index 8420f52..98a81d8 100644 --- a/config/menus.php +++ b/config/menus.php @@ -8,7 +8,7 @@ 'integrations' => [ - 'inertia' => true, + 'inertia' => false, 'blade' => false, diff --git a/src/Manager.php b/src/Manager.php index 0c78fb5..b1e16b2 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -18,15 +18,20 @@ class Manager public static $menus = []; /** - * Register the given menus. + * @var array + */ + protected static $active = []; + + /** + * Activate the given menus. * * @param array $menus * * @return static */ - public static function menus(array $menus) + public static function activate(array $menus) { - static::$menus = array_merge(static::$menus, $menus); + static::$active = array_merge_recursive(static::$active, $menus); return new static(); } @@ -59,9 +64,10 @@ public static function menusIn($directory) } } - static::menus( - Collection::make($menus)->sort()->mapWithKeys(fn ($class) => [$class => $class::$routes])->all() - ); + static::$menus = Collection::make($menus) + ->sort() + ->mapWithKeys(fn ($class) => [$class => $class::$routes]) + ->all(); } /** @@ -85,18 +91,18 @@ public static function enabled($integration) * * @return void */ - public static function dataInject(Request $request, $key, $data) + public static function dataInject(Request $request) { - if (static::enabled('inertia') && $request->inertia()) { - inertia()->share($key, array_merge(inertia()->getShared($key), $data)); - } - if (static::enabled('json') && $request->wantsJson()) { // TODO: } if (static::enabled('blade') && !$request->wantsJson()) { - Session::flash(static::prefix(), array_merge(Session::get(static::prefix(), []), $data)); + Session::flash(static::prefix(), static::$active); + } + + if (static::enabled('inertia') && !$request->wantsJson()) { + inertia()->share(static::prefix(), static::$active); } } diff --git a/src/Menu.php b/src/Menu.php index 1deb8d2..778c0e3 100644 --- a/src/Menu.php +++ b/src/Menu.php @@ -4,6 +4,7 @@ use DateInterval; use Illuminate\Http\Request; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Str; @@ -30,10 +31,11 @@ abstract class Menu implements Arrayable */ public function __construct(Request $request) { - $resolved = $this->resolve($request); + $activeMenuArr = []; - Manager::dataInject($request, $this->identifier(), $resolved) - && $resolved; + Arr::set($activeMenuArr, $this->identifier(), $this->resolve($request)); + + Manager::activate($activeMenuArr); } /** @@ -129,7 +131,7 @@ protected function addLink($title, $uri, $params = [], $meta = []) * * @return \SkoreLabs\LaravelMenuBuilder\MenuGroup */ - public function addGroup($title = null, array $items = []) + public function addGroup($title = 'default', array $items = []) { return new MenuGroup($title, $items); } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index eb4105a..f60d518 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -57,6 +57,8 @@ public function register() ? $menu::make($event->request) : null; } + + Manager::dataInject($event->request); }); } }