Skip to content

[TwigComponent]: Add the ability to spread attributes from the ... attribute #2923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/TwigComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.26.0

- Add the ability to spread attributes from the `...` attribute

## 2.25.2

- Fix `ComponentAttributes` rendering when using `StimulusAttributes` as default attributes
Expand Down
33 changes: 32 additions & 1 deletion src/TwigComponent/src/ComponentAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ final class ComponentAttributes implements \Stringable, \IteratorAggregate, \Cou
private const ALPINE_REGEX = '#^x-([a-z]+):[^:]+$#';
private const VUE_REGEX = '#^v-([a-z]+):[^:]+$#';

private const SPREADABLE_ATTRIBUTE = '...';

/** @var array<string, string|bool> */
private array $attributes = [];

/** @var array<string,true> */
private array $rendered = [];

/**
* @param array<string, string|bool> $attributes
*/
public function __construct(
private array $attributes,
array $attributes,
private readonly EscaperRuntime $escaper,
) {
$this->attributes = $this->handleAttributes($attributes);
}

public function __toString(): string
Expand Down Expand Up @@ -103,6 +109,31 @@ public function __clone(): void
$this->rendered = [];
}

private function handleAttributes(array $attributes): array
{
$spreadAttributes = $attributes[self::SPREADABLE_ATTRIBUTE] ?? null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should be done in constructor to avoid method call for everyone not using this feature

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to break the code guideline but ok, if it's allowed


if (!$spreadAttributes) {
return $attributes;
}

if ($spreadAttributes instanceof StimulusAttributes) {
$spreadAttributes = $spreadAttributes->toArray();
}

if ($spreadAttributes instanceof \Traversable) {
$spreadAttributes = iterator_to_array($spreadAttributes);
}

if (!\is_array($spreadAttributes)) {
throw new \InvalidArgumentException(\sprintf('The "%s" attribute must be an array, "%s" given.', self::SPREADABLE_ATTRIBUTE, get_debug_type($spreadAttributes)));
}

unset($attributes[self::SPREADABLE_ATTRIBUTE]);

return [...$attributes, ...$spreadAttributes];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something that can be done userland, no ? Wether in PHP or Twig

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean ? The interest is to be recursive. Currently, for the "primary" attributes it's not that usefull but for nested attributes, we need that

}

public function render(string $attribute): ?string
{
if (null === $value = $this->attributes[$attribute] ?? null) {
Expand Down
6 changes: 0 additions & 6 deletions src/TwigComponent/src/ComponentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,6 @@ public function mountFromObject(object $component, array $data, ComponentMetadat
$attributes = $data[$attributesVar] ?? [];
unset($data[$attributesVar]);

foreach ($data as $key => $value) {
if ($value instanceof \Stringable) {
$data[$key] = (string) $value;
}
}
Comment on lines -116 to -120
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this removal ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this loop force attributes to be strings if they are Stringable. However, StimulusAttributes implements Stringable so I can't pass recursively the object in the ComponentAttributes. Maybe I can exclude StimulusAttributes like this :
if (!$value instanceof \StimulusAttributes && $value instanceof \Stringable) { ?


return new MountedComponent(
$componentMetadata->getName(),
$component,
Expand Down
Loading