package and depencies
This commit is contained in:
@@ -28,25 +28,34 @@ use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
*/
|
||||
final class ControllerArgumentsEvent extends KernelEvent
|
||||
{
|
||||
private $controller;
|
||||
private $arguments;
|
||||
private ControllerEvent $controllerEvent;
|
||||
private array $arguments;
|
||||
private array $namedArguments;
|
||||
|
||||
public function __construct(HttpKernelInterface $kernel, callable $controller, array $arguments, Request $request, ?int $requestType)
|
||||
public function __construct(HttpKernelInterface $kernel, callable|ControllerEvent $controller, array $arguments, Request $request, ?int $requestType)
|
||||
{
|
||||
parent::__construct($kernel, $request, $requestType);
|
||||
|
||||
$this->controller = $controller;
|
||||
if (!$controller instanceof ControllerEvent) {
|
||||
$controller = new ControllerEvent($kernel, $controller, $request, $requestType);
|
||||
}
|
||||
|
||||
$this->controllerEvent = $controller;
|
||||
$this->arguments = $arguments;
|
||||
}
|
||||
|
||||
public function getController(): callable
|
||||
{
|
||||
return $this->controller;
|
||||
return $this->controllerEvent->getController();
|
||||
}
|
||||
|
||||
public function setController(callable $controller)
|
||||
/**
|
||||
* @param array<class-string, list<object>>|null $attributes
|
||||
*/
|
||||
public function setController(callable $controller, array $attributes = null): void
|
||||
{
|
||||
$this->controller = $controller;
|
||||
$this->controllerEvent->setController($controller, $attributes);
|
||||
unset($this->namedArguments);
|
||||
}
|
||||
|
||||
public function getArguments(): array
|
||||
@@ -57,5 +66,38 @@ final class ControllerArgumentsEvent extends KernelEvent
|
||||
public function setArguments(array $arguments)
|
||||
{
|
||||
$this->arguments = $arguments;
|
||||
unset($this->namedArguments);
|
||||
}
|
||||
|
||||
public function getNamedArguments(): array
|
||||
{
|
||||
if (isset($this->namedArguments)) {
|
||||
return $this->namedArguments;
|
||||
}
|
||||
|
||||
$namedArguments = [];
|
||||
$arguments = $this->arguments;
|
||||
|
||||
foreach ($this->controllerEvent->getControllerReflector()->getParameters() as $i => $param) {
|
||||
if ($param->isVariadic()) {
|
||||
$namedArguments[$param->name] = \array_slice($arguments, $i);
|
||||
break;
|
||||
}
|
||||
if (\array_key_exists($i, $arguments)) {
|
||||
$namedArguments[$param->name] = $arguments[$i];
|
||||
} elseif ($param->isDefaultvalueAvailable()) {
|
||||
$namedArguments[$param->name] = $param->getDefaultValue();
|
||||
}
|
||||
}
|
||||
|
||||
return $this->namedArguments = $namedArguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string, list<object>>
|
||||
*/
|
||||
public function getAttributes(): array
|
||||
{
|
||||
return $this->controllerEvent->getAttributes();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user