/** * @return mixed */ public function notFoundByRequestedCriteria($criteriaErrors) { $zendResponse = $this->mvcEvent->getResponse(); $zendResponse->setStatusCode(404); $this->viewModel->setVariable('message', 'The requested resource was not found by requested criteria'); $this->viewModel->setTemplate('error/404'); $this->mvcEvent->setResult($this->viewModel); return $this->viewModel; }
/** * * @param ModelInterface $model */ protected function renderChildren(ModelInterface $model) { foreach ($model->getChildren() as $child) { $result = $this->render($child); $capture = $child->captureTo(); if (!empty($capture)) { if ($child->isAppend()) { $oldResult = $model->{$capture}; $model->setVariable($capture, $oldResult . $result); } else { $model->setVariable($capture, $result); } } } }
/** * @param \Zend\View\Model\ModelInterface $oViewModel * @throws \DomainException * @return \BoilerAppMessenger\Media\Mail\MailMessageRenderer */ protected function renderChildren(\Zend\View\Model\ModelInterface $oViewModel) { foreach ($oViewModel as $oChild) { if ($oChild->terminate()) { throw new \DomainException('Inconsistent state; child view model is marked as terminal'); } $oChild->setOption('has_parent', true); $sResult = $this->renderChildren($oChild)->render($oChild); $oChild->setOption('has_parent', null); $sCapture = $oChild->captureTo(); if (!empty($sCapture)) { $oViewModel->setVariable($sCapture, $oChild->isAppend() ? $oViewModel->{$sCapture} . $sResult : $sResult); } } return $this; }
/** * Loop through children, rendering each * * @param Model $model * @throws Exception\DomainException * @return void */ protected function renderChildren(Model $model) { foreach ($model as $child) { if ($child->terminate()) { throw new Exception\DomainException('Inconsistent state; child view model is marked as terminal'); } $child->setOption('has_parent', true); $result = $this->render($child); $child->setOption('has_parent', null); $capture = $child->captureTo(); if (!empty($capture)) { if ($child->isAppend()) { $oldResult = $model->{$capture}; $model->setVariable($capture, $oldResult . $result); } else { $model->setVariable($capture, $result); } } } }
/** * Inject discovered child model values into parent model * * @todo detect collisions and decide whether to append and/or aggregate? * @param Model $model * @param array $children */ protected function injectChildren(Model $model, array $children) { foreach ($children as $child => $value) { // TODO detect collisions and decide whether to append and/or aggregate? $model->setVariable($child, $value); } }
/** * Do a recursive, depth-first rendering of a view model. * * @param ModelInterface $model * @param RendererInterface $renderer * @return string * @throws Exception\RenderingException if it encounters a terminal child. */ private function renderModel(ModelInterface $model, RendererInterface $renderer) { foreach ($model as $child) { if ($child->terminate()) { throw new Exception\RenderingException('Cannot render; encountered a child marked terminal'); } $capture = $child->captureTo(); if (empty($capture)) { continue; } $result = $this->renderModel($child, $renderer); if ($child->isAppend()) { $oldResult = $model->{$capture}; $model->setVariable($capture, $oldResult . $result); continue; } $model->setVariable($capture, $result); } return $renderer->render($model); }
/** * assign wrapper template to block * * @param ModelInterface $block * @param array|string $options */ protected function wrapBlock(ModelInterface $block, $options) { $attributes = $options; if (is_string($options)) { $wrapperTemplate = $options; $attributes = []; } elseif (is_array($options) && !isset($options['template'])) { $wrapperTemplate = self::WRAPPER_DEFAULT; } else { $wrapperTemplate = $options['template']; unset($attributes['template']); } if (isset($options['tag'])) { $block->setVariable('wrapperTag', $options['tag']); unset($attributes['tag']); } $originalTemplate = $block->getTemplate(); $block->setOption('is_wrapped', true); $block->setTemplate($wrapperTemplate); $block->setVariable('wrapperAttributes', $attributes); $block->setVariable('originalTemplate', $originalTemplate); }
/** * * @param ViewModel $block * @return string */ protected function determineAnonymousBlockId(ModelInterface $block) { $blockId = $block->getVariable(self::BLOCK_ID_VAR); if (!$blockId) { $blockId = sprintf(self::ANONYMOUS_ID_PATTERN, $block->captureTo(), self::$anonymousSuffix++); $block->setVariable(self::BLOCK_ID_VAR, $blockId); } return $blockId; }
/** * @inheritDoc */ public function configure(ModelInterface $block, array $specs) { $specs = $this->prepareOptions($specs); foreach ($this->getOption('options', $specs) as $name => $option) { $block->setOption($name, $option); } foreach ($this->getOption('variables', $specs) as $name => $variable) { $block->setVariable($name, $variable); } foreach ($this->getOption('actions', $specs) as $params) { if (isset($params['method'])) { $method = (string) $params['method']; if (method_exists($block, $method)) { $this->invokeArgs($block, $method, $params); } else { throw new BadMethodCallException(sprintf('Call to undefined block method %s::%s()', get_class($block), $method)); } } } if (!$block->getTemplate() && ($template = $this->getOption('template', $specs))) { $block->setTemplate($template); } $block->setCaptureTo($this->getOption('capture_to', $specs)); $block->setAppend($this->getOption('append', $specs)); $block->setVariable('block', $block); if ($block instanceof BlockInterface) { $block->setView($this->container->get('ViewRenderer')); $block->setRequest($this->container->get('Request')); } $results = $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, ['block' => $block, 'specs' => $specs], function ($result) { return $result instanceof ModelInterface; }); if ($results->stopped()) { $block = $results->last(); } return $block; }
/** * Add a variable to the View Model * * @param string $name * @param mixed $value */ protected function addVariable($name, $value) { $this->model->setVariable($name, $value); }
/** * Add acl to the view model * * @param ModelInterface $viewModel * * @return mixed */ public function attachToView(ModelInterface $viewModel) { $viewModel->setVariable('acl', $this->getAcl()); return $this; }
/** * Prepare the layout, if any. * * Injects the view model in the layout view model, if present. * * If the view model contains a non-empty 'layout' variable, that value * will be used to seed a layout view model, if: * * - it is a string layout template name * - it is a ModelInterface instance * * If a layout is discovered in this way, it will override the one set in * the constructor, if any. * * Returns the provided $viewModel unchanged if no layout is discovered; * otherwise, a view model representing the layout, with the provided * view model as a child, is returned. * * @param ModelInterface $viewModel * @return ModelInterface */ private function prepareLayout(ModelInterface $viewModel) { $layout = $this->layout ? clone $this->layout : null; $providedLayout = $viewModel->getVariable('layout', false); if (is_string($providedLayout) && !empty($providedLayout)) { $layout = new ViewModel(); $layout->setTemplate($providedLayout); $viewModel->setVariable('layout', null); } elseif ($providedLayout instanceof ModelInterface) { $layout = $providedLayout; $viewModel->setVariable('layout', null); } if ($layout) { $layout->addChild($viewModel); $viewModel = $layout; } return $viewModel; }