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'), -80);
44 $this->listeners[] = $events->attach('dispatch', array($this, 'renderLayout'), -1000);
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 = 'pages/' . $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->setResult($content);
134 public function renderLayout(MvcEvent $e)
136 $response = $e->getResponse();
138 $response = new Response();
139 $e->setResponse($response);
141 if ($response->isRedirect()) {
145 $footer = $e->getParam('footer', false);
146 $vars = array('footer' => $footer);
148 if (false !== ($contentParam = $e->getParam('content', false))) {
149 $vars['content'] = $contentParam;
151 $vars['content'] = $e->getResult();
154 $layout = $this->view->render($this->layout, $vars);
155 $response->setContent($layout);
159 public function render404(MvcEvent $e)
161 $vars = $e->getResult();
162 if ($vars instanceof Response) {
166 $response = $e->getResponse();
167 if ($response->getStatusCode() != 404) {
173 'message' => 'Page not found.',
174 'exception' => $e->getParam('exception'),
175 'display_exceptions' => $this->displayExceptions(),
178 $content = $this->view->render('pages/404.phtml', $vars);
180 $e->setResult($content);
182 return $this->renderLayout($e);
185 public function renderError(MvcEvent $e)
187 $error = $e->getError();
188 $app = $e->getTarget();
189 $response = $e->getResponse();
191 $response = new Response();
192 $e->setResponse($response);
196 case Application::ERROR_CONTROLLER_NOT_FOUND:
197 case Application::ERROR_CONTROLLER_INVALID:
199 'message' => 'Page not found.',
200 'exception' => $e->getParam('exception'),
201 'display_exceptions' => $this->displayExceptions(),
203 $response->setStatusCode(404);
206 case Application::ERROR_EXCEPTION:
208 $exception = $e->getParam('exception');
210 'message' => 'An error occurred during execution; please try again later.',
211 'exception' => $e->getParam('exception'),
212 'display_exceptions' => $this->displayExceptions(),
214 $response->setStatusCode(500);
218 $content = $this->view->render('error/index.phtml', $vars);
220 $e->setResult($content);
222 return $this->renderLayout($e);