From e716638ec3b2209e82fc730ed8dde2534db3f620 Mon Sep 17 00:00:00 2001 From: Romain Monteil Date: Thu, 27 Apr 2023 12:40:28 +0200 Subject: [PATCH 1/2] Fix incorrect classes assignation and controller rendering --- src/Dto/StimulusControllersDto.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Dto/StimulusControllersDto.php b/src/Dto/StimulusControllersDto.php index 973aa19a..a842f44f 100644 --- a/src/Dto/StimulusControllersDto.php +++ b/src/Dto/StimulusControllersDto.php @@ -36,7 +36,7 @@ public function addController(string $controllerName, array $controllerValues = foreach ($controllerClasses as $key => $class) { $key = $this->escapeAsHtmlAttr($this->normalizeKeyName($key)); - $this->values['data-'.$controllerName.'-'.$key.'-class'] = $class; + $this->classes['data-'.$controllerName.'-'.$key.'-class'] = $class; } } @@ -46,15 +46,11 @@ public function __toString(): string return ''; } - return rtrim( - 'data-controller="'.implode(' ', $this->controllers).'" '. - implode(' ', array_map(function (string $attribute, string $value): string { - return $attribute.'="'.$this->escapeAsHtmlAttr($value).'"'; - }, array_keys($this->values), $this->values)).' '. - implode(' ', array_map(function (string $attribute, string $value): string { - return $attribute.'="'.$this->escapeAsHtmlAttr($value).'"'; - }, array_keys($this->classes), $this->classes)) - ); + return rtrim(implode(' ', array_filter([ + 'data-controller="'.implode(' ', $this->controllers).'"', + $this->formatDataAttribute($this->values), + $this->formatDataAttribute($this->classes), + ]))); } public function toArray(): array @@ -82,4 +78,11 @@ private function normalizeKeyName(string $str): string // Adapted from ByteString::snake return strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1-\2', $str)); } + + private function formatDataAttribute(array $data): string + { + return implode(' ', array_map(function (string $attribute, string $value): string { + return $attribute.'="'.$this->escapeAsHtmlAttr($value).'"'; + }, array_keys($data), $data)); + } } From 49f93022d5fce599814d264b95790a7f0d167af4 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Sun, 28 May 2023 20:07:04 -0400 Subject: [PATCH 2/2] Increasing priority of pass to remove before Twig tries to use it --- src/WebpackEncoreBundle.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/WebpackEncoreBundle.php b/src/WebpackEncoreBundle.php index 3a6be80a..1efa5c45 100644 --- a/src/WebpackEncoreBundle.php +++ b/src/WebpackEncoreBundle.php @@ -9,6 +9,7 @@ namespace Symfony\WebpackEncoreBundle; +use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\WebpackEncoreBundle\DependencyInjection\Compiler\RemoveStimulusServicesPass; @@ -17,6 +18,7 @@ final class WebpackEncoreBundle extends Bundle { public function build(ContainerBuilder $container) { - $container->addCompilerPass(new RemoveStimulusServicesPass()); + // run before TwigEnvironmentPass to remove the twig extension before it's used + $container->addCompilerPass(new RemoveStimulusServicesPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10); } }