ControllerArgumentsEvent.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 $arguments;
  29. private array $namedArguments;
  30. public function __construct(HttpKernelInterface $kernel, callable|ControllerEvent $controller, array $arguments, Request $request, ?int $requestType)
  31. {
  32. parent::__construct($kernel, $request, $requestType);
  33. if (!$controller instanceof ControllerEvent) {
  34. $controller = new ControllerEvent($kernel, $controller, $request, $requestType);
  35. }
  36. $this->controllerEvent = $controller;
  37. $this->arguments = $arguments;
  38. }
  39. public function getController(): callable
  40. {
  41. return $this->controllerEvent->getController();
  42. }
  43. /**
  44. * @param array<class-string, list<object>>|null $attributes
  45. */
  46. public function setController(callable $controller, ?array $attributes = null): void
  47. {
  48. $this->controllerEvent->setController($controller, $attributes);
  49. unset($this->namedArguments);
  50. }
  51. public function getArguments(): array
  52. {
  53. return $this->arguments;
  54. }
  55. public function setArguments(array $arguments): void
  56. {
  57. $this->arguments = $arguments;
  58. unset($this->namedArguments);
  59. }
  60. public function getNamedArguments(): array
  61. {
  62. if (isset($this->namedArguments)) {
  63. return $this->namedArguments;
  64. }
  65. $namedArguments = [];
  66. $arguments = $this->arguments;
  67. foreach ($this->controllerEvent->getControllerReflector()->getParameters() as $i => $param) {
  68. if ($param->isVariadic()) {
  69. $namedArguments[$param->name] = \array_slice($arguments, $i);
  70. break;
  71. }
  72. if (\array_key_exists($i, $arguments)) {
  73. $namedArguments[$param->name] = $arguments[$i];
  74. } elseif ($param->isDefaultvalueAvailable()) {
  75. $namedArguments[$param->name] = $param->getDefaultValue();
  76. }
  77. }
  78. return $this->namedArguments = $namedArguments;
  79. }
  80. /**
  81. * @template T of class-string|null
  82. *
  83. * @param T $className
  84. *
  85. * @return array<class-string, list<object>>|list<object>
  86. *
  87. * @psalm-return (T is null ? array<class-string, list<object>> : list<object>)
  88. */
  89. public function getAttributes(?string $className = null): array
  90. {
  91. return $this->controllerEvent->getAttributes($className);
  92. }
  93. }