/** * * @param ViewModel $viewModel */ private function extractCacheTags(ViewModel $viewModel) { if ($viewModel->getOption('esi') && !$viewModel->terminate()) { return; } $tags = (array) $viewModel->getOption(self::OPTION_CACHE_TAGS, []); $this->cacheTags = ArrayUtils::merge($this->cacheTags, $tags); if ($viewModel->hasChildren()) { foreach ($viewModel->getChildren() as $childViewModel) { $this->extractCacheTags($childViewModel); } } }
public function testTerminationFlagIsMutable() { $model = new ViewModel(); $model->setTerminal(true); $this->assertTrue($model->terminate()); }
/** * Render an action from a controller and render it's associated template * @param string $expr * @param array $attributes * @param array $options * @return string */ public function renderAction($expr, array $attributes, array $options) { ArgValidator::assert($expr, 'string'); $serviceManager = Module::getServiceManager(); $application = $serviceManager->get('Application'); //parse the name of the controller, action and template directory that should be used if (strpos($expr, '/') > 0) { list($controllerName, $actionName) = explode('/', $expr); $templateDir = $controllerName . '/'; } else { list($moduleName, $controllerName, $actionName) = explode(':', $expr); $actionName = lcfirst($actionName); $actionName = strtolower(preg_replace('/([A-Z])/', '-$1', $actionName)); $templateDir = lcfirst($moduleName) . '/' . lcfirst($controllerName) . '/'; $controllerName = $moduleName . '\\Controller\\' . $controllerName . 'Controller'; } //instantiate the controller based on the given name $controller = $serviceManager->get('ControllerLoader')->get($controllerName); //clone the MvcEvent and route and update them with the provided parameters $event = $application->getMvcEvent(); $routeMatch = clone $event->getRouteMatch(); $event = clone $event; foreach ($attributes as $key => $value) { $routeMatch->setParam($key, $value); } $event->setRouteMatch($routeMatch); //inject the new event into the controller if ($controller instanceof InjectApplicationEventInterface) { $controller->setEvent($event); } //test if the action exists in the controller and change it to not-found if missing $method = AbstractActionController::getMethodFromAction($actionName); if (!method_exists($controller, $method)) { $method = 'notFoundAction'; $actionName = 'not-found'; } //call the method on the controller $response = $controller->{$method}(); //if the result is an instance of the Response class return it if ($response instanceof Response) { return $response->getBody(); } //if the response is an instance of ViewModel then render that one if ($response instanceof ModelInterface) { $viewModel = $response; } elseif ($response === null || is_array($response) || $response instanceof \ArrayAccess || $response instanceof \Traversable) { $viewModel = new ViewModel($response); $viewModel->setTemplate($templateDir . $actionName); } else { return ''; } $viewModel->terminate(); $viewModel->setOption('has_parent', true); $view = $serviceManager->get('Zend\\View\\View'); $output = $view->render($viewModel); return $output; }
/** * Inspect the result for erroneous action of JSON/AJAX/Feed * * @param MvcEvent $e * @return void */ public function canonizeErrorResult(MvcEvent $e) { $result = $e->getResult(); if ($result instanceof Response) { return; } if (!$this->type) { return; } $response = $e->getResponse(); $statusCode = $response->getStatusCode(); if ($statusCode < 400) { return; } // Cast controller view model to result ViewModel switch ($this->type) { // For Feed case 'feed': if ($result instanceof FeedModel) { $model = $result; $model->setTemplate(''); $model->clearChildren(); } else { $variables = array(); $options = array(); if ($result instanceof ViewModel) { $variables = $result->getVariables(); $options = $result->getOptions(); $variables = (array) new FeedDataModel($variables); } elseif ($result instanceof FeedDataModel) { $variables = (array) $result; $options = array('feed_type' => $result->getType()); } $model = new FeedModel($variables, $options); } break; // For Json // For Json case 'json': if ($result instanceof JsonModel) { $model = $result; $model->setTemplate(''); $model->clearChildren(); } else { $variables = array(); $options = array(); if ($result instanceof ViewModel) { $variables = $result->getVariables(); $options = $result->getOptions(); } $model = new JsonModel($variables, $options); } break; // For AJAX/Flash // For AJAX/Flash case 'ajax': // MISC // MISC default: if ($result instanceof ViewModel) { $model = $result; $model->setTemplate(''); $model->clearChildren(); $result = null; } else { $model = new ViewModel(); } $model->terminate(true); break; } $e->setViewModel($model); }