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 = 'Application\Controller\PageController';
59 $handler = $events->attach($ident, 'dispatch', array($this, 'renderPageController'), -50);
60 $this->staticListeners[] = array($ident, $handler);
62 $ident = 'Zend\Mvc\Controller\ActionController';
63 $handler = $events->attach($ident, 'dispatch', array($this, 'renderView'), -50);
64 $this->staticListeners[] = array($ident, $handler);
67 public function detachStaticListeners(StaticEventCollection $events)
69 foreach ($this->staticListeners as $i => $info) {
70 list($id, $handler) = $info;
71 $events->detach($id, $handler);
72 unset($this->staticListeners[$i]);
76 public function renderPageController(MvcEvent $e)
78 $page = $e->getResult();
79 if ($page instanceof Response) {
83 $response = $e->getResponse();
84 if ($response->isNotFound()) {
88 $routeMatch = $e->getRouteMatch();
93 $page = $routeMatch->getParam('action', '404');
97 $response->setStatusCode(404);
100 $script = 'error/' . $page . '.phtml';
103 $content = $this->view->render($script);
104 $e->setResult($content);
106 return $this->renderLayout($e);
109 public function renderView(MvcEvent $e)
111 $response = $e->getResponse();
112 if (!$response->isSuccess()) {
116 $routeMatch = $e->getRouteMatch();
117 $controller = $routeMatch->getParam('controller', 'index');
118 $action = $routeMatch->getParam('action', 'index');
119 $script = $controller . '/' . $action . '.phtml';
121 $vars = $e->getResult();
122 if (is_scalar($vars)) {
123 $vars = array('content' => $vars);
124 } elseif (is_object($vars) && !$vars instanceof ArrayAccess) {
125 $vars = (array) $vars;
128 $content = $this->view->render($script, $vars);
130 $e->setParam('content', $content);
134 public function renderLayout(MvcEvent $e)
136 $response = $e->getResponse();
138 $response = new Response();
139 $e->setResponse($response);
141 if ($response->isRedirect()) {
145 $vars = $e->getResult();
146 if (is_scalar($vars)) {
147 $vars = array('content' => $vars);
148 } elseif (is_object($vars) && !$vars instanceof ArrayAccess) {
149 $vars = (array) $vars;
152 if (false !== ($contentParam = $e->getParam('content', false))) {
153 $vars['content'] = $contentParam;
156 $layout = $this->view->render($this->layout, $vars);
157 $response->setContent($layout);
161 public function render404(MvcEvent $e)
163 $vars = $e->getResult();
164 if ($vars instanceof Response) {
168 $response = $e->getResponse();
169 if ($response->getStatusCode() != 404) {
175 'message' => 'Page not found.',
176 'exception' => $e->getParam('exception'),
177 'display_exceptions' => $this->displayExceptions(),
180 $content = $this->view->render('error/404.phtml', $vars);
182 $e->setResult($content);
184 return $this->renderLayout($e);
187 public function renderError(MvcEvent $e)
189 $error = $e->getError();
190 $app = $e->getTarget();
191 $response = $e->getResponse();
193 $response = new Response();
194 $e->setResponse($response);
198 case Application::ERROR_CONTROLLER_NOT_FOUND:
199 case Application::ERROR_CONTROLLER_INVALID:
201 'message' => 'Page not found.',
202 'exception' => $e->getParam('exception'),
203 'display_exceptions' => $this->displayExceptions(),
205 $response->setStatusCode(404);
208 case Application::ERROR_EXCEPTION:
210 $exception = $e->getParam('exception');
212 'message' => 'An error occurred during execution; please try again later.',
213 'exception' => $e->getParam('exception'),
214 'display_exceptions' => $this->displayExceptions(),
216 $response->setStatusCode(500);
220 $content = $this->view->render('error/index.phtml', $vars);
222 $e->setResult($content);
224 return $this->renderLayout($e);