init(); } /** * Return the template engine object * * Returns the object instance, as it is its own template engine * * @return PhpRenderer */ public function getEngine() { return $this; } /** * Allow custom object initialization when extending PhpRenderer * * Triggered by {@link __construct() the constructor} as its final action. * * @return void */ public function init() { } /** * Set script resolver * * @param Resolver $resolver * @return PhpRenderer * @throws Exception\InvalidArgumentException */ public function setResolver(Resolver $resolver) { $this->__templateResolver = $resolver; return $this; } /** * Retrieve template name or template resolver * * @param null|string $name * @return string|Resolver */ public function resolver($name = null) { if (null === $this->__templateResolver) { $this->setResolver(new TemplatePathStack()); } if (null !== $name) { return $this->__templateResolver->resolve($name, $this); } return $this->__templateResolver; } /** * Set variable storage * * Expects either an array, or an object implementing ArrayAccess. * * @param array|ArrayAccess $variables * @return PhpRenderer * @throws Exception\InvalidArgumentException */ public function setVars($variables) { if (!is_array($variables) && !$variables instanceof ArrayAccess) { throw new Exception\InvalidArgumentException(sprintf( 'Expected array or ArrayAccess object; received "%s"', (is_object($variables) ? get_class($variables) : gettype($variables)) )); } // Enforce a Variables container if (!$variables instanceof Variables) { $variablesAsArray = array(); foreach ($variables as $key => $value) { $variablesAsArray[$key] = $value; } $variables = new Variables($variablesAsArray); } $this->__vars = $variables; return $this; } /** * Get a single variable, or all variables * * @param mixed $key * @return mixed */ public function vars($key = null) { if (null === $this->__vars) { $this->setVars(new Variables()); } if (null === $key) { return $this->__vars; } return $this->__vars[$key]; } /** * Get a single variable * * @param mixed $key * @return mixed */ public function get($key) { if (null === $this->__vars) { $this->setVars(new Variables()); } return $this->__vars[$key]; } /** * Overloading: proxy to Variables container * * @param string $name * @return mixed */ public function __get($name) { $vars = $this->vars(); return $vars[$name]; } /** * Overloading: proxy to Variables container * * @param string $name * @param mixed $value * @return void */ public function __set($name, $value) { $vars = $this->vars(); $vars[$name] = $value; } /** * Overloading: proxy to Variables container * * @param string $name * @return bool */ public function __isset($name) { $vars = $this->vars(); return isset($vars[$name]); } /** * Overloading: proxy to Variables container * * @param string $name * @return void */ public function __unset($name) { $vars = $this->vars(); if (!isset($vars[$name])) { return; } unset($vars[$name]); } /** * Set helper plugin manager instance * * @param string|HelperPluginManager $helpers * @return PhpRenderer * @throws Exception\InvalidArgumentException */ public function setHelperPluginManager($helpers) { if (is_string($helpers)) { if (!class_exists($helpers)) { throw new Exception\InvalidArgumentException(sprintf( 'Invalid helper helpers class provided (%s)', $helpers )); } $helpers = new $helpers(); } if (!$helpers instanceof HelperPluginManager) { throw new Exception\InvalidArgumentException(sprintf( 'Helper helpers must extend Zend\View\HelperPluginManager; got type "%s" instead', (is_object($helpers) ? get_class($helpers) : gettype($helpers)) )); } $helpers->setRenderer($this); $this->__helpers = $helpers; return $this; } /** * Get helper plugin manager instance * * @return HelperPluginManager */ public function getHelperPluginManager() { if (null === $this->__helpers) { $this->setHelperPluginManager(new HelperPluginManager()); } return $this->__helpers; } /** * Get plugin instance * * @param string $name Name of plugin to return * @param null|array $options Options to pass to plugin constructor (if not already instantiated) * @return AbstractHelper */ public function plugin($name, array $options = null) { return $this->getHelperPluginManager()->get($name, $options); } /** * Overloading: proxy to helpers * * Proxies to the attached plugin manager to retrieve, return, and potentially * execute helpers. * * * If the helper does not define __invoke, it will be returned * * If the helper does define __invoke, it will be called as a functor * * @param string $method * @param array $argv * @return mixed */ public function __call($method, $argv) { $plugin = $this->plugin($method); if (is_callable($plugin)) { return call_user_func_array($plugin, $argv); } return $plugin; } /** * Set filter chain * * @param FilterChain $filters * @return PhpRenderer */ public function setFilterChain(FilterChain $filters) { $this->__filterChain = $filters; return $this; } /** * Retrieve filter chain for post-filtering script content * * @return FilterChain */ public function getFilterChain() { if (null === $this->__filterChain) { $this->setFilterChain(new FilterChain()); } return $this->__filterChain; } /** * Processes a view script and returns the output. * * @param string|Model $nameOrModel Either the template to use, or a * ViewModel. The ViewModel must have the * template as an option in order to be * valid. * @param null|array|Traversable $values Values to use when rendering. If none * provided, uses those in the composed * variables container. * @return string The script output. * @throws Exception\DomainException if a ViewModel is passed, but does not * contain a template option. * @throws Exception\InvalidArgumentException if the values passed are not * an array or ArrayAccess object * @throws Exception\RuntimeException if the template cannot be rendered */ public function render($nameOrModel, $values = null) { if ($nameOrModel instanceof Model) { $model = $nameOrModel; $nameOrModel = $model->getTemplate(); if (empty($nameOrModel)) { throw new Exception\DomainException(sprintf( '%s: received View Model argument, but template is empty', __METHOD__ )); } $options = $model->getOptions(); foreach ($options as $setting => $value) { $method = 'set' . $setting; if (method_exists($this, $method)) { $this->$method($value); } unset($method, $setting, $value); } unset($options); // Give view model awareness via ViewModel helper $helper = $this->plugin('view_model'); $helper->setCurrent($model); $values = $model->getVariables(); unset($model); } // find the script file name using the parent private method $this->addTemplate($nameOrModel); unset($nameOrModel); // remove $name from local scope $this->__varsCache[] = $this->vars(); if (null !== $values) { $this->setVars($values); } unset($values); // extract all assigned vars (pre-escaped), but not 'this'. // assigns to a double-underscored variable, to prevent naming collisions $__vars = $this->vars()->getArrayCopy(); if (array_key_exists('this', $__vars)) { unset($__vars['this']); } extract($__vars); unset($__vars); // remove $__vars from local scope while ($this->__template = array_pop($this->__templates)) { $this->__file = $this->resolver($this->__template); if (!$this->__file) { throw new Exception\RuntimeException(sprintf( '%s: Unable to render template "%s"; resolver could not resolve to a file', __METHOD__, $this->__template )); } try { ob_start(); $includeReturn = include $this->__file; $this->__content = ob_get_clean(); } catch (\Exception $ex) { ob_end_clean(); throw $ex; } if ($includeReturn === false && empty($this->__content)) { throw new Exception\UnexpectedValueException(sprintf( '%s: Unable to render template "%s"; file include failed', __METHOD__, $this->__file )); } } $this->setVars(array_pop($this->__varsCache)); return $this->getFilterChain()->filter($this->__content); // filter output } /** * Set flag indicating whether or not we should render trees of view models * * If set to true, the View instance will not attempt to render children * separately, but instead pass the root view model directly to the PhpRenderer. * It is then up to the developer to render the children from within the * view script. * * @param bool $renderTrees * @return PhpRenderer */ public function setCanRenderTrees($renderTrees) { $this->__renderTrees = (bool) $renderTrees; return $this; } /** * Can we render trees, or are we configured to do so? * * @return bool */ public function canRenderTrees() { return $this->__renderTrees; } /** * Add a template to the stack * * @param string $template * @return PhpRenderer */ public function addTemplate($template) { $this->__templates[] = $template; return $this; } /** * Make sure View variables are cloned when the view is cloned. * * @return PhpRenderer */ public function __clone() { $this->__vars = clone $this->vars(); } }