1515use Illuminate \Foundation \AliasLoader ;
1616use Illuminate \Support \Collection ;
1717use Illuminate \Support \Facades \Facade ;
18+ use Illuminate \Support \Facades \Config ;
1819use Illuminate \Support \Str ;
1920use Illuminate \Support \Traits \Macroable ;
2021use ReflectionClass ;
@@ -35,7 +36,7 @@ class Generator
3536 protected $ magic = [];
3637 protected $ interfaces = [];
3738 protected $ helpers ;
38- protected array $ macroableTraits = [] ;
39+ protected static ? array $ _macroableTraits = null ;
3940
4041 /**
4142 * @param \Illuminate\Config\Repository $config
@@ -58,9 +59,9 @@ public function __construct(
5859 // Find the drivers to add to the extra/interfaces
5960 $ this ->detectDrivers ();
6061
61- $ this ->extra = array_merge ($ this ->extra , $ this ->config ->get ('ide-helper.extra ' ) , []);
62- $ this ->magic = array_merge ($ this ->magic , $ this ->config ->get ('ide-helper.magic ' ) , []);
63- $ this ->interfaces = array_merge ($ this ->interfaces , $ this ->config ->get ('ide-helper.interfaces ' ) , []);
62+ $ this ->extra = array_merge ($ this ->extra , $ this ->config ->get ('ide-helper.extra ' , []) );
63+ $ this ->magic = array_merge ($ this ->magic , $ this ->config ->get ('ide-helper.magic ' , []) );
64+ $ this ->interfaces = array_merge ($ this ->interfaces , $ this ->config ->get ('ide-helper.interfaces ' , []) );
6465 // Make all interface classes absolute
6566 foreach ($ this ->interfaces as &$ interface ) {
6667 $ interface = '\\' . ltrim ($ interface , '\\' );
@@ -360,7 +361,7 @@ protected function addMacroableClasses(Collection $aliases)
360361 continue ;
361362 }
362363
363- $ aliases [] = new Alias ($ this ->config , $ class , $ class , [], $ this ->interfaces , true );
364+ $ aliases [] = new Alias ($ this ->config , $ class , $ class , [], $ this ->interfaces );
364365 }
365366 }
366367
@@ -379,22 +380,7 @@ protected function getMacroableClasses(Collection $aliases)
379380 // Filter out internal classes and class aliases
380381 return !$ reflection ->isInternal () && $ reflection ->getName () === $ class ;
381382 })
382- ->filter (function ($ class ) {
383- $ traits = class_uses_recursive ($ class );
384-
385- if (isset ($ traits [Macroable::class])) {
386- return true ;
387- }
388-
389- // Filter only classes with a macroable trait
390- foreach ($ this ->config ->get ('ide-helper.macroable_traits ' , []) as $ trait ) {
391- if (isset ($ traits [$ trait ])) {
392- return true ;
393- }
394- }
395-
396- return false ;
397- })
383+ ->filter (fn ($ class ) => static ::hasMacroableTrait (class_uses_recursive ($ class )))
398384 ->filter (function ($ class ) use ($ aliases ) {
399385 $ class = Str::start ($ class , '\\' );
400386
@@ -404,4 +390,19 @@ protected function getMacroableClasses(Collection $aliases)
404390 });
405391 });
406392 }
393+
394+ public static function hasMacroableTrait ($ traits )
395+ {
396+ if (!static ::$ _macroableTraits ) {
397+ static ::$ _macroableTraits = array_merge ([Macroable::class], Config::get ('ide-helper.macroable_traits ' , []));;
398+ }
399+
400+ foreach (static ::$ _macroableTraits as $ trait ) {
401+ if (isset ($ traits [$ trait ])) {
402+ return true ;
403+ }
404+ }
405+
406+ return false ;
407+ }
407408}
0 commit comments