Skip to content

Commit

Permalink
Allow adding custom Macroable classes. (#1629)
Browse files Browse the repository at this point in the history
* Allow adding custom Macroable classes.

* Remove duplication, tweak config

* Remove test

---------

Co-authored-by: Barry vd. Heuvel <[email protected]>
  • Loading branch information
mathieutu and barryvdh authored Jan 1, 2025
1 parent beda399 commit 05d9c3c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ Str::macro('concat', function(string $str1, string $str2) : string {
});
```

You can add any custom Macroable traits to detect in the `macroable_traits` config option.

### 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
14 changes: 14 additions & 0 deletions config/ide-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,18 @@
// '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' => [
\Filament\Support\Concerns\Macroable::class,
\Spatie\Macroable\Macroable::class,
],

];
8 changes: 5 additions & 3 deletions src/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class Alias
protected $phpdoc = null;
protected $classAliases = [];

protected $isMacroable = false;

/** @var ConfigRepository */
protected $config;

Expand All @@ -60,12 +62,13 @@ class Alias
* @param array $magicMethods
* @param array $interfaces
*/
public function __construct($config, $alias, $facade, $magicMethods = [], $interfaces = [])
public function __construct($config, $alias, $facade, $magicMethods = [], $interfaces = [], $isMacroable = false)
{
$this->alias = $alias;
$this->magicMethods = $magicMethods;
$this->interfaces = $interfaces;
$this->config = $config;
$this->isMacroable = $isMacroable;

// Make the class absolute
$facade = '\\' . ltrim($facade, '\\');
Expand Down Expand Up @@ -395,8 +398,7 @@ protected function detectMethods()

// Check if the class is macroable
// (Eloquent\Builder is also macroable but doesn't use Macroable trait)
$traits = collect($reflection->getTraitNames());
if ($traits->contains('Illuminate\Support\Traits\Macroable') || $class === EloquentBuilder::class) {
if ($this->isMacroable || $class === EloquentBuilder::class) {
$properties = $reflection->getStaticProperties();
$macros = isset($properties['macros']) ? $properties['macros'] : [];
foreach ($macros as $macro_name => $macro_func) {
Expand Down
17 changes: 14 additions & 3 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Generator
protected $magic = [];
protected $interfaces = [];
protected $helpers;
protected array $macroableTraits = [];

/**
* @param \Illuminate\Config\Repository $config
Expand Down Expand Up @@ -341,7 +342,7 @@ protected function addMacroableClasses(Collection $aliases)
continue;
}

$aliases[] = new Alias($this->config, $class, $class, [], $this->interfaces);
$aliases[] = new Alias($this->config, $class, $class, [], $this->interfaces, true);
}
}

Expand All @@ -363,8 +364,18 @@ 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]);
if (isset($traits[Macroable::class])) {
return true;
}

// Filter only classes with a macroable trait
foreach ($this->config->get('ide-helper.macroable_traits', []) as $trait) {
if (isset($traits[$trait])) {
return true;
}
}

return false;
})
->filter(function ($class) use ($aliases) {
$class = Str::start($class, '\\');
Expand Down
27 changes: 1 addition & 26 deletions tests/AliasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,6 @@
*/
class AliasTest extends TestCase
{
/**
* @covers ::detectMethods
*/
public function testDetectMethodsMacroableMacros(): void
{
// Mock
$macro = __FUNCTION__;
$alias = new AliasMock();

// Macros
Builder::macro(
$macro,
function () {
// empty
}
);

// Prepare
$alias->setClasses([Builder::class]);
$alias->detectMethods();

// Test
$this->assertNotNull($this->getAliasMacro($alias, Builder::class, $macro));
}

/**
* @covers ::detectMethods
*/
Expand All @@ -50,7 +25,7 @@ public function testDetectMethodsEloquentBuilderMacros(): void
$macro = __FUNCTION__;
$alias = new AliasMock();

// Macros
// Macrosx
EloquentBuilder::macro(
$macro,
function () {
Expand Down

0 comments on commit 05d9c3c

Please sign in to comment.