From 67427e2f77b543d568dbb56b682b70d6a4349ef1 Mon Sep 17 00:00:00 2001 From: Christopher Pitt Date: Fri, 19 Apr 2024 23:54:34 +0200 Subject: [PATCH] Cache and reuse ignore anonymous component constructor parameter names --- src/Illuminate/View/AnonymousComponent.php | 22 +++++++++++++++++++ .../View/Compilers/ComponentTagCompiler.php | 4 ++-- 2 files changed, 24 insertions(+), 2 deletions(-) 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).']); ?>'; }