PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Controller/Plugin/ErrorHandler.php

https://gitlab.com/devtoannh/cafe
PHP | 300 lines | 125 code | 29 blank | 146 comment | 16 complexity | 564b057417ca7151c883263a767913b2 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage Plugins
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Controller_Plugin_Abstract */
  22. require_once 'Zend/Controller/Plugin/Abstract.php';
  23. /**
  24. * Handle exceptions that bubble up based on missing controllers, actions, or
  25. * application errors, and forward to an error handler.
  26. *
  27. * @uses Zend_Controller_Plugin_Abstract
  28. * @category Zend
  29. * @package Zend_Controller
  30. * @subpackage Plugins
  31. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @version $Id: ErrorHandler.php 24241 2011-07-14 08:09:41Z bate $
  34. */
  35. class Zend_Controller_Plugin_ErrorHandler extends Zend_Controller_Plugin_Abstract
  36. {
  37. /**
  38. * Const - No controller exception; controller does not exist
  39. */
  40. const EXCEPTION_NO_CONTROLLER = 'EXCEPTION_NO_CONTROLLER';
  41. /**
  42. * Const - No action exception; controller exists, but action does not
  43. */
  44. const EXCEPTION_NO_ACTION = 'EXCEPTION_NO_ACTION';
  45. /**
  46. * Const - No route exception; no routing was possible
  47. */
  48. const EXCEPTION_NO_ROUTE = 'EXCEPTION_NO_ROUTE';
  49. /**
  50. * Const - Other Exception; exceptions thrown by application controllers
  51. */
  52. const EXCEPTION_OTHER = 'EXCEPTION_OTHER';
  53. /**
  54. * Module to use for errors; defaults to default module in dispatcher
  55. * @var string
  56. */
  57. protected $_errorModule;
  58. /**
  59. * Controller to use for errors; defaults to 'error'
  60. * @var string
  61. */
  62. protected $_errorController = 'error';
  63. /**
  64. * Action to use for errors; defaults to 'error'
  65. * @var string
  66. */
  67. protected $_errorAction = 'error';
  68. /**
  69. * Flag; are we already inside the error handler loop?
  70. * @var bool
  71. */
  72. protected $_isInsideErrorHandlerLoop = false;
  73. /**
  74. * Exception count logged at first invocation of plugin
  75. * @var int
  76. */
  77. protected $_exceptionCountAtFirstEncounter = 0;
  78. /**
  79. * Constructor
  80. *
  81. * Options may include:
  82. * - module
  83. * - controller
  84. * - action
  85. *
  86. * @param Array $options
  87. * @return void
  88. */
  89. public function __construct(Array $options = array())
  90. {
  91. $this->setErrorHandler($options);
  92. }
  93. /**
  94. * setErrorHandler() - setup the error handling options
  95. *
  96. * @param array $options
  97. * @return Zend_Controller_Plugin_ErrorHandler
  98. */
  99. public function setErrorHandler(Array $options = array())
  100. {
  101. if (isset($options['module'])) {
  102. $this->setErrorHandlerModule($options['module']);
  103. }
  104. if (isset($options['controller'])) {
  105. $this->setErrorHandlerController($options['controller']);
  106. }
  107. if (isset($options['action'])) {
  108. $this->setErrorHandlerAction($options['action']);
  109. }
  110. return $this;
  111. }
  112. /**
  113. * Set the module name for the error handler
  114. *
  115. * @param string $module
  116. * @return Zend_Controller_Plugin_ErrorHandler
  117. */
  118. public function setErrorHandlerModule($module)
  119. {
  120. $this->_errorModule = (string) $module;
  121. return $this;
  122. }
  123. /**
  124. * Retrieve the current error handler module
  125. *
  126. * @return string
  127. */
  128. public function getErrorHandlerModule()
  129. {
  130. if (null === $this->_errorModule) {
  131. $this->_errorModule = Zend_Controller_Front::getInstance()->getDispatcher()->getDefaultModule();
  132. }
  133. return $this->_errorModule;
  134. }
  135. /**
  136. * Set the controller name for the error handler
  137. *
  138. * @param string $controller
  139. * @return Zend_Controller_Plugin_ErrorHandler
  140. */
  141. public function setErrorHandlerController($controller)
  142. {
  143. $this->_errorController = (string) $controller;
  144. return $this;
  145. }
  146. /**
  147. * Retrieve the current error handler controller
  148. *
  149. * @return string
  150. */
  151. public function getErrorHandlerController()
  152. {
  153. return $this->_errorController;
  154. }
  155. /**
  156. * Set the action name for the error handler
  157. *
  158. * @param string $action
  159. * @return Zend_Controller_Plugin_ErrorHandler
  160. */
  161. public function setErrorHandlerAction($action)
  162. {
  163. $this->_errorAction = (string) $action;
  164. return $this;
  165. }
  166. /**
  167. * Retrieve the current error handler action
  168. *
  169. * @return string
  170. */
  171. public function getErrorHandlerAction()
  172. {
  173. return $this->_errorAction;
  174. }
  175. /**
  176. * Route shutdown hook -- Ccheck for router exceptions
  177. *
  178. * @param Zend_Controller_Request_Abstract $request
  179. */
  180. public function routeShutdown(Zend_Controller_Request_Abstract $request)
  181. {
  182. $this->_handleError($request);
  183. }
  184. /**
  185. * Pre dispatch hook -- check for exceptions and dispatch error handler if
  186. * necessary
  187. *
  188. * @param Zend_Controller_Request_Abstract $request
  189. */
  190. public function preDispatch(Zend_Controller_Request_Abstract $request)
  191. {
  192. $this->_handleError($request);
  193. }
  194. /**
  195. * Post dispatch hook -- check for exceptions and dispatch error handler if
  196. * necessary
  197. *
  198. * @param Zend_Controller_Request_Abstract $request
  199. */
  200. public function postDispatch(Zend_Controller_Request_Abstract $request)
  201. {
  202. $this->_handleError($request);
  203. }
  204. /**
  205. * Handle errors and exceptions
  206. *
  207. * If the 'noErrorHandler' front controller flag has been set,
  208. * returns early.
  209. *
  210. * @param Zend_Controller_Request_Abstract $request
  211. * @return void
  212. */
  213. protected function _handleError(Zend_Controller_Request_Abstract $request)
  214. {
  215. $frontController = Zend_Controller_Front::getInstance();
  216. if ($frontController->getParam('noErrorHandler')) {
  217. return;
  218. }
  219. $response = $this->getResponse();
  220. if ($this->_isInsideErrorHandlerLoop) {
  221. $exceptions = $response->getException();
  222. if (count($exceptions) > $this->_exceptionCountAtFirstEncounter) {
  223. // Exception thrown by error handler; tell the front controller to throw it
  224. $frontController->throwExceptions(true);
  225. throw array_pop($exceptions);
  226. }
  227. }
  228. // check for an exception AND allow the error handler controller the option to forward
  229. if (($response->isException()) && (!$this->_isInsideErrorHandlerLoop)) {
  230. $this->_isInsideErrorHandlerLoop = true;
  231. // Get exception information
  232. $error = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
  233. $exceptions = $response->getException();
  234. $exception = $exceptions[0];
  235. $exceptionType = get_class($exception);
  236. $error->exception = $exception;
  237. switch ($exceptionType) {
  238. case 'Zend_Controller_Router_Exception':
  239. if (404 == $exception->getCode()) {
  240. $error->type = self::EXCEPTION_NO_ROUTE;
  241. } else {
  242. $error->type = self::EXCEPTION_OTHER;
  243. }
  244. break;
  245. case 'Zend_Controller_Dispatcher_Exception':
  246. $error->type = self::EXCEPTION_NO_CONTROLLER;
  247. break;
  248. case 'Zend_Controller_Action_Exception':
  249. if (404 == $exception->getCode()) {
  250. $error->type = self::EXCEPTION_NO_ACTION;
  251. } else {
  252. $error->type = self::EXCEPTION_OTHER;
  253. }
  254. break;
  255. default:
  256. $error->type = self::EXCEPTION_OTHER;
  257. break;
  258. }
  259. // Keep a copy of the original request
  260. $error->request = clone $request;
  261. // get a count of the number of exceptions encountered
  262. $this->_exceptionCountAtFirstEncounter = count($exceptions);
  263. // Forward to the error handler
  264. $request->setParam('error_handler', $error)
  265. ->setModuleName($this->getErrorHandlerModule())
  266. ->setControllerName($this->getErrorHandlerController())
  267. ->setActionName($this->getErrorHandlerAction())
  268. ->setDispatched(false);
  269. }
  270. }
  271. }