ControllerArgumentsEvent.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Event;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\HttpKernelInterface;
  13. /**
  14. * Allows filtering of controller arguments.
  15. *
  16. * You can call getController() to retrieve the controller and getArguments
  17. * to retrieve the current arguments. With setArguments() you can replace
  18. * arguments that are used to call the controller.
  19. *
  20. * Arguments set in the event must be compatible with the signature of the
  21. * controller.
  22. *
  23. * @author Christophe Coevoet <stof@notk.org>
  24. */
  25. final class ControllerArgumentsEvent extends KernelEvent
  26. {
  27. private ControllerEvent $controllerEvent;
  28. private array $namedArguments;
  29. public function __construct(
  30. HttpKernelInterface $kernel,
  31. callable|ControllerEvent $controller,
  32. private array $arguments,
  33. Request $request,
  34. ?int $requestType,
  35. ) {
  36. parent::__construct($kernel, $request, $requestType);
  37. if (!$controller instanceof ControllerEvent) {
  38. $controller = new ControllerEvent($kernel, $controller, $request, $requestType);
  39. }
  40. $this->controllerEvent = $controller;
  41. }
  42. public function getController(): callable
  43. {
  44. return $this->controllerEvent->getController();
  45. }
  46. /**
  47. * @param array<class-string, list<object>>|null $attributes
  48. */
  49. public function setController(callable $controller, ?array $attributes = null): void
  50. {
  51. $this->controllerEvent->setController($controller, $attributes);
  52. unset($this->namedArguments);
  53. }
  54. public function getArguments(): array
  55. {
  56. return $this->arguments;
  57. }
  58. public function setArguments(array $arguments): void
  59. {
  60. $this->arguments = $arguments;
  61. unset($this->namedArguments);
  62. }
  63. public function getNamedArguments(): array
  64. {
  65. if (isset($this->namedArguments)) {
  66. return $this->namedArguments;
  67. }
  68. $namedArguments = [];
  69. $arguments = $this->arguments;
  70. foreach ($this->controllerEvent->getControllerReflector()->getParameters() as $i => $param) {
  71. if ($param->isVariadic()) {
  72. $namedArguments[$param->name] = \array_slice($arguments, $i);
  73. break;
  74. }
  75. if (\array_key_exists($i, $arguments)) {
  76. $namedArguments[$param->name] = $arguments[$i];
  77. } elseif ($param->isDefaultvalueAvailable()) {
  78. $namedArguments[$param->name] = $param->getDefaultValue();
  79. }
  80. }
  81. return $this->namedArguments = $namedArguments;
  82. }
  83. /**
  84. * @template T of class-string|null
  85. *
  86. * @param T $className
  87. *
  88. * @return array<class-string, list<object>>|list<object>
  89. *
  90. * @psalm-return (T is null ? array<class-string, list<object>> : list<object>)
  91. */
  92. public function getAttributes(?string $className = null): array
  93. {
  94. return $this->controllerEvent->getAttributes($className);
  95. }
  96. }