Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Mvc / View / Http / ExceptionStrategy.php
blobbb81fd810900be8731a0e5d68d0b8e35f4254908
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Mvc\View\Http;
12 use Zend\EventManager\AbstractListenerAggregate;
13 use Zend\EventManager\EventManagerInterface;
14 use Zend\Http\Response as HttpResponse;
15 use Zend\Mvc\Application;
16 use Zend\Mvc\MvcEvent;
17 use Zend\Stdlib\ResponseInterface as Response;
18 use Zend\View\Model\ViewModel;
20 class ExceptionStrategy extends AbstractListenerAggregate
22 /**
23 * Display exceptions?
24 * @var bool
26 protected $displayExceptions = false;
28 /**
29 * Name of exception template
30 * @var string
32 protected $exceptionTemplate = 'error';
34 /**
35 * {@inheritDoc}
37 public function attach(EventManagerInterface $events)
39 $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'prepareExceptionViewModel'));
40 $this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER_ERROR, array($this, 'prepareExceptionViewModel'));
43 /**
44 * Flag: display exceptions in error pages?
46 * @param bool $displayExceptions
47 * @return ExceptionStrategy
49 public function setDisplayExceptions($displayExceptions)
51 $this->displayExceptions = (bool) $displayExceptions;
52 return $this;
55 /**
56 * Should we display exceptions in error pages?
58 * @return bool
60 public function displayExceptions()
62 return $this->displayExceptions;
65 /**
66 * Set the exception template
68 * @param string $exceptionTemplate
69 * @return ExceptionStrategy
71 public function setExceptionTemplate($exceptionTemplate)
73 $this->exceptionTemplate = (string) $exceptionTemplate;
74 return $this;
77 /**
78 * Retrieve the exception template
80 * @return string
82 public function getExceptionTemplate()
84 return $this->exceptionTemplate;
87 /**
88 * Create an exception view model, and set the HTTP status code
90 * @todo dispatch.error does not halt dispatch unless a response is
91 * returned. As such, we likely need to trigger rendering as a low
92 * priority dispatch.error event (or goto a render event) to ensure
93 * rendering occurs, and that munging of view models occurs when
94 * expected.
95 * @param MvcEvent $e
96 * @return void
98 public function prepareExceptionViewModel(MvcEvent $e)
100 // Do nothing if no error in the event
101 $error = $e->getError();
102 if (empty($error)) {
103 return;
106 // Do nothing if the result is a response object
107 $result = $e->getResult();
108 if ($result instanceof Response) {
109 return;
112 switch ($error) {
113 case Application::ERROR_CONTROLLER_NOT_FOUND:
114 case Application::ERROR_CONTROLLER_INVALID:
115 case Application::ERROR_ROUTER_NO_MATCH:
116 // Specifically not handling these
117 return;
119 case Application::ERROR_EXCEPTION:
120 default:
121 $model = new ViewModel(array(
122 'message' => 'An error occurred during execution; please try again later.',
123 'exception' => $e->getParam('exception'),
124 'display_exceptions' => $this->displayExceptions(),
126 $model->setTemplate($this->getExceptionTemplate());
127 $e->setResult($model);
129 $response = $e->getResponse();
130 if (!$response) {
131 $response = new HttpResponse();
132 $response->setStatusCode(500);
133 $e->setResponse($response);
134 } else {
135 $statusCode = $response->getStatusCode();
136 if ($statusCode === 200) {
137 $response->setStatusCode(500);
141 break;