Source of file InjectViewModelListener.php
Size: 2,020 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Mvc/View/Http/InjectViewModelListener.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Mvc\View\Http; use Zend\EventManager\AbstractListenerAggregate; use Zend\EventManager\EventManagerInterface as Events; use Zend\Mvc\MvcEvent; use Zend\View\Model\ClearableModelInterface; use Zend\View\Model\ModelInterface as ViewModel; class InjectViewModelListener extends AbstractListenerAggregate { /** * FilterInterface/inflector used to normalize names for use as template identifiers * * @var mixed */ protected $inflector; /** * {@inheritDoc} */ public function attach(Events $events) { $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'injectViewModel'), -100); $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'injectViewModel'), -100); $this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER_ERROR, array($this, 'injectViewModel'), -100); } /** * Insert the view model into the event * * Inspects the MVC result; if it's a view model, it then either (a) adds * it as a child to the default, composed view model, or (b) replaces it * if the result is marked as terminable. * * @param MvcEvent $e * @return void */ public function injectViewModel(MvcEvent $e) { $result = $e->getResult(); if (!$result instanceof ViewModel) { return; } $model = $e->getViewModel(); if ($result->terminate()) { $e->setViewModel($result); return; } if ($e->getError() && $model instanceof ClearableModelInterface) { $model->clearChildren(); } $model->addChild($result); } } |