container = $di; } // private ILogger $logger; protected function redirect(string $url, int $status = 302): RedirectResponse { return new RedirectResponse($url, $status); } protected function redirectToRoute(string $route, array $parameters = [], int $status = 302): RedirectResponse { return $this->redirect($this->generateUrl($route, $parameters), $status); } protected function renderView(string $view, array $parameters = []): string { return $this->doRenderView($view, null, $parameters, __FUNCTION__); } protected function render(string $view, array $parameters = [], Response $response = null): Response { return $this->doRender($view, null, $parameters, $response, __FUNCTION__); } /** * Renders a view. * * If an invalid form is found in the list of parameters, a 422 status code is returned. * Forms found in parameters are auto-cast to form views. */ protected function render(string $view, array $parameters = [], Response $response = null): Response { $content = $this->renderView($view, $parameters); $response ??= new Response(); if (200 === $response->getStatusCode()) { foreach ($parameters as $v) { if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) { $response->setStatusCode(422); break; } } } $response->setContent($content); return $response; } private function doRenderView(string $view, ?string $block, array $parameters, string $method): string { if (!$this->container->has('twig')) { throw new \LogicException(sprintf('You cannot use the "%s" method if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".', $method)); } foreach ($parameters as $k => $v) { if ($v instanceof FormInterface) { $parameters[$k] = $v->createView(); } } if (null !== $block) { return $this->container->get('twig')->load($view)->renderBlock($block, $parameters); } } private function doRender(string $view, ?string $block, array $parameters, ?Response $response, string $method): Response { $content = $this->doRenderView($view, $block, $parameters, $method); $response ??= new Response(); if (200 === $response->getStatusCode()) { foreach ($parameters as $v) { if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) { $response->setStatusCode(422); break; } } } $response->setContent($content); return $response; } /** * Creates and returns a Form instance from the type of the form. */ protected function createForm(string $type, mixed $data = null, array $options = []): FormInterface { return $this->container->get('form.factory')->create($type, $data, $options); } /** * Creates and returns a form builder instance. */ protected function createFormBuilder(mixed $data = null, array $options = []): FormBuilderInterface { return $this->container->get('form.factory')->createBuilder(FormType::class, $data, $options); } abstract public function index(); public function __call($method, $parameters) { throw new BadMethodCallException(sprintf( 'Method %s::%s does not exist.', static::class, $method )); } /** * Execute an action on the controller. * * @param string $method * @param array $parameters */ public function callAction($method, $parameters): Response { return $this->{$method}(...array_values($parameters)); } } ?>