/** * @return ControllerResponse * @throws Page404Exception * @throws RuntimeMvcException */ public function execute() { $class = $this->getControllerClass(); try { $reflectionClass = new \ReflectionClass($class); if ($reflectionClass->implementsInterface(ControllerInterface::class)) { /** @var ControllerInterface $controller */ $controller = $reflectionClass->newInstance(); $controller->setReflectionClass($reflectionClass); $this->getResponse()->setControllerInstance($controller); $controller->setDi($this->container); $reflectionMethod = new \ReflectionMethod($class, $this->getActionCamelize()); $controller->setReflectionAction($reflectionMethod); $controller->setNamespace($this->getNamespace()); $controller->setName($this->getController()); $controller->setAction($this->getAction()); $controller->setParams($this->getParams()); $controller->beforeExecute(); $this->response->setControllerContent($reflectionMethod->invokeArgs($controller, $this->getParams())); $controller->afterExecute(); $this->container->get('event')->dispatch(MvcEvent::ON_AFTER_RUN, new MvcEvent($controller)); } else { throw new RuntimeMvcException('Controller found but it should implemented interface [:name]', ['name' => ControllerInterface::class]); } } catch (\ReflectionException $exception) { $this->container->get('event')->dispatch(MvcEvent::ON_PAGE_404, new MvcEvent($this)); $this->container->get('event')->dispatch(MvcEvent::ON_ACTION_ERROR, new MvcEvent($exception)); throw new Page404Exception($exception->getMessage()); } catch (\Exception $exception) { $this->container->get('event')->dispatch(MvcEvent::ON_ACTION_ERROR, new MvcEvent($exception)); throw new RuntimeMvcException($exception->getMessage()); } return $this->response; }
/** * @param $name * @return mixed * @throws MvcException */ public function __get($name) { if (static::$container->has($name)) { return static::$container->get($name); } else { throw new MvcException("Service '{$name}' not found on default container"); } }