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 / Console / ExceptionStrategy.php
bloba920f8d17395c5807d1eb6e1119bcc51992b0d06
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\Console;
12 use Zend\EventManager\AbstractListenerAggregate;
13 use Zend\EventManager\EventManagerInterface;
14 use Zend\Mvc\Application;
15 use Zend\Mvc\MvcEvent;
16 use Zend\Stdlib\ResponseInterface as Response;
17 use Zend\View\Model\ConsoleModel;
19 class ExceptionStrategy extends AbstractListenerAggregate
21 /**
22 * Display exceptions?
23 * @var bool
25 protected $displayExceptions = true;
27 /**
28 * A template for message to show in console when an exception has occurred.
29 * @var string|callable
31 protected $message = <<<EOT
32 ======================================================================
33 The application has thrown an exception!
34 ======================================================================
35 :className
36 :message
37 ----------------------------------------------------------------------
38 :file::line
39 :stack
40 ======================================================================
41 Previous Exception(s):
42 :previous
44 EOT;
46 /**
47 * A template for message to show in console when an exception has previous exceptions.
48 * @var string
50 protected $previousMessage = <<<EOT
51 ======================================================================
52 :className
53 :message
54 ----------------------------------------------------------------------
55 :file::line
56 :stack
58 EOT;
60 /**
61 * {@inheritDoc}
63 public function attach(EventManagerInterface $events)
65 $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'prepareExceptionViewModel'));
66 $this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER_ERROR, array($this, 'prepareExceptionViewModel'));
69 /**
70 * Flag: display exceptions in error pages?
72 * @param bool $displayExceptions
73 * @return ExceptionStrategy
75 public function setDisplayExceptions($displayExceptions)
77 $this->displayExceptions = (bool) $displayExceptions;
78 return $this;
81 /**
82 * Should we display exceptions in error pages?
84 * @return bool
86 public function displayExceptions()
88 return $this->displayExceptions;
91 /**
92 * Get current template for message that will be shown in Console.
94 * @return string
96 public function getMessage()
98 return $this->message;
102 * Set template for message that will be shown in Console.
103 * The message can be a string (template) or a callable (i.e. a closure).
105 * The closure is expected to return a string and will be called with 2 parameters:
106 * Exception $exception - the exception being thrown
107 * boolean $displayExceptions - whether to display exceptions or not
109 * If the message is a string, one can use the following template params:
111 * :className - full class name of exception instance
112 * :message - exception message
113 * :code - exception code
114 * :file - the file where the exception has been thrown
115 * :line - the line where the exception has been thrown
116 * :stack - full exception stack
118 * @param string|callable $message
119 * @return ExceptionStrategy
121 public function setMessage($message)
123 $this->message = $message;
124 return $this;
128 * Sets template for previous message that will be shown in Console.
130 * @param string $previousMessage
131 * @return ExceptionStrategy
133 public function setPreviousMessage($previousMessage)
135 $this->previousMessage = $previousMessage;
136 return $this;
140 * @return callable|string
142 public function getPreviousMessage()
144 return $this->previousMessage;
148 * Create an exception view model, and set the HTTP status code
150 * @todo dispatch.error does not halt dispatch unless a response is
151 * returned. As such, we likely need to trigger rendering as a low
152 * priority dispatch.error event (or goto a render event) to ensure
153 * rendering occurs, and that munging of view models occurs when
154 * expected.
155 * @param MvcEvent $e
156 * @return void
158 public function prepareExceptionViewModel(MvcEvent $e)
160 // Do nothing if no error in the event
161 $error = $e->getError();
162 if (empty($error)) {
163 return;
166 // Do nothing if the result is a response object
167 $result = $e->getResult();
168 if ($result instanceof Response) {
169 return;
172 switch ($error) {
173 case Application::ERROR_CONTROLLER_NOT_FOUND:
174 case Application::ERROR_CONTROLLER_INVALID:
175 case Application::ERROR_ROUTER_NO_MATCH:
176 // Specifically not handling these because they are handled by routeNotFound strategy
177 return;
179 case Application::ERROR_EXCEPTION:
180 default:
181 // Prepare error message
182 $exception = $e->getParam('exception');
184 if (is_callable($this->message)) {
185 $callback = $this->message;
186 $message = (string) $callback($exception, $this->displayExceptions);
187 } elseif ($this->displayExceptions && $exception instanceof \Exception) {
188 $previous = '';
189 $previousException = $exception->getPrevious();
190 while($previousException) {
191 $previous .= str_replace(
192 array(
193 ':className',
194 ':message',
195 ':code',
196 ':file',
197 ':line',
198 ':stack',
199 ),array(
200 get_class($previousException),
201 $previousException->getMessage(),
202 $previousException->getCode(),
203 $previousException->getFile(),
204 $previousException->getLine(),
205 $exception->getTraceAsString(),
207 $this->previousMessage
209 $previousException = $previousException->getPrevious();
212 /* @var $exception \Exception */
213 $message = str_replace(
214 array(
215 ':className',
216 ':message',
217 ':code',
218 ':file',
219 ':line',
220 ':stack',
221 ':previous',
222 ),array(
223 get_class($exception),
224 $exception->getMessage(),
225 $exception->getCode(),
226 $exception->getFile(),
227 $exception->getLine(),
228 $exception->getTraceAsString(),
229 $previous
231 $this->message
233 } else {
234 $message = str_replace(
235 array(
236 ':className',
237 ':message',
238 ':code',
239 ':file',
240 ':line',
241 ':stack',
242 ':previous',
243 ),array(
252 $this->message
256 // Prepare view model
257 $model = new ConsoleModel();
258 $model->setResult($message);
259 $model->setErrorLevel(1);
261 // Inject it into MvcEvent
262 $e->setResult($model);
264 break;