/** * Creates the view model. * * @param SystemEvent $event The system event */ public function __invoke(SystemEvent $event) { $result = $event->getResult(SystemEvent::DISPATCH); if (false !== $result && !$result instanceof ViewModelInterface && !$result instanceof ResponseInterface) { $model = new ViewModel($result); $event->setResult(SystemEvent::DISPATCH, $model); } }
/** * Injects the view model to Layout. * * @param \Es\System\SystemEvent $event The system event */ public function __invoke(SystemEvent $event) { $result = $event->getResult(SystemEvent::DISPATCH); if ($result instanceof ViewModelInterface) { $view = $this->getView(); $layout = $view->getLayout(); $layout->addChild($result); } }
/** * Sends a response to client. * * @param \Es\System\SystemEvent $event The system event * * @throws \UnexpectedValueException If the final result of a system event * is not PSR Response */ public function __invoke(SystemEvent $event) { $result = $event->getResult(SystemEvent::FINISH); if (!$result instanceof ResponseInterface) { throw new UnexpectedValueException(sprintf('The system event provided invalid final result; must be ' . 'an instance of "%s", "%s" received.', ResponseInterface::CLASS, is_object($result) ? get_class($result) : gettype($result))); } $server = $this->getServer(); $emitter = $server->getEmitter(); $emitter->emit($result); }
/** * Removes from the response of type "text/html" the blank lines. * * @param \Es\System\SystemEvent $event The system event */ public function __invoke(SystemEvent $event) { $result = $event->getResult(SystemEvent::FINISH); if ($result instanceof ResponseInterface) { $contentType = $result->getHeaderLine('Content-Type'); if (0 === strpos($contentType, 'text/html')) { $body = (string) $result->getBody(); $cleaned = preg_replace("#(\\s)*(\n)+(\r)*#", "\n", $body); $stream = Stream::make($cleaned); $event->setResult(SystemEvent::FINISH, $result->withBody($stream)); } } }
/** * Injects the template name to a view model. * * @param \Es\System\SystemEvent $event The system event * * @throws \RuntimeException If the module namespace is not specified * @throws \UnexpectedValueException If the context of system event is * not object */ public function __invoke(SystemEvent $event) { $result = $event->getResult(SystemEvent::DISPATCH); if ($result instanceof ViewModelInterface && !$result->getTemplate()) { $module = $result->getModule(); if (!$module) { throw new RuntimeException('Unable to resolve template for View Model. ' . 'The module namespace is not specified.'); } $context = $event->getContext(); if (!is_object($context)) { throw new UnexpectedValueException(sprintf('Invalid context of system event; must be an object, ' . '"%s" received.', gettype($context))); } $template = $this->resolveTemplate($context, $module); $result->setTemplate($template); } }
/** * Injects the module namespace of the dispatched controller to a view model * and to layout. * * @param \Es\System\SystemEvent $event The system event * * @throws \UnexpectedValueException If the context of system event is * not object */ public function __invoke(SystemEvent $event) { $context = $event->getContext(); if (!is_object($context)) { throw new UnexpectedValueException(sprintf('Invalid context of system event; must be an object, ' . '"%s" received.', gettype($context))); } $module = $this->resolveModule($context); $view = $this->getView(); $layout = $view->getLayout(); if (!$layout->getModule()) { $layout->setModule($module); } $result = $event->getResult(SystemEvent::DISPATCH); if ($result instanceof ViewModelInterface && !$result->getModule()) { $result->setModule($module); } }
/** * Creates the report about debugging dump. * * @param \Es\System\SystemEvent $event The system event */ public function __invoke(SystemEvent $event) { $system = $this->getSystem(); if ($system->isDevMode()) { $result = $event->getResult(SystemEvent::FINISH); if ($result instanceof ResponseInterface) { $contentType = $result->getHeaderLine('Content-Type'); if (0 === strpos($contentType, 'text/html')) { $model = $this->getModel(); $view = $this->getView(); $model['dumps'] = Debug::getDumpInstances(); $injection = $view->render($model); $body = (string) $result->getBody(); $html = str_replace('</body>', $injection . '</body>', $body); $stream = Stream::make($html); $event->setResult(SystemEvent::FINISH, $result->withBody($stream)); } } } }