Skip to content
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

[StimulusBundle] Adds Form Extension #2450

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
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
162 changes: 162 additions & 0 deletions src/StimulusBundle/src/Form/Extension/FormTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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;
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 temporary right ?

I think we won't need it and use a new object but if we don't, you can inject the StimulusHelper here


public static function getExtendedTypes(): iterable
{
return [FormType::class];
}

public function buildView(FormView $view, FormInterface $form, array $options): void
{
if (
Comment on lines +32 to +34
Copy link
Member

Choose a reason for hiding this comment

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

In this method you repeat two times the same code, this can be reduce a lot i think

isset($options['stimulus_controller'])
|| !isset($options['stimulus_target'])
|| !isset($options['stimulus_action'])
) {
Comment on lines +34 to +38
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (
isset($options['stimulus_controller'])
|| !isset($options['stimulus_target'])
|| !isset($options['stimulus_action'])
) {
if ($options['stimulus_controller'] || $options['stimulus_target'] || $options['stimulus_action']) {

$this->stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader()));

if (isset($options['stimulus_controller'])) {
$this->handleController($options['stimulus_controller']);
}
Comment on lines +41 to +43
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (isset($options['stimulus_controller'])) {
$this->handleController($options['stimulus_controller']);
}
if ($options['stimulus_controller']) {
$this->handleController($options['stimulus_controller']);
}
// same for the other ones


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;
Comment on lines +53 to +54
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$attributes = array_merge($view->vars['attr'], $this->stimulusAttributes->toArray());
$view->vars['attr'] = $attributes;
$view->vars['attr'] = [...$view->vars['attr'], ...$this->stimulusAttributes->toArray()];

Avoid unnecessary var

}

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];
Copy link
Member

Choose a reason for hiding this comment

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

Could be handle outside this method i suppose (will call $this->stimulusAttributes->addController(...) every case)

}

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);
}
}
Comment on lines +104 to +109
Copy link
Member

Choose a reason for hiding this comment

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

Can be removed / inline when it is called


private function handleAction(string|array $actions): void
{
// 'stimulus_action' => 'controllerName#actionName'
// 'stimulus_action' => 'eventName->controllerName#actionName'
if (\is_string($actions) && str_contains($actions, '#')) {
Comment on lines +113 to +115
Copy link
Member

Choose a reason for hiding this comment

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

The reason i want another object, we should not have to duplicate this in multiple place. Will open a draft later if i can

Copy link
Member

Choose a reason for hiding this comment

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

(i cannot 😅 🕐 )

$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']);
}
}
Loading