fix travis build
[GitHub/Stricted/Domain-Control-Panel.git] / vendor / Zend / Mvc / View / Http / InjectViewModelListener.php
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
4 *
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
8 */
9
10 namespace Zend\Mvc\View\Http;
11
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;
17
18 class InjectViewModelListener extends AbstractListenerAggregate
19 {
20 /**
21 * {@inheritDoc}
22 */
23 public function attach(Events $events, $priority = 1)
24 {
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);
28 }
29
30 /**
31 * Insert the view model into the event
32 *
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.
36 *
37 * @param MvcEvent $e
38 * @return void
39 */
40 public function injectViewModel(MvcEvent $e)
41 {
42 $result = $e->getResult();
43 if (!$result instanceof ViewModel) {
44 return;
45 }
46
47 $model = $e->getViewModel();
48
49 if ($result->terminate()) {
50 $e->setViewModel($result);
51 return;
52 }
53
54 if ($e->getError() && $model instanceof ClearableModelInterface) {
55 $model->clearChildren();
56 }
57
58 $model->addChild($result);
59 }
60 }