public function injectLayout(MvcEvent $e) { $request = $e->getRequest(); if (!$request instanceof HttpRequest) { return; } $result = $e->getResult(); if (!$result instanceof ZendViewModel) { return; } $response = $e->getResponse(); if ($response->getStatusCode() != 200) { return; } $matchedRoute = $e->getRouteMatch(); if (!$matchedRoute) { return; } $matchedRouteName = $matchedRoute->getMatchedRouteName(); $serviceLocator = $e->getApplication()->getServiceManager(); $config = $serviceLocator->get('config'); $options = $config['sebaks-view']; if (!isset($options['contents'][$matchedRouteName])) { return; } $viewConfig = $options['contents'][$matchedRouteName]; $config = new Config(array_merge($options['layouts'], $options['contents'], $options['blocks'])); $viewConfig = $config->applyInheritance($viewConfig); $viewBuilder = new ViewBuilder($config, $serviceLocator); $data = $result->getVariables()->getArrayCopy(); if (isset($viewConfig['layout'])) { $viewComponentLayout = $viewConfig['layout']; if (!isset($options['layouts'][$viewComponentLayout])) { throw new \Exception("Layout '{$viewComponentLayout}' not found for view component '{$matchedRouteName}'"); } $options['layouts'][$viewComponentLayout]['children']['content'] = $viewConfig; $viewComponent = $viewBuilder->build($options['layouts'][$viewComponentLayout], $data); } else { $viewComponent = $viewBuilder->build($viewConfig, $data); } $viewComponent->setTerminal(true); $e->setViewModel($viewComponent); $e->setResult($viewComponent); }
/** * @param $config * @param array $data * @return array|object|ViewModel */ public function build($config, array $data = array()) { $allOptions = $this->config->getOptions(); $i = 0; $queue = new \SplQueue(); $queue->enqueue($config); while ($queue->count() > 0) { $options = $queue->dequeue(); $options = $this->config->applyInheritance($options); if (isset($options['viewModel'])) { $this->serviceLocator->setShared($options['viewModel'], false); $viewModel = $this->serviceLocator->get($options['viewModel']); } else { $viewModel = new ZendViewModel(); } if ($i == 0) { $rootViewModel = $viewModel; } if (isset($options['template'])) { $viewModel->setTemplate($options['template']); } if (isset($options['capture'])) { $viewModel->setCaptureTo($options['capture']); } elseif (isset($options['id'])) { $viewModel->setCaptureTo($options['id']); } if (isset($options['data']['static'])) { $viewModel->setVariables($options['data']['static']); } if (isset($options['data']['fromGlobal'])) { $globalVar = $options['data']['fromGlobal']; if (is_array($globalVar)) { foreach ($globalVar as $globalVarName => $viewVarName) { $globalVarValue = $this->getVarValue($globalVarName, $data); $viewModel->setVariable($viewVarName, $globalVarValue); } } else { $globalVarValue = $this->getVarValue($globalVar, $data); $viewModel->setVariable($globalVar, $globalVarValue); } } if (isset($options['parent'])) { /** @var ViewModel $parent */ $parent = $options['parent']; $parent->addChild($viewModel, $viewModel->captureTo(), true); if (isset($options['data']['fromParent'])) { $varFromParent = $options['data']['fromParent']; $parentVars = $parent->getVariables(); if (is_array($varFromParent)) { foreach ($varFromParent as $varFromParentName => $viewVarName) { $fromParentVal = $this->getVarValue($varFromParentName, $parentVars); if ($fromParentVal === null) { $fromParentVal = $parent->getVariable($varFromParentName); } if (is_array($viewVarName)) { $dataFromParent = []; foreach ($viewVarName as $varName) { $dataFromParent[$varName] = $fromParentVal; } } else { $dataFromParent = [$viewVarName => $fromParentVal]; } $viewModel->setVariables($dataFromParent); } } else { $viewVarName = $options['data']['fromParent']; $fromParentVal = $this->getVarValue($viewVarName, $parentVars); if ($fromParentVal === null) { $fromParentVal = $parent->getVariable($viewVarName); } $viewModel->setVariables([$viewVarName => $fromParentVal]); } } } if (!empty($options['children'])) { foreach ($options['children'] as $childId => $child) { if (is_string($child)) { $childId = $child; $child = $allOptions[$child]; } if (isset($options['childrenDynamicLists'][$childId])) { continue; } $child['id'] = $childId; $child['parent'] = $viewModel; $queue->enqueue($child); } } if (isset($options['childrenDynamicLists'])) { foreach ($options['childrenDynamicLists'] as $childName => $listName) { $list = $viewModel->getVariable($listName); if ($list === null) { throw new \UnexpectedValueException("Cannot build children list of '{$childName}' by '{$listName}' list . View does not contain variable '{$listName}'."); } if (!is_array($list) && !$list instanceof \Traversable) { throw new \UnexpectedValueException("Cannot build children list of '{$childName}' by '{$listName}' list . List '{$listName}' must be array " . gettype($list) . " given."); } if (array_key_exists($childName, $options['children'])) { $child = $options['children'][$childName]; } else { if (in_array($childName, $options['children'])) { $child = $allOptions[$childName]; } else { throw new \UnexpectedValueException("Cannot build children list of '{$childName}' by '{$listName}' list . Child '{$childName}' not found"); } } $child['id'] = $childName; $child['parent'] = $viewModel; if (isset($child['data']['fromParent'])) { $varFromParent = $child['data']['fromParent']; } foreach ($list as $entry) { if (isset($varFromParent)) { if (is_array($varFromParent)) { foreach ($varFromParent as $varFromParentName => $viewVarName) { $dataForChild = [$viewVarName => $entry]; } } else { $dataForChild = [$varFromParent => $entry]; } if (!isset($child['data']['static'])) { $child['data']['static'] = []; } $child['data']['static'] = array_merge($child['data']['static'], $dataForChild); unset($child['data']['fromParent']); } $queue->enqueue($child); } } } $i++; } return $rootViewModel; }
public function testApplyInheritance() { $result = $this->config->applyInheritance($this->options['block1']); $expect = ['extend' => 'block2', 'template' => 'block3/template', 'children' => ['block4/template'], 'variables' => ['block2/variable' => 'b2v', 'block3/variable' => 'b2v', 'block3/variable2' => 'b3v2']]; $this->assertEquals($expect, $result); }