From c79ecfb6c6b600d8660ae98593e6641198cb8cdd Mon Sep 17 00:00:00 2001 From: Mathieu TUDISCO Date: Wed, 11 Dec 2024 11:45:28 +0100 Subject: [PATCH] Allow adding custom Macroable classes. --- README.md | 4 ++++ config/ide-helper.php | 13 +++++++++++++ src/Generator.php | 13 ++++++++++--- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5baba5e20..38d31361e 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,10 @@ Str::macro('concat', function(string $str1, string $str2) : string { }); ``` +You can add any custom Macroable traits to detect in the `macroable_traits` config option. + +```php + ### Automatic PHPDocs for models If you don't want to write your properties yourself, you can use the command `php artisan ide-helper:models` to generate diff --git a/config/ide-helper.php b/config/ide-helper.php index 5519dca8b..578f87dc4 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -334,4 +334,17 @@ // 'ide-helper:models --nowrite', ], + /* + |-------------------------------------------------------------------------- + | Macroable Traits + |-------------------------------------------------------------------------- + | + | Define which traits should be considered capable of adding Macro. + | You can add any custom trait that behaves like the original Laravel one. + | + */ + 'macroable_traits' => [ + Illuminate\Support\Traits\Macroable::class, + ], + ]; diff --git a/src/Generator.php b/src/Generator.php index 1fcdbba26..ff6d7d831 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -15,7 +15,6 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Facade; use Illuminate\Support\Str; -use Illuminate\Support\Traits\Macroable; use PhpParser\Lexer\Emulative; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Namespace_; @@ -38,6 +37,7 @@ class Generator protected $magic = []; protected $interfaces = []; protected $helpers; + protected array $macroableTraits = []; /** * @param \Illuminate\Config\Repository $config @@ -62,6 +62,7 @@ public function __construct( $this->extra = array_merge($this->extra, $this->config->get('ide-helper.extra'), []); $this->magic = array_merge($this->magic, $this->config->get('ide-helper.magic'), []); $this->interfaces = array_merge($this->interfaces, $this->config->get('ide-helper.interfaces'), []); + $this->macroableTraits = array_merge($this->macroableTraits, $this->config->get('ide-helper.macroable_traits'), []); // Make all interface classes absolute foreach ($this->interfaces as &$interface) { $interface = '\\' . ltrim($interface, '\\'); @@ -347,8 +348,14 @@ protected function getMacroableClasses(Collection $aliases) ->filter(function ($class) { $traits = class_uses_recursive($class); - // Filter only classes with the macroable trait - return isset($traits[Macroable::class]); + // Filter only classes with a macroable trait + foreach ($this->macroableTraits as $trait) { + if (isset($traits[$trait])) { + return true; + } + } + + return false; }) ->filter(function ($class) use ($aliases) { $class = Str::start($class, '\\');