From 5bd58dde61644aa8c79669b82b45d14ad419646c Mon Sep 17 00:00:00 2001 From: 19Gerhard85 Date: Wed, 29 Jan 2025 11:33:11 +0100 Subject: [PATCH] AttributeBuilder --- .../src/Builder/StimulusAttributeBuilder.php | 155 +++++++++++++++++ .../src/Form/Extension/FormTypeExtension.php | 162 ------------------ .../src/Helper/StimulusSyntaxHelper.php | 40 +++++ 3 files changed, 195 insertions(+), 162 deletions(-) create mode 100644 src/StimulusBundle/src/Builder/StimulusAttributeBuilder.php delete mode 100644 src/StimulusBundle/src/Form/Extension/FormTypeExtension.php create mode 100644 src/StimulusBundle/src/Helper/StimulusSyntaxHelper.php diff --git a/src/StimulusBundle/src/Builder/StimulusAttributeBuilder.php b/src/StimulusBundle/src/Builder/StimulusAttributeBuilder.php new file mode 100644 index 00000000000..d25207179b7 --- /dev/null +++ b/src/StimulusBundle/src/Builder/StimulusAttributeBuilder.php @@ -0,0 +1,155 @@ +controllerName = $controllerName; + + return $builder; + } + + public function action(string $actionName, ?string $eventName = null, array $parameters = []): self + { + $this->actions[] = [ + 'actionName' => $actionName, + 'eventName' => $eventName, + 'parameters' => $parameters, + ]; + + return $this; + } + + public function value(string $key, mixed $value): self + { + $this->values[$key] = $value; + + return $this; + } + + public function class(string $key, mixed $value): self + { + $this->classes[$key] = $value; + + return $this; + } + + public function target(string $key, mixed $value): self + { + $this->targets[$key] = $value; + + return $this; + } + + public function outlet(string $identifier, string $outlet, mixed $selector): self + { + $this->outlets[] = [ + 'identifier' => $identifier, + 'outlet' => $outlet, + 'selector' => $selector, + ]; + + return $this; + } + + public function build(): array + { + $syntaxHelper = new StimulusSyntaxHelper(); + $controllerName = $syntaxHelper->normalizeControllerName($this->controllerName); + + $this->attributes['data-controller'] = $controllerName; + + //Actions + $this->attributes = array_merge( + $this->attributes, + ... + array_map(function (array $actionData) use ($controllerName): array { + $actionName = htmlspecialchars($actionData['actionName']); + $eventName = $actionData['eventName']; + + $action = sprintf('%s#%s', $controllerName, $actionName); + if (null !== $eventName) { + $action = sprintf('%s->%s', htmlspecialchars($eventName), $action); + } + + return ['data-action' => $action]; + }, $this->actions) + ); + + //Action Parameters + $this->attributes = array_merge( + $this->attributes, + ... + array_map( + function (array $actionData) use ($controllerName, $syntaxHelper): array { + $parameters = []; + + foreach ($actionData['parameters'] as $name => $value) { + $key = $syntaxHelper->normalizeKeyName($name); + $value = $syntaxHelper->getFormattedValue($value); + + $parameters[sprintf( + 'data-%s-%s-param', + $controllerName, + $key + )] = htmlspecialchars($value); + } + + return $parameters; + }, + $this->actions + ) + ); + + //Values + foreach ($this->values as $key => $value) { + if (null === $value) { + continue; + } + + $key = $syntaxHelper->normalizeKeyName($key); + $value = $syntaxHelper->getFormattedValue($value); + + $this->attributes[sprintf('data-%s-%s-value', $controllerName, $key)] = $value; + } + + //Classes + foreach ($this->classes as $key => $class) { + $key = $syntaxHelper->normalizeKeyName($key); + + $this->attributes[sprintf('data-%s-%s-class', $controllerName, $key)] = $class; + } + + //Outlets + foreach ($this->outlets as $outletItem) { + $identifier = $syntaxHelper->normalizeControllerName($outletItem['identifier']); + $outlet = $syntaxHelper->normalizeKeyName($outletItem['outlet']); + + $this->attributes[sprintf('data-%s-%s-outlet', $identifier, $outlet)] = htmlspecialchars( + $outletItem['selector'] + ); + } + + //Targets + foreach ($this->targets as $key => $target) { + $key = $syntaxHelper->normalizeKeyName($key); + + $this->attributes[sprintf('data-%s-target', $key)] = htmlspecialchars($target); + } + + return $this->attributes; + } +} diff --git a/src/StimulusBundle/src/Form/Extension/FormTypeExtension.php b/src/StimulusBundle/src/Form/Extension/FormTypeExtension.php deleted file mode 100644 index 00e9579160e..00000000000 --- a/src/StimulusBundle/src/Form/Extension/FormTypeExtension.php +++ /dev/null @@ -1,162 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace App\Form\Extension; - -use Symfony\Component\Form\AbstractTypeExtension; -use Symfony\Component\Form\Extension\Core\Type\FormType; -use Symfony\Component\Form\FormInterface; -use Symfony\Component\Form\FormView; -use Symfony\Component\OptionsResolver\OptionsResolver; -use Symfony\UX\StimulusBundle\Dto\StimulusAttributes; -use Twig\Environment; -use Twig\Loader\ArrayLoader; - -class FormTypeExtension extends AbstractTypeExtension -{ - private StimulusAttributes $stimulusAttributes; - - public static function getExtendedTypes(): iterable - { - return [FormType::class]; - } - - public function buildView(FormView $view, FormInterface $form, array $options): void - { - if ( - isset($options['stimulus_controller']) - || !isset($options['stimulus_target']) - || !isset($options['stimulus_action']) - ) { - $this->stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader())); - - if (isset($options['stimulus_controller'])) { - $this->handleController($options['stimulus_controller']); - } - - if (isset($options['stimulus_target'])) { - $this->handleTarget($options['stimulus_target']); - } - - if (isset($options['stimulus_action'])) { - $this->handleAction($options['stimulus_action']); - } - - $attributes = array_merge($view->vars['attr'], $this->stimulusAttributes->toArray()); - $view->vars['attr'] = $attributes; - } - - foreach (['row_attr', 'choice_attr'] as $index) { - if ( - isset($options[$index]) - && ( - isset($options[$index]['stimulus_controller']) - || isset($options[$index]['stimulus_target']) - || isset($options[$index]['stimulus_action']) - ) - ) { - $this->stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader())); - - if (isset($options[$index]['stimulus_controller'])) { - $this->handleController($options[$index]['stimulus_controller']); - unset($options[$index]['stimulus_controller']); - } - - if (isset($options[$index]['stimulus_target'])) { - $this->handleTarget($options[$index]['stimulus_target']); - unset($options[$index]['stimulus_target']); - } - - if (isset($options[$index]['stimulus_action'])) { - $this->handleAction($options[$index]['stimulus_action']); - unset($options[$index]['stimulus_action']); - } - - $attributes = array_merge($options[$index], $this->stimulusAttributes->toArray()); - $view->vars[$index] = $attributes; - } - } - } - - private function handleController(string|array $controllers): void - { - if (\is_string($controllers)) { - $controllers = [$controllers]; - } - - foreach ($controllers as $controllerName => $controller) { - if (\is_string($controller)) { // 'stimulus_controller' => ['controllerName1', 'controllerName2'] - $this->stimulusAttributes->addController($controller); - } elseif (\is_array($controller)) { // 'stimulus_controller' => ['controllerName' => ['values' => ['key' => 'value'], 'classes' => ['key' => 'value'], 'targets' => ['otherControllerName' => '.targetName']]] - $this->stimulusAttributes->addController((string) $controllerName, $controller['values'] ?? [], $controller['classes'] ?? [], $controller['outlets'] ?? []); - } - } - } - - private function handleTarget(array $targets): void - { - foreach ($targets as $controllerName => $target) { - $this->stimulusAttributes->addTarget($controllerName, \is_array($target) ? implode(' ', $target) : $target); - } - } - - private function handleAction(string|array $actions): void - { - // 'stimulus_action' => 'controllerName#actionName' - // 'stimulus_action' => 'eventName->controllerName#actionName' - if (\is_string($actions) && str_contains($actions, '#')) { - $eventName = null; - - if (str_contains($actions, '->')) { - [$eventName, $rest] = explode('->', $actions, 2); - } else { - $rest = $actions; - } - - [$controllerName, $actionName] = explode('#', $rest, 2); - - $this->stimulusAttributes->addAction($controllerName, $actionName, $eventName); - - return; - } - - foreach ($actions as $controllerName => $action) { - if (\is_string($action)) { // 'stimulus_action' => ['controllerName' => 'actionName'] - $this->stimulusAttributes->addAction($controllerName, $action); - } elseif (\is_array($action)) { - foreach ($action as $eventName => $actionName) { - if (\is_string($actionName)) { // 'stimulus_action' => ['controllerName' => ['eventName' => 'actionName']] - $this->stimulusAttributes->addAction($controllerName, $actionName, $eventName); - } elseif (\is_array($actionName)) { // 'stimulus_action' => ['controllerName' => ['eventName' => ['actionName' => ['key' => 'value']]]] - foreach ($actionName as $index => $params) { - $this->stimulusAttributes->addAction($controllerName, $index, $eventName, $params); - } - } - } - } - } - } - - public function configureOptions(OptionsResolver $resolver): void - { - parent::configureOptions($resolver); - - $resolver->setDefaults([ - 'stimulus_action' => null, - 'stimulus_controller' => null, - 'stimulus_target' => null, - ]); - - $resolver->setAllowedTypes('stimulus_action', ['string', 'array', 'callable', 'null']); - $resolver->setAllowedTypes('stimulus_controller', ['string', 'array', 'callable', 'null']); - $resolver->setAllowedTypes('stimulus_target', ['string', 'array', 'callable', 'null']); - } -} diff --git a/src/StimulusBundle/src/Helper/StimulusSyntaxHelper.php b/src/StimulusBundle/src/Helper/StimulusSyntaxHelper.php new file mode 100644 index 00000000000..2ea9c0b25d2 --- /dev/null +++ b/src/StimulusBundle/src/Helper/StimulusSyntaxHelper.php @@ -0,0 +1,40 @@ +