PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/rsilveira1987/Expresso
PHP | 257 lines | 105 code | 25 blank | 127 comment | 13 complexity | 3a14398f42a0151e825dce62c46a592e 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-2009 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-2009 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 10020 2009-08-18 14:34:09Z j.fischer@metaways.de $
  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 - Other Exception; exceptions thrown by application controllers
  47. */
  48. const EXCEPTION_OTHER = 'EXCEPTION_OTHER';
  49. /**
  50. * Module to use for errors; defaults to default module in dispatcher
  51. * @var string
  52. */
  53. protected $_errorModule;
  54. /**
  55. * Controller to use for errors; defaults to 'error'
  56. * @var string
  57. */
  58. protected $_errorController = 'error';
  59. /**
  60. * Action to use for errors; defaults to 'error'
  61. * @var string
  62. */
  63. protected $_errorAction = 'error';
  64. /**
  65. * Flag; are we already inside the error handler loop?
  66. * @var bool
  67. */
  68. protected $_isInsideErrorHandlerLoop = false;
  69. /**
  70. * Exception count logged at first invocation of plugin
  71. * @var int
  72. */
  73. protected $_exceptionCountAtFirstEncounter = 0;
  74. /**
  75. * Constructor
  76. *
  77. * Options may include:
  78. * - module
  79. * - controller
  80. * - action
  81. *
  82. * @param Array $options
  83. * @return void
  84. */
  85. public function __construct(Array $options = array())
  86. {
  87. $this->setErrorHandler($options);
  88. }
  89. /**
  90. * setErrorHandler() - setup the error handling options
  91. *
  92. * @param array $options
  93. * @return Zend_Controller_Plugin_ErrorHandler
  94. */
  95. public function setErrorHandler(Array $options = array())
  96. {
  97. if (isset($options['module'])) {
  98. $this->setErrorHandlerModule($options['module']);
  99. }
  100. if (isset($options['controller'])) {
  101. $this->setErrorHandlerController($options['controller']);
  102. }
  103. if (isset($options['action'])) {
  104. $this->setErrorHandlerAction($options['action']);
  105. }
  106. return $this;
  107. }
  108. /**
  109. * Set the module name for the error handler
  110. *
  111. * @param string $module
  112. * @return Zend_Controller_Plugin_ErrorHandler
  113. */
  114. public function setErrorHandlerModule($module)
  115. {
  116. $this->_errorModule = (string) $module;
  117. return $this;
  118. }
  119. /**
  120. * Retrieve the current error handler module
  121. *
  122. * @return string
  123. */
  124. public function getErrorHandlerModule()
  125. {
  126. if (null === $this->_errorModule) {
  127. $this->_errorModule = Zend_Controller_Front::getInstance()->getDispatcher()->getDefaultModule();
  128. }
  129. return $this->_errorModule;
  130. }
  131. /**
  132. * Set the controller name for the error handler
  133. *
  134. * @param string $controller
  135. * @return Zend_Controller_Plugin_ErrorHandler
  136. */
  137. public function setErrorHandlerController($controller)
  138. {
  139. $this->_errorController = (string) $controller;
  140. return $this;
  141. }
  142. /**
  143. * Retrieve the current error handler controller
  144. *
  145. * @return string
  146. */
  147. public function getErrorHandlerController()
  148. {
  149. return $this->_errorController;
  150. }
  151. /**
  152. * Set the action name for the error handler
  153. *
  154. * @param string $action
  155. * @return Zend_Controller_Plugin_ErrorHandler
  156. */
  157. public function setErrorHandlerAction($action)
  158. {
  159. $this->_errorAction = (string) $action;
  160. return $this;
  161. }
  162. /**
  163. * Retrieve the current error handler action
  164. *
  165. * @return string
  166. */
  167. public function getErrorHandlerAction()
  168. {
  169. return $this->_errorAction;
  170. }
  171. /**
  172. * postDispatch() plugin hook -- check for exceptions and dispatch error
  173. * handler if necessary
  174. *
  175. * If the 'noErrorHandler' front controller flag has been set,
  176. * returns early.
  177. *
  178. * @param Zend_Controller_Request_Abstract $request
  179. * @return void
  180. */
  181. public function postDispatch(Zend_Controller_Request_Abstract $request)
  182. {
  183. $frontController = Zend_Controller_Front::getInstance();
  184. if ($frontController->getParam('noErrorHandler')) {
  185. return;
  186. }
  187. $response = $this->getResponse();
  188. if ($this->_isInsideErrorHandlerLoop) {
  189. $exceptions = $response->getException();
  190. if (count($exceptions) > $this->_exceptionCountAtFirstEncounter) {
  191. // Exception thrown by error handler; tell the front controller to throw it
  192. $frontController->throwExceptions(true);
  193. throw array_pop($exceptions);
  194. }
  195. }
  196. // check for an exception AND allow the error handler controller the option to forward
  197. if (($response->isException()) && (!$this->_isInsideErrorHandlerLoop)) {
  198. $this->_isInsideErrorHandlerLoop = true;
  199. // Get exception information
  200. $error = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
  201. $exceptions = $response->getException();
  202. $exception = $exceptions[0];
  203. $exceptionType = get_class($exception);
  204. $error->exception = $exception;
  205. switch ($exceptionType) {
  206. case 'Zend_Controller_Dispatcher_Exception':
  207. $error->type = self::EXCEPTION_NO_CONTROLLER;
  208. break;
  209. case 'Zend_Controller_Action_Exception':
  210. if (404 == $exception->getCode()) {
  211. $error->type = self::EXCEPTION_NO_ACTION;
  212. } else {
  213. $error->type = self::EXCEPTION_OTHER;
  214. }
  215. break;
  216. default:
  217. $error->type = self::EXCEPTION_OTHER;
  218. break;
  219. }
  220. // Keep a copy of the original request
  221. $error->request = clone $request;
  222. // get a count of the number of exceptions encountered
  223. $this->_exceptionCountAtFirstEncounter = count($exceptions);
  224. // Forward to the error handler
  225. $request->setParam('error_handler', $error)
  226. ->setModuleName($this->getErrorHandlerModule())
  227. ->setControllerName($this->getErrorHandlerController())
  228. ->setActionName($this->getErrorHandlerAction())
  229. ->setDispatched(false);
  230. }
  231. }
  232. }