Skip to content

Commit

Permalink
Cache and reuse ignore anonymous component constructor parameter names
Browse files Browse the repository at this point in the history
  • Loading branch information
assertchris committed Apr 19, 2024
1 parent 3f7a0e3 commit 5f4d7cd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/Illuminate/View/AnonymousComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\View;

use ReflectionClass;

class AnonymousComponent extends Component
{
/**
Expand All @@ -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.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/View/Compilers/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ protected function componentString(string $component, array $attributes)
}

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

0 comments on commit 5f4d7cd

Please sign in to comment.