Source of file ExceptionStrategy.php
Size: 4,298 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Mvc/View/Http/ExceptionStrategy.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Mvc\View\Http; use Zend\EventManager\AbstractListenerAggregate; use Zend\EventManager\EventManagerInterface; use Zend\Http\Response as HttpResponse; use Zend\Mvc\Application; use Zend\Mvc\MvcEvent; use Zend\Stdlib\ResponseInterface as Response; use Zend\View\Model\ViewModel; class ExceptionStrategy extends AbstractListenerAggregate { /** * Display exceptions? * @var bool */ protected $displayExceptions = false; /** * Name of exception template * @var string */ protected $exceptionTemplate = 'error'; /** * {@inheritDoc} */ public function attach(EventManagerInterface $events) { $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'prepareExceptionViewModel')); $this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER_ERROR, array($this, 'prepareExceptionViewModel')); } /** * Flag: display exceptions in error pages? * * @param bool $displayExceptions * @return ExceptionStrategy */ public function setDisplayExceptions($displayExceptions) { $this->displayExceptions = (bool) $displayExceptions; return $this; } /** * Should we display exceptions in error pages? * * @return bool */ public function displayExceptions() { return $this->displayExceptions; } /** * Set the exception template * * @param string $exceptionTemplate * @return ExceptionStrategy */ public function setExceptionTemplate($exceptionTemplate) { $this->exceptionTemplate = (string) $exceptionTemplate; return $this; } /** * Retrieve the exception template * * @return string */ public function getExceptionTemplate() { return $this->exceptionTemplate; } /** * Create an exception view model, and set the HTTP status code * * @todo dispatch.error does not halt dispatch unless a response is * returned. As such, we likely need to trigger rendering as a low * priority dispatch.error event (or goto a render event) to ensure * rendering occurs, and that munging of view models occurs when * expected. * @param MvcEvent $e * @return void */ public function prepareExceptionViewModel(MvcEvent $e) { // Do nothing if no error in the event $error = $e->getError(); if (empty($error)) { return; } // Do nothing if the result is a response object $result = $e->getResult(); if ($result instanceof Response) { return; } switch ($error) { case Application::ERROR_CONTROLLER_NOT_FOUND: case Application::ERROR_CONTROLLER_INVALID: case Application::ERROR_ROUTER_NO_MATCH: // Specifically not handling these return; case Application::ERROR_EXCEPTION: default: $model = new ViewModel(array( 'message' => 'An error occurred during execution; please try again later.', 'exception' => $e->getParam('exception'), 'display_exceptions' => $this->displayExceptions(), )); $model->setTemplate($this->getExceptionTemplate()); $e->setResult($model); $response = $e->getResponse(); if (!$response) { $response = new HttpResponse(); $response->setStatusCode(500); $e->setResponse($response); } else { $statusCode = $response->getStatusCode(); if ($statusCode === 200) { $response->setStatusCode(500); } } break; } } } |