vendor/symfony/webpack-encore-bundle/src/Twig/StimulusTwigExtension.php line 163

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony WebpackEncoreBundle package.
  4. * (c) Fabien Potencier <fabien@symfony.com>
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace Symfony\WebpackEncoreBundle\Twig;
  9. use Symfony\WebpackEncoreBundle\Dto\StimulusActionsDto;
  10. use Symfony\WebpackEncoreBundle\Dto\StimulusControllersDto;
  11. use Symfony\WebpackEncoreBundle\Dto\StimulusTargetsDto;
  12. use Twig\Environment;
  13. use Twig\Extension\AbstractExtension;
  14. use Twig\TwigFilter;
  15. use Twig\TwigFunction;
  16. /**
  17. * @deprecated since 1.17.0 - install symfony/stimulus-bundle instead.
  18. */
  19. final class StimulusTwigExtension extends AbstractExtension
  20. {
  21. public function getFunctions(): array
  22. {
  23. return [
  24. new TwigFunction('stimulus_controller', [$this, 'renderStimulusController'], ['needs_environment' => true, 'is_safe' => ['html_attr']]),
  25. new TwigFunction('stimulus_action', [$this, 'renderStimulusAction'], ['needs_environment' => true, 'is_safe' => ['html_attr']]),
  26. new TwigFunction('stimulus_target', [$this, 'renderStimulusTarget'], ['needs_environment' => true, 'is_safe' => ['html_attr']]),
  27. ];
  28. }
  29. public function getFilters(): array
  30. {
  31. return [
  32. new TwigFilter('stimulus_controller', [$this, 'appendStimulusController'], ['is_safe' => ['html_attr']]),
  33. new TwigFilter('stimulus_action', [$this, 'appendStimulusAction'], ['is_safe' => ['html_attr']]),
  34. new TwigFilter('stimulus_target', [$this, 'appendStimulusTarget'], ['is_safe' => ['html_attr']]),
  35. ];
  36. }
  37. /**
  38. * @param string $controllerName the Stimulus controller name
  39. * @param array $controllerValues array of controller values
  40. * @param array $controllerClasses array of controller CSS classes
  41. */
  42. public function renderStimulusController(Environment $env, $controllerName, array $controllerValues = [], array $controllerClasses = []): StimulusControllersDto
  43. {
  44. $dto = new StimulusControllersDto($env);
  45. if (\is_array($controllerName)) {
  46. trigger_deprecation('symfony/webpack-encore-bundle', 'v1.15.0', 'Passing an array as first argument of stimulus_controller() is deprecated.');
  47. if ($controllerValues || $controllerClasses) {
  48. throw new \InvalidArgumentException('You cannot pass an array to the first and second/third argument of stimulus_controller(): check the documentation.');
  49. }
  50. $data = $controllerName;
  51. foreach ($data as $controllerName => $controllerValues) {
  52. $dto->addController($controllerName, $controllerValues);
  53. }
  54. return $dto;
  55. }
  56. $dto->addController($controllerName, $controllerValues, $controllerClasses);
  57. return $dto;
  58. }
  59. /**
  60. * @param array $parameters Parameters to pass to the action. Optional.
  61. */
  62. public function renderStimulusAction(Environment $env, $controllerName, string $actionName = null, string $eventName = null, array $parameters = []): StimulusActionsDto
  63. {
  64. $dto = new StimulusActionsDto($env);
  65. if (\is_array($controllerName)) {
  66. trigger_deprecation('symfony/webpack-encore-bundle', 'v1.15.0', 'Passing an array as first argument of stimulus_action() is deprecated.');
  67. if ($actionName || $eventName || $parameters) {
  68. throw new \InvalidArgumentException('You cannot pass a string to the second or third argument nor an array to the fourth argument while passing an array to the first argument of stimulus_action(): check the documentation.');
  69. }
  70. $data = $controllerName;
  71. foreach ($data as $controllerName => $controllerActions) {
  72. if (\is_string($controllerActions)) {
  73. $controllerActions = [[$controllerActions]];
  74. }
  75. foreach ($controllerActions as $possibleEventName => $controllerAction) {
  76. if (\is_string($possibleEventName) && \is_string($controllerAction)) {
  77. $controllerAction = [$possibleEventName => $controllerAction];
  78. } elseif (\is_string($controllerAction)) {
  79. $controllerAction = [$controllerAction];
  80. }
  81. foreach ($controllerAction as $eventName => $actionName) {
  82. $dto->addAction($controllerName, $actionName, \is_string($eventName) ? $eventName : null);
  83. }
  84. }
  85. }
  86. return $dto;
  87. }
  88. $dto->addAction($controllerName, $actionName, $eventName, $parameters);
  89. return $dto;
  90. }
  91. public function appendStimulusController(StimulusControllersDto $dto, string $controllerName, array $controllerValues = [], array $controllerClasses = []): StimulusControllersDto
  92. {
  93. $dto->addController($controllerName, $controllerValues, $controllerClasses);
  94. return $dto;
  95. }
  96. /**
  97. * @param array $parameters Parameters to pass to the action. Optional.
  98. */
  99. public function appendStimulusAction(StimulusActionsDto $dto, string $controllerName, string $actionName, string $eventName = null, array $parameters = []): StimulusActionsDto
  100. {
  101. $dto->addAction($controllerName, $actionName, $eventName, $parameters);
  102. return $dto;
  103. }
  104. /**
  105. * @param string $controllerName the Stimulus controller name
  106. * @param string|null $targetNames The space-separated list of target names if a string is passed to the 1st argument. Optional.
  107. */
  108. public function renderStimulusTarget(Environment $env, $controllerName, string $targetNames = null): StimulusTargetsDto
  109. {
  110. $dto = new StimulusTargetsDto($env);
  111. if (\is_array($controllerName)) {
  112. trigger_deprecation('symfony/webpack-encore-bundle', 'v1.15.0', 'Passing an array as first argument of stimulus_target() is deprecated.');
  113. if ($targetNames) {
  114. throw new \InvalidArgumentException('You cannot pass a string to the second argument while passing an array to the first argument of stimulus_target(): check the documentation.');
  115. }
  116. $data = $controllerName;
  117. foreach ($data as $controllerName => $targetNames) {
  118. $dto->addTarget($controllerName, $targetNames);
  119. }
  120. return $dto;
  121. }
  122. $dto->addTarget($controllerName, $targetNames);
  123. return $dto;
  124. }
  125. /**
  126. * @param string $controllerName the Stimulus controller name
  127. * @param string|null $targetNames The space-separated list of target names if a string is passed to the 1st argument. Optional.
  128. */
  129. public function appendStimulusTarget(StimulusTargetsDto $dto, string $controllerName, string $targetNames = null): StimulusTargetsDto
  130. {
  131. $dto->addTarget($controllerName, $targetNames);
  132. return $dto;
  133. }
  134. }