Skip to content

Commit f184d15

Browse files
committed
Cache and reuse ignore anonymous component constructor parameter names
1 parent 34d19a1 commit f184d15

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/Illuminate/View/AnonymousComponent.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Illuminate\View;
44

5+
use ReflectionClass;
6+
57
class AnonymousComponent extends Component
68
{
79
/**
@@ -18,6 +20,26 @@ class AnonymousComponent extends Component
1820
*/
1921
protected $data = [];
2022

23+
protected static array $ignoredParameterNames = [];
24+
25+
/**
26+
* Fetch a cached set of anonymous component constructor parameter names to exclude.
27+
*/
28+
public static function ignoredParameterNames(): array
29+
{
30+
if (!isset(static::$ignoredParameterNames)) {
31+
$constructor = (new ReflectionClass(
32+
static::class
33+
))->getConstructor();
34+
35+
static::$ignoredParameterNames = collect($constructor->getParameters())
36+
->map->getName()
37+
->all();
38+
}
39+
40+
return static::$ignoredParameterNames;
41+
}
42+
2143
/**
2244
* Create a new anonymous component instance.
2345
*

src/Illuminate/View/Compilers/ComponentTagCompiler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ protected function componentString(string $component, array $attributes)
259259
}
260260

261261
return "##BEGIN-COMPONENT-CLASS##@component('{$class}', '{$component}', [".$this->attributesToString($parameters, $escapeBound = false).'])
262-
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag && $constructor = (new ReflectionClass('.$class.'::class))->getConstructor()): ?>
263-
<?php $attributes = $attributes->except(collect($constructor->getParameters())->map->getName()->all()); ?>
262+
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
263+
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
264264
<?php endif; ?>
265265
<?php $component->withAttributes(['.$this->attributesToString($attributes->all(), $escapeAttributes = $class !== DynamicComponent::class).']); ?>';
266266
}

0 commit comments

Comments
 (0)