22
33namespace Statikbe \FilamentFlexibleContentBlockPages \Components ;
44
5- use Illuminate \Database \Eloquent \Collection ;
5+ use Illuminate \Support \Collection ;
6+ use Illuminate \Support \Facades \Cache ;
67use Illuminate \View \Component ;
8+ use Statikbe \FilamentFlexibleContentBlockPages \Components \Data \MenuData ;
79use Statikbe \FilamentFlexibleContentBlockPages \Facades \FilamentFlexibleContentBlockPages ;
810use Statikbe \FilamentFlexibleContentBlockPages \FilamentFlexibleContentBlockPagesServiceProvider ;
911
1012class Menu extends Component
1113{
12- public ?Menu $ menu ;
14+ const CACHE_MENU_KEY = 'menu_%s_%s ' ;
15+
16+ public ?MenuData $ menu ;
1317
1418 public ?Collection $ items ;
1519
@@ -22,19 +26,29 @@ public function __construct(
2226 ?string $ style = null ,
2327 ?string $ locale = null
2428 ) {
25- $ this ->menu = $ this ->getMenuByCode ($ code );
2629 $ this ->locale = $ locale ?: app ()->getLocale ();
30+ $ this ->menu = $ this ->getMenuData ($ code );
2731
2832 // Determine the style to use with proper fallback chain
2933 if ($ style ) {
3034 $ this ->style = $ style ;
31- } elseif ($ this ->menu ) {
32- $ this ->style = $ this ->menu ->getEffectiveStyle ();
3335 } else {
34- $ this ->style = FilamentFlexibleContentBlockPages:: config ()-> getDefaultMenuStyle ();
36+ $ this ->style = $ this -> getEffectiveStyle ();
3537 }
3638
37- $ this ->items = $ this ->menu ? $ this ->getMenuItems ($ this ->menu , $ this ->locale ) : [];
39+ $ this ->items = $ this ->menu ? $ this ->menu ->items : collect ();
40+ }
41+
42+ public static function getMenuCacheKey (string $ code , string $ locale ): string
43+ {
44+ return flexiblePagesPrefix (sprintf (self ::CACHE_MENU_KEY , $ locale , $ code ));
45+ }
46+
47+ public static function clearMenuCache (string $ code ): void
48+ {
49+ foreach (FilamentFlexibleContentBlockPages::config ()->getSupportedLocales () as $ locale ) {
50+ Cache::forget (self ::getMenuCacheKey ($ code , $ locale ));
51+ }
3852 }
3953
4054 public function render ()
@@ -53,44 +67,30 @@ public function render()
5367 return view ($ defaultTemplate );
5468 }
5569
56- protected function getMenuByCode (string $ code ): ?Menu
57- {
58- $ menuModel = FilamentFlexibleContentBlockPages::config ()->getMenuModel ();
59-
60- return $ menuModel ::getByCode ($ code );
61- }
62-
63- protected function getMenuItems ($ menu , ?string $ locale = null ): Collection
70+ protected function getMenuData (string $ code ): ?MenuData
6471 {
65- if (! $ menu ) {
66- return collect ();
67- }
72+ return Cache:: rememberForever ( self :: getMenuCacheKey ( $ code , $ this -> locale ), function () use ( $ code ) {
73+ $ menuModel = FilamentFlexibleContentBlockPages:: config ()-> getMenuModel ();
74+ $ menu = $ menuModel :: getByCode ( $ code );
6875
69- $ maxDepth = $ menu ->getEffectiveMaxDepth ();
70- $ eagerLoadRelations = $ this ->buildEagerLoadRelations ($ maxDepth );
76+ if (! $ menu ) {
77+ return null ;
78+ }
7179
72- // Get only top-level menu items with their visible children based on max depth
73- return $ menu ->menuItems ()
74- ->with ($ eagerLoadRelations )
75- ->visible ()
76- ->ordered ()
77- ->get ();
80+ return MenuData::create ($ menu , $ this ->locale );
81+ });
7882 }
7983
80- protected function buildEagerLoadRelations ( int $ maxDepth ): array
84+ public function getEffectiveStyle ( ): string
8185 {
82- $ relations = ['linkable ' , 'linkable.parent ' , 'linkable.parent.parent ' ];
83- $ currentPath = '' ;
84- $ depth = 1 ;
85-
86- while ($ depth <= $ maxDepth ) {
87- $ currentPath .= $ depth === 1 ? 'children ' : '.children ' ;
88- $ relations [$ currentPath ] = function ($ query ) {
89- $ query ->visible ()->ordered ()->with ('linkable ' , 'linkable.parent ' , 'linkable.parent.parent ' );
90- };
91- $ depth ++;
86+ // Return the menu's style if set, otherwise fall back to config default
87+ if ($ this ->menu && ! empty ($ this ->menu ->style )) {
88+ $ availableStyles = FilamentFlexibleContentBlockPages::config ()->getMenuStyles ();
89+ if (in_array ($ this ->menu ->style , $ availableStyles )) {
90+ return $ this ->menu ->style ;
91+ }
9292 }
9393
94- return $ relations ;
94+ return FilamentFlexibleContentBlockPages:: config ()-> getDefaultMenuStyle () ;
9595 }
9696}
0 commit comments