/** * @param GetResponseForControllerResultEvent $event */ public function postController(GetResponseForControllerResultEvent $event) { if ($this->viewModelService->getViewModel()->getTemplate() === null) { return; } if (is_array($event->getControllerResult())) { $this->viewModelService->set($event->getControllerResult()); } if (!$event->hasResponse()) { $event->setResponse($this->viewModelService->render()); } }
/** * @param FilterControllerEvent $event * * @throws \Exception */ public function onKernelController(FilterControllerEvent $event) { if (!is_array($controller = $event->getController())) { return; } $object = new \ReflectionObject($controller[0]); $method = $object->getMethod($controller[1]); foreach ($this->reader->getMethodAnnotations($method) as $configuration) { if ($configuration instanceof ViewModel) { if ($configuration->hasClass()) { $class = $configuration->getClass(); $viewModel = new $class($this->templating); } elseif ($configuration->hasService()) { $viewModel = $this->getService($configuration->getService()); } else { throw new \Exception("Invalid View Model configuration."); } if (!$viewModel instanceof ViewModelInterface) { throw new \Exception("View model passed does not implement " . "Aequasi\\Bundle\\ViewModelBundle\\View\\Model\\ViewModelInterface"); } $this->viewModelService->setViewModel($viewModel); } if ($configuration instanceof ViewModelFactory) { $factory = $this->getFactory($configuration->getFactory()); if (!$factory instanceof ViewModelFactoryInterface) { throw new \Exception("View model passed does not implement " . "Aequasi\\Bundle\\ViewModelBundle\\View\\Model\\ViewModelInterface"); } $viewModel = $factory->create($configuration->getArguments()); if (!$viewModel instanceof ViewModelInterface) { throw new \Exception("View model passed does not implement " . "Aequasi\\Bundle\\ViewModelBundle\\View\\Model\\ViewModelInterface"); } $this->viewModelService->setViewModel($viewModel); } } }