3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\View\Http
;
12 use Zend\EventManager\AbstractListenerAggregate
;
13 use Zend\EventManager\EventManagerInterface
as Events
;
14 use Zend\Mvc\MvcEvent
;
15 use Zend\View\Model\ClearableModelInterface
;
16 use Zend\View\Model\ModelInterface
as ViewModel
;
18 class InjectViewModelListener
extends AbstractListenerAggregate
23 public function attach(Events
$events, $priority = 1)
25 $this->listeners
[] = $events->attach(MvcEvent
::EVENT_DISPATCH
, [$this, 'injectViewModel'], -100);
26 $this->listeners
[] = $events->attach(MvcEvent
::EVENT_DISPATCH_ERROR
, [$this, 'injectViewModel'], -100);
27 $this->listeners
[] = $events->attach(MvcEvent
::EVENT_RENDER_ERROR
, [$this, 'injectViewModel'], -100);
31 * Insert the view model into the event
33 * Inspects the MVC result; if it's a view model, it then either (a) adds
34 * it as a child to the default, composed view model, or (b) replaces it
35 * if the result is marked as terminable.
40 public function injectViewModel(MvcEvent
$e)
42 $result = $e->getResult();
43 if (!$result instanceof ViewModel
) {
47 $model = $e->getViewModel();
49 if ($result->terminate()) {
50 $e->setViewModel($result);
54 if ($e->getError() && $model instanceof ClearableModelInterface
) {
55 $model->clearChildren();
58 $model->addChild($result);