Skip to content

Commit 2e29dca

Browse files
[TwigBridge] Add #[Template()] to describe how to render arrays returned by controllers
1 parent 3e116f6 commit 2e29dca

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

Event/ControllerArgumentsEvent.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ final class ControllerArgumentsEvent extends KernelEvent
3030
{
3131
private ControllerEvent $controllerEvent;
3232
private array $arguments;
33+
private array $namedArguments;
3334

3435
public function __construct(HttpKernelInterface $kernel, callable|ControllerEvent $controller, array $arguments, Request $request, ?int $requestType)
3536
{
@@ -54,6 +55,7 @@ public function getController(): callable
5455
public function setController(callable $controller, array $attributes = null): void
5556
{
5657
$this->controllerEvent->setController($controller, $attributes);
58+
unset($this->namedArguments);
5759
}
5860

5961
public function getArguments(): array
@@ -64,6 +66,32 @@ public function getArguments(): array
6466
public function setArguments(array $arguments)
6567
{
6668
$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+
$r = $this->getRequest()->attributes->get('_controller_reflectors')[1] ?? new \ReflectionFunction($this->controllerEvent->getController());
81+
82+
foreach ($r->getParameters() as $i => $param) {
83+
if ($param->isVariadic()) {
84+
$namedArguments[$param->name] = \array_slice($arguments, $i);
85+
break;
86+
}
87+
if (\array_key_exists($i, $arguments)) {
88+
$namedArguments[$param->name] = $arguments[$i];
89+
} elseif ($param->isDefaultvalueAvailable()) {
90+
$namedArguments[$param->name] = $param->getDefaultValue();
91+
}
92+
}
93+
94+
return $this->namedArguments = $namedArguments;
6795
}
6896

6997
/**

Event/ViewEvent.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
*/
2626
final class ViewEvent extends RequestEvent
2727
{
28+
public readonly ?ControllerArgumentsEvent $controllerArgumentsEvent;
2829
private mixed $controllerResult;
2930

30-
public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, mixed $controllerResult)
31+
public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, mixed $controllerResult, ControllerArgumentsEvent $controllerArgumentsEvent = null)
3132
{
3233
parent::__construct($kernel, $request, $requestType);
3334

3435
$this->controllerResult = $controllerResult;
36+
$this->controllerArgumentsEvent = $controllerArgumentsEvent;
3537
}
3638

3739
public function getControllerResult(): mixed

HttpKernel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,13 @@ private function handleRaw(Request $request, int $type = self::MAIN_REQUEST): Re
156156
$this->dispatcher->dispatch($event, KernelEvents::CONTROLLER_ARGUMENTS);
157157
$controller = $event->getController();
158158
$arguments = $event->getArguments();
159-
$request->attributes->remove('_controller_reflectors');
160159

161160
// call controller
162161
$response = $controller(...$arguments);
163162

164163
// view
165164
if (!$response instanceof Response) {
166-
$event = new ViewEvent($this, $request, $type, $response);
165+
$event = new ViewEvent($this, $request, $type, $response, $event);
167166
$this->dispatcher->dispatch($event, KernelEvents::VIEW);
168167

169168
if ($event->hasResponse()) {
@@ -179,6 +178,7 @@ private function handleRaw(Request $request, int $type = self::MAIN_REQUEST): Re
179178
throw new ControllerDoesNotReturnResponseException($msg, $controller, __FILE__, __LINE__ - 17);
180179
}
181180
}
181+
$request->attributes->remove('_controller_reflectors');
182182

183183
return $this->filterResponse($response, $request, $type);
184184
}

0 commit comments

Comments
 (0)