mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 01:57:13 +00:00
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]>
This commit is contained in:
co-authored by
Barry vd. Heuvel
parent
beda399e7e
commit
05d9c3cd65
@@ -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
|
### 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
|
If you don't want to write your properties yourself, you can use the command `php artisan ide-helper:models` to generate
|
||||||
|
|||||||
@@ -334,4 +334,18 @@ return [
|
|||||||
// 'ide-helper:models --nowrite',
|
// '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,
|
||||||
|
],
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|||||||
+5
-3
@@ -46,6 +46,8 @@ class Alias
|
|||||||
protected $phpdoc = null;
|
protected $phpdoc = null;
|
||||||
protected $classAliases = [];
|
protected $classAliases = [];
|
||||||
|
|
||||||
|
protected $isMacroable = false;
|
||||||
|
|
||||||
/** @var ConfigRepository */
|
/** @var ConfigRepository */
|
||||||
protected $config;
|
protected $config;
|
||||||
|
|
||||||
@@ -60,12 +62,13 @@ class Alias
|
|||||||
* @param array $magicMethods
|
* @param array $magicMethods
|
||||||
* @param array $interfaces
|
* @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->alias = $alias;
|
||||||
$this->magicMethods = $magicMethods;
|
$this->magicMethods = $magicMethods;
|
||||||
$this->interfaces = $interfaces;
|
$this->interfaces = $interfaces;
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
|
$this->isMacroable = $isMacroable;
|
||||||
|
|
||||||
// Make the class absolute
|
// Make the class absolute
|
||||||
$facade = '\\' . ltrim($facade, '\\');
|
$facade = '\\' . ltrim($facade, '\\');
|
||||||
@@ -395,8 +398,7 @@ class Alias
|
|||||||
|
|
||||||
// Check if the class is macroable
|
// Check if the class is macroable
|
||||||
// (Eloquent\Builder is also macroable but doesn't use Macroable trait)
|
// (Eloquent\Builder is also macroable but doesn't use Macroable trait)
|
||||||
$traits = collect($reflection->getTraitNames());
|
if ($this->isMacroable || $class === EloquentBuilder::class) {
|
||||||
if ($traits->contains('Illuminate\Support\Traits\Macroable') || $class === EloquentBuilder::class) {
|
|
||||||
$properties = $reflection->getStaticProperties();
|
$properties = $reflection->getStaticProperties();
|
||||||
$macros = isset($properties['macros']) ? $properties['macros'] : [];
|
$macros = isset($properties['macros']) ? $properties['macros'] : [];
|
||||||
foreach ($macros as $macro_name => $macro_func) {
|
foreach ($macros as $macro_name => $macro_func) {
|
||||||
|
|||||||
+14
-3
@@ -35,6 +35,7 @@ class Generator
|
|||||||
protected $magic = [];
|
protected $magic = [];
|
||||||
protected $interfaces = [];
|
protected $interfaces = [];
|
||||||
protected $helpers;
|
protected $helpers;
|
||||||
|
protected array $macroableTraits = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Illuminate\Config\Repository $config
|
* @param \Illuminate\Config\Repository $config
|
||||||
@@ -341,7 +342,7 @@ class Generator
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$aliases[] = new Alias($this->config, $class, $class, [], $this->interfaces);
|
$aliases[] = new Alias($this->config, $class, $class, [], $this->interfaces, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -363,8 +364,18 @@ class Generator
|
|||||||
->filter(function ($class) {
|
->filter(function ($class) {
|
||||||
$traits = class_uses_recursive($class);
|
$traits = class_uses_recursive($class);
|
||||||
|
|
||||||
// Filter only classes with the macroable trait
|
if (isset($traits[Macroable::class])) {
|
||||||
return 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) {
|
->filter(function ($class) use ($aliases) {
|
||||||
$class = Str::start($class, '\\');
|
$class = Str::start($class, '\\');
|
||||||
|
|||||||
+1
-26
@@ -16,31 +16,6 @@ use Illuminate\Support\Arr;
|
|||||||
*/
|
*/
|
||||||
class AliasTest extends TestCase
|
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
|
* @covers ::detectMethods
|
||||||
*/
|
*/
|
||||||
@@ -50,7 +25,7 @@ class AliasTest extends TestCase
|
|||||||
$macro = __FUNCTION__;
|
$macro = __FUNCTION__;
|
||||||
$alias = new AliasMock();
|
$alias = new AliasMock();
|
||||||
|
|
||||||
// Macros
|
// Macrosx
|
||||||
EloquentBuilder::macro(
|
EloquentBuilder::macro(
|
||||||
$macro,
|
$macro,
|
||||||
function () {
|
function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user