Skip to content

Commit

Permalink
Allow adding custom Macroable classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieutu committed Dec 11, 2024
1 parent 4d2728c commit c79ecfb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions config/ide-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],

];
13 changes: 10 additions & 3 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand All @@ -38,6 +37,7 @@ class Generator
protected $magic = [];
protected $interfaces = [];
protected $helpers;
protected array $macroableTraits = [];

/**
* @param \Illuminate\Config\Repository $config
Expand All @@ -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, '\\');
Expand Down Expand Up @@ -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, '\\');
Expand Down

0 comments on commit c79ecfb

Please sign in to comment.