diff --git a/src/Illuminate/View/AnonymousComponent.php b/src/Illuminate/View/AnonymousComponent.php index eba64365626b..db3e0b8ba122 100644 --- a/src/Illuminate/View/AnonymousComponent.php +++ b/src/Illuminate/View/AnonymousComponent.php @@ -2,6 +2,8 @@ namespace Illuminate\View; +use ReflectionClass; + class AnonymousComponent extends Component { /** @@ -18,6 +20,26 @@ class AnonymousComponent extends Component */ protected $data = []; + protected static array $ignoredParameterNames = []; + + /** + * Fetch a cached set of anonymous component constructor parameter names to exclude. + */ + public static function ignoredParameterNames(): array + { + if (!isset(static::$ignoredParameterNames)) { + $constructor = (new ReflectionClass( + static::class + ))->getConstructor(); + + static::$ignoredParameterNames = collect($constructor->getParameters()) + ->map->getName() + ->all(); + } + + return static::$ignoredParameterNames; + } + /** * Create a new anonymous component instance. * diff --git a/src/Illuminate/View/Compilers/ComponentTagCompiler.php b/src/Illuminate/View/Compilers/ComponentTagCompiler.php index 8b360762127e..bb095c8a0faa 100644 --- a/src/Illuminate/View/Compilers/ComponentTagCompiler.php +++ b/src/Illuminate/View/Compilers/ComponentTagCompiler.php @@ -259,8 +259,8 @@ protected function componentString(string $component, array $attributes) } return "##BEGIN-COMPONENT-CLASS##@component('{$class}', '{$component}', [".$this->attributesToString($parameters, $escapeBound = false).']) -getConstructor()): ?> -except(collect($constructor->getParameters())->map->getName()->all()); ?> + +except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?> withAttributes(['.$this->attributesToString($attributes->all(), $escapeAttributes = $class !== DynamicComponent::class).']); ?>'; }