Skip to content

Commit 05d9c3c

Browse files
mathieutubarryvdh
andauthored
Allow adding custom Macroable classes. (#1629)
* Allow adding custom Macroable classes. * Remove duplication, tweak config * Remove test --------- Co-authored-by: Barry vd. Heuvel <[email protected]>
1 parent beda399 commit 05d9c3c

File tree

5 files changed

+36
-32
lines changed

5 files changed

+36
-32
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ Str::macro('concat', function(string $str1, string $str2) : string {
115115
});
116116
```
117117

118+
You can add any custom Macroable traits to detect in the `macroable_traits` config option.
119+
118120
### Automatic PHPDocs for models
119121

120122
If you don't want to write your properties yourself, you can use the command `php artisan ide-helper:models` to generate

config/ide-helper.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,18 @@
334334
// 'ide-helper:models --nowrite',
335335
],
336336

337+
/*
338+
|--------------------------------------------------------------------------
339+
| Macroable Traits
340+
|--------------------------------------------------------------------------
341+
|
342+
| Define which traits should be considered capable of adding Macro.
343+
| You can add any custom trait that behaves like the original Laravel one.
344+
|
345+
*/
346+
'macroable_traits' => [
347+
\Filament\Support\Concerns\Macroable::class,
348+
\Spatie\Macroable\Macroable::class,
349+
],
350+
337351
];

src/Alias.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class Alias
4646
protected $phpdoc = null;
4747
protected $classAliases = [];
4848

49+
protected $isMacroable = false;
50+
4951
/** @var ConfigRepository */
5052
protected $config;
5153

@@ -60,12 +62,13 @@ class Alias
6062
* @param array $magicMethods
6163
* @param array $interfaces
6264
*/
63-
public function __construct($config, $alias, $facade, $magicMethods = [], $interfaces = [])
65+
public function __construct($config, $alias, $facade, $magicMethods = [], $interfaces = [], $isMacroable = false)
6466
{
6567
$this->alias = $alias;
6668
$this->magicMethods = $magicMethods;
6769
$this->interfaces = $interfaces;
6870
$this->config = $config;
71+
$this->isMacroable = $isMacroable;
6972

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

396399
// Check if the class is macroable
397400
// (Eloquent\Builder is also macroable but doesn't use Macroable trait)
398-
$traits = collect($reflection->getTraitNames());
399-
if ($traits->contains('Illuminate\Support\Traits\Macroable') || $class === EloquentBuilder::class) {
401+
if ($this->isMacroable || $class === EloquentBuilder::class) {
400402
$properties = $reflection->getStaticProperties();
401403
$macros = isset($properties['macros']) ? $properties['macros'] : [];
402404
foreach ($macros as $macro_name => $macro_func) {

src/Generator.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Generator
3535
protected $magic = [];
3636
protected $interfaces = [];
3737
protected $helpers;
38+
protected array $macroableTraits = [];
3839

3940
/**
4041
* @param \Illuminate\Config\Repository $config
@@ -341,7 +342,7 @@ protected function addMacroableClasses(Collection $aliases)
341342
continue;
342343
}
343344

344-
$aliases[] = new Alias($this->config, $class, $class, [], $this->interfaces);
345+
$aliases[] = new Alias($this->config, $class, $class, [], $this->interfaces, true);
345346
}
346347
}
347348

@@ -363,8 +364,18 @@ protected function getMacroableClasses(Collection $aliases)
363364
->filter(function ($class) {
364365
$traits = class_uses_recursive($class);
365366

366-
// Filter only classes with the macroable trait
367-
return isset($traits[Macroable::class]);
367+
if (isset($traits[Macroable::class])) {
368+
return true;
369+
}
370+
371+
// Filter only classes with a macroable trait
372+
foreach ($this->config->get('ide-helper.macroable_traits', []) as $trait) {
373+
if (isset($traits[$trait])) {
374+
return true;
375+
}
376+
}
377+
378+
return false;
368379
})
369380
->filter(function ($class) use ($aliases) {
370381
$class = Str::start($class, '\\');

tests/AliasTest.php

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,6 @@
1616
*/
1717
class AliasTest extends TestCase
1818
{
19-
/**
20-
* @covers ::detectMethods
21-
*/
22-
public function testDetectMethodsMacroableMacros(): void
23-
{
24-
// Mock
25-
$macro = __FUNCTION__;
26-
$alias = new AliasMock();
27-
28-
// Macros
29-
Builder::macro(
30-
$macro,
31-
function () {
32-
// empty
33-
}
34-
);
35-
36-
// Prepare
37-
$alias->setClasses([Builder::class]);
38-
$alias->detectMethods();
39-
40-
// Test
41-
$this->assertNotNull($this->getAliasMacro($alias, Builder::class, $macro));
42-
}
43-
4419
/**
4520
* @covers ::detectMethods
4621
*/
@@ -50,7 +25,7 @@ public function testDetectMethodsEloquentBuilderMacros(): void
5025
$macro = __FUNCTION__;
5126
$alias = new AliasMock();
5227

53-
// Macros
28+
// Macrosx
5429
EloquentBuilder::macro(
5530
$macro,
5631
function () {

0 commit comments

Comments
 (0)