/** * Selects the template engine and trigger it. * * @param \Es\View\ViewEvent $event The view event * * @throws \UnexpectedValueException * * - If the extension of template file is not associated with any * template engine * - If the specified template engine not implements * the Es\View\TemplateEngineInterface */ public function __invoke(ViewEvent $event) { $resolver = $this->getResolver(); $model = $event->getContext(); $template = $model->getTemplate(); $module = $model->getModule(); $file = $resolver->resolve($template, $module); $extension = pathinfo($file, PATHINFO_EXTENSION); if (!isset($this->config[$extension])) { throw new UnexpectedValueException(sprintf('Unable to render template "%s" of module "%s". the extension ' . '"%s" of template file is not associated with any ' . 'template engine.', $template, $module, $extension)); } $services = $this->getServices(); $engine = $services->get($this->config[$extension]); if (!$engine instanceof TemplateEngineInterface) { throw new UnexpectedValueException(sprintf('The template engine "%s" must implement the "%s".', $this->config[$extension], TemplateEngineInterface::CLASS)); } $result = $engine->render($model); $event->setResult($result); }
public function testConstructor() { $model = new ViewModel(); $event = new ViewEvent($model); $this->assertSame($model, $event->getContext()); }