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

/www/shop/engine/Library/Enlight/Extensions/ErrorHandler/Bootstrap.php

https://bitbucket.org/weberlars/sot-shopware
PHP | 287 lines | 128 code | 26 blank | 133 comment | 12 complexity | 3f9b682ba8b15411c976200a526701c8 MD5 | raw file
Possible License(s): AGPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * Enlight
  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://enlight.de/license
  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@shopware.de so we can send you a copy immediately.
  14. *
  15. * @category Enlight
  16. * @package Enlight_Extensions
  17. * @copyright Copyright (c) 2011, shopware AG (http://www.shopware.de)
  18. * @license http://enlight.de/license New BSD License
  19. * @version $Id$
  20. * @author Heiner Lohaus
  21. * @author $Author$
  22. */
  23. /**
  24. * Enlight error handler extension to log the exception into an array.
  25. *
  26. * The Enlight_Extensions_ErrorHandler_Bootstrap logs the exceptions into an array for further processing.
  27. * It uses the setErrorHandler function. If the same exception multiple thrown a count property will increased.
  28. *
  29. * @category Enlight
  30. * @package Enlight_Extensions
  31. * @copyright Copyright (c) 2011, shopware AG (http://www.shopware.de)
  32. * @license http://enlight.de/license New BSD License
  33. */
  34. class Enlight_Extensions_ErrorHandler_Bootstrap extends Enlight_Plugin_Bootstrap_Config
  35. {
  36. /**
  37. * @var callback Contains the original error handler
  38. * which will be set in the registerErrorHandler method.
  39. *
  40. */
  41. protected $origErrorHandler = null;
  42. /**
  43. * @var boolean Flag whether the error handler is already registered. Will be set in the
  44. * registerErrorHandler function.
  45. */
  46. protected $registeredErrorHandler = false;
  47. /**
  48. * @var array Contains all mapped error handlers
  49. */
  50. protected $errorHandlerMap = null;
  51. /**
  52. * @var int Contains the current error level. Used to set the error handler.
  53. */
  54. protected $errorLevel = null;
  55. /**
  56. * @var array Flag whether errors should be logged
  57. */
  58. protected $errorLog = false;
  59. /**
  60. * @var array Contains all logged errors.
  61. */
  62. protected $errorList = array();
  63. /**
  64. * @var array List of all error levels.
  65. */
  66. protected $errorLevelList = array(
  67. E_ERROR => 'E_ERROR',
  68. E_WARNING => 'E_WARNING',
  69. E_PARSE => 'E_PARSE',
  70. E_NOTICE => 'E_NOTICE',
  71. E_CORE_ERROR => 'E_CORE_ERROR',
  72. E_CORE_WARNING => 'E_CORE_WARNING',
  73. E_COMPILE_ERROR => 'E_COMPILE_ERROR',
  74. E_COMPILE_WARNING => 'E_COMPILE_WARNING',
  75. E_USER_ERROR => 'E_USER_ERROR',
  76. E_USER_WARNING => 'E_USER_WARNING',
  77. E_USER_NOTICE => 'E_USER_NOTICE',
  78. E_ALL => 'E_ALL',
  79. E_STRICT => 'E_STRICT',
  80. E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
  81. //E_DEPRECATED => 'E_DEPRECATED',
  82. //E_USER_DEPRECATED => 'E_USER_DEPRECATED',
  83. );
  84. /**
  85. * Initial the error level list.
  86. * @return void
  87. */
  88. public function init()
  89. {
  90. if (defined('E_DEPRECATED')) {
  91. $this->errorLevelList[E_DEPRECATED] = 'E_DEPRECATED';
  92. }
  93. if (defined('E_USER_DEPRECATED')) {
  94. $this->errorLevelList[E_USER_DEPRECATED] = 'E_USER_DEPRECATED';
  95. }
  96. }
  97. /**
  98. * Plugin install method. Subscribes the Enlight_Controller_Front_StartDispatch
  99. * event to register the error handler.
  100. *
  101. * @return bool success
  102. */
  103. public function install()
  104. {
  105. $this->subscribeEvent(
  106. 'Enlight_Controller_Front_StartDispatch',
  107. 'onStartDispatch'
  108. );
  109. return true;
  110. }
  111. /**
  112. * Listener method of the Enlight_Controller_Front_StartDispatch event.
  113. * Calls the internal function "registerErrorHandler" to register the error handler.
  114. */
  115. public function onStartDispatch()
  116. {
  117. $this->registerErrorHandler();
  118. }
  119. /**
  120. * Register error handler callback method.
  121. *
  122. * @link http://www.php.net/manual/en/function.set-error-handler.php Custom error handler
  123. * @param int $errorLevel
  124. * @return Enlight_Extensions_ErrorHandler_Bootstrap
  125. */
  126. public function registerErrorHandler($errorLevel = null)
  127. {
  128. if ($errorLevel === null) {
  129. $errorLevel = E_ALL | E_STRICT;
  130. }
  131. // Only register once. Avoids loop issues if it gets registered twice.
  132. if ($this->registeredErrorHandler
  133. && $errorLevel === $this->errorLevel) {
  134. return $this;
  135. }
  136. if (isset($this->errorLevel) && isset($this->origErrorHandler)) {
  137. set_error_handler(array($this, 'errorHandler'), $errorLevel);
  138. } else {
  139. $this->origErrorHandler = set_error_handler(array($this, 'errorHandler'), $errorLevel);
  140. }
  141. $this->errorLevel = $errorLevel;
  142. $this->registeredErrorHandler = true;
  143. return $this;
  144. }
  145. /**
  146. * Error Handler will convert error into log message, and then call the original error handler
  147. *
  148. * @link http://www.php.net/manual/en/function.set-error-handler.php Custom error handler
  149. * @param int $errorLevel
  150. * @param string $errorMessage
  151. * @param string $errorFile
  152. * @param int $errorLine
  153. * @param array $errorContext
  154. * @return bool
  155. */
  156. public function errorHandler($errorLevel, $errorMessage, $errorFile, $errorLine, $errorContext)
  157. {
  158. if ($this->errorLog) {
  159. $hashId = md5($errorLevel . $errorMessage . $errorFile . $errorLine);
  160. if (!isset($this->errorList[$hashId])) {
  161. $errorName = isset($this->errorLevelList[$errorLevel]) ? $this->errorLevelList[$errorLevel] : '';
  162. $this->errorList[$hashId] = array(
  163. 'count' => 1,
  164. 'code' => $errorLevel,
  165. 'name' => $errorName,
  166. 'message' => $errorMessage,
  167. 'line' => $errorLine,
  168. 'file' => $errorFile
  169. );
  170. } else {
  171. ++$this->errorList[$hashId]['count'];
  172. }
  173. }
  174. //throw new ErrorException($errorMessage, 0, $errorLevel, $errorFile, $errorLine);
  175. if ($this->origErrorHandler !== null) {
  176. return call_user_func(
  177. $this->origErrorHandler,
  178. $errorLevel,
  179. $errorMessage,
  180. $errorFile,
  181. $errorLine,
  182. $errorContext
  183. );
  184. }
  185. return true;
  186. }
  187. /**
  188. * Getter method for the errorList property. Contains all logged errors.
  189. *
  190. * @return array
  191. */
  192. public function getErrorLog()
  193. {
  194. return $this->errorList;
  195. }
  196. /**
  197. * Setter method for the errorLog property. The errorLog is a flag whether errors should be logged.
  198. *
  199. * @param bool $value
  200. * @return Shopware_Plugins_Core_ErrorHandler_Bootstrap
  201. */
  202. public function setEnabledLog($value = true)
  203. {
  204. $this->errorLog = $value ? true : false;
  205. return $this;
  206. }
  207. /**
  208. * Getter method for the errorLog property.
  209. *
  210. * @return array|bool
  211. */
  212. public function isEnabledLog()
  213. {
  214. return $this->errorLog;
  215. }
  216. /**
  217. * Returns the original error handler. The original error handler will
  218. * be set in the registerErrorHandler method
  219. *
  220. * @return callback|null
  221. */
  222. public function getOrigErrorHandler()
  223. {
  224. return $this->origErrorHandler;
  225. }
  226. /**
  227. * Getter method for the registeredErrorHandler property.
  228. * Flag whether the error handler is already registered.
  229. *
  230. * @return bool
  231. */
  232. public function isRegisteredErrorHandler()
  233. {
  234. return $this->registeredErrorHandler;
  235. }
  236. /**
  237. * Getter method for the errorLevelList property. Contains all error levels.
  238. * @return array
  239. */
  240. public function getErrorLevelList()
  241. {
  242. return $this->errorLevelList;
  243. }
  244. /**
  245. * Returns the current registered error level.
  246. * @return array|int
  247. */
  248. public function getErrorLevel()
  249. {
  250. return $this->errorLevel;
  251. }
  252. /**
  253. * Setter method for the original error handler method.
  254. * @access private
  255. * @param $handler
  256. */
  257. public function setOrigErrorHandler($handler)
  258. {
  259. $this->origErrorHandler = $handler;
  260. }
  261. }