3 namespace Application\View;
7 Zend\EventManager\EventCollection,
8 Zend\EventManager\ListenerAggregate,
9 Zend\EventManager\StaticEventCollection,
10 Zend\Http\PhpEnvironment\Response,
15 class Listener implements ListenerAggregate
18 protected $listeners = array();
19 protected $staticListeners = array();
21 protected $displayExceptions = false;
23 public function __construct(Renderer $renderer, $layout = 'layout.phtml')
25 $this->view = $renderer;
26 $this->layout = $layout;
29 public function setDisplayExceptionsFlag($flag)
31 $this->displayExceptions = (bool) $flag;
35 public function displayExceptions()
37 return $this->displayExceptions;
40 public function attach(EventCollection $events)
42 $this->listeners[] = $events->attach('dispatch.error', array($this, 'renderError'));
43 $this->listeners[] = $events->attach('dispatch', array($this, 'render404'), -1000);
44 $this->listeners[] = $events->attach('dispatch', array($this, 'renderLayout'), -80);
47 public function detach(EventCollection $events)
49 foreach ($this->listeners as $key => $listener) {
50 $events->detach($listener);
51 unset($this->listeners[$key]);
56 public function registerStaticListeners(StaticEventCollection $events, $locator)
58 $ident = 'Zend\Mvc\Controller\ActionController';
59 $handler = $events->attach($ident, 'dispatch', array($this, 'renderView'), -50);
60 $this->staticListeners[] = array($ident, $handler);
63 public function detachStaticListeners(StaticEventCollection $events)
65 foreach ($this->staticListeners as $i => $info) {
66 list($id, $handler) = $info;
67 $events->detach($id, $handler);
68 unset($this->staticListeners[$i]);
72 public function renderView(MvcEvent $e)
74 $response = $e->getResponse();
75 if (!$response->isSuccess()) {
79 $routeMatch = $e->getRouteMatch();
80 $controller = $routeMatch->getParam('controller', 'index');
81 $action = $routeMatch->getParam('action', 'index');
82 $script = $controller . '/' . $action . '.phtml';
84 $vars = $e->getResult();
85 if (is_scalar($vars)) {
86 $vars = array('content' => $vars);
87 } elseif (is_object($vars) && !$vars instanceof ArrayAccess) {
88 $vars = (array) $vars;
91 $content = $this->view->render($script, $vars);
93 $e->setParam('content', $content);
97 public function renderLayout(MvcEvent $e)
99 $response = $e->getResponse();
101 $response = new Response();
102 $e->setResponse($response);
104 if ($response->isRedirect()) {
108 $vars = $e->getResult();
109 if (is_scalar($vars)) {
110 $vars = array('content' => $vars);
111 } elseif (is_object($vars) && !$vars instanceof ArrayAccess) {
112 $vars = (array) $vars;
115 if (false !== ($contentParam = $e->getParam('content', false))) {
116 $vars['content'] = $contentParam;
119 $layout = $this->view->render($this->layout, $vars);
120 $response->setContent($layout);
124 public function render404(MvcEvent $e)
126 $vars = $e->getResult();
127 if ($vars instanceof Response) {
131 $response = $e->getResponse();
132 if ($response->getStatusCode() != 404) {
138 'message' => 'Page not found.',
139 'exception' => $e->getParam('exception'),
140 'display_exceptions' => $this->displayExceptions(),
143 $content = $this->view->render('error/404.phtml', $vars);
145 $e->setResult($content);
147 return $this->renderLayout($e);
150 public function renderError(MvcEvent $e)
152 $error = $e->getError();
153 $response = $e->getResponse();
155 $response = new Response();
156 $e->setResponse($response);
160 case Application::ERROR_CONTROLLER_NOT_FOUND:
161 case Application::ERROR_CONTROLLER_INVALID:
163 'message' => 'Page not found.',
164 'exception' => $e->getParam('exception'),
165 'display_exceptions' => $this->displayExceptions(),
167 $response->setStatusCode(404);
170 case Application::ERROR_EXCEPTION:
173 'message' => 'An error occurred during execution; please try again later.',
174 'exception' => $e->getParam('exception'),
175 'display_exceptions' => $this->displayExceptions(),
177 $response->setStatusCode(500);
181 $content = $this->view->render('error/index.phtml', $vars);
183 $e->setResult($content);
185 return $this->renderLayout($e);