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