PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/www/shop/engine/Shopware/Plugins/Default/Core/ErrorHandler/Bootstrap.php

https://bitbucket.org/weberlars/sot-shopware
PHP | 220 lines | 110 code | 18 blank | 92 comment | 6 complexity | 03893ff4a87947aae85e5de9893c6979 MD5 | raw file
Possible License(s): AGPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * Shopware 4.0
  4. * Copyright Š 2012 shopware AG
  5. *
  6. * According to our dual licensing model, this program can be used either
  7. * under the terms of the GNU Affero General Public License, version 3,
  8. * or under a proprietary license.
  9. *
  10. * The texts of the GNU Affero General Public License with an additional
  11. * permission and of our proprietary license can be found at and
  12. * in the LICENSE file you have received along with this program.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * "Shopware" is a registered trademark of shopware AG.
  20. * The licensing of the program under the AGPLv3 does not imply a
  21. * trademark license. Therefore any rights, title and interest in
  22. * our trademarks remain entirely with us.
  23. *
  24. * @category Shopware
  25. * @package Shopware_Plugins
  26. * @subpackage ErrorHandler
  27. * @copyright Copyright (c) 2012, shopware AG (http://www.shopware.de)
  28. * @version $Id$
  29. * @author $Author$
  30. * @author Heiner Lohaus
  31. */
  32. /**
  33. * Shopware Error Handler
  34. *
  35. * todo@all: Documentation
  36. */
  37. class Shopware_Plugins_Core_ErrorHandler_Bootstrap extends Shopware_Components_Plugin_Bootstrap
  38. {
  39. /**
  40. * Plugin install method
  41. */
  42. public function install()
  43. {
  44. $event = $this->createEvent(
  45. 'Enlight_Controller_Front_StartDispatch',
  46. 'onStartDispatch'
  47. );
  48. $this->subscribeEvent($event);
  49. return true;
  50. }
  51. /**
  52. * Plugin event method
  53. *
  54. * @param Enlight_Event_EventArgs $args
  55. */
  56. public function onStartDispatch($args)
  57. {
  58. $this->registerErrorHandler(E_ALL | E_STRICT);
  59. }
  60. /**
  61. * @var callback
  62. */
  63. protected $_origErrorHandler = null;
  64. /**
  65. * @var boolean
  66. */
  67. protected $_registeredErrorHandler = false;
  68. /**
  69. * @var array
  70. */
  71. protected $_errorHandlerMap = null;
  72. /**
  73. * @var array
  74. */
  75. protected $_errorLevel = 0;
  76. /**
  77. * @var array
  78. */
  79. protected $_errorLog = false;
  80. /**
  81. * @var array
  82. */
  83. protected $_errorList = array();
  84. protected $_errorLevelList = array(
  85. E_ERROR => 'E_ERROR',
  86. E_WARNING => 'E_WARNING',
  87. E_PARSE => 'E_PARSE',
  88. E_NOTICE => 'E_NOTICE',
  89. E_CORE_ERROR => 'E_CORE_ERROR',
  90. E_CORE_WARNING => 'E_CORE_WARNING',
  91. E_COMPILE_ERROR => 'E_COMPILE_ERROR',
  92. E_COMPILE_WARNING => 'E_COMPILE_WARNING',
  93. E_USER_ERROR => 'E_USER_ERROR',
  94. E_USER_WARNING => 'E_USER_WARNING',
  95. E_USER_NOTICE => 'E_USER_NOTICE',
  96. E_ALL => 'E_ALL',
  97. E_STRICT => 'E_STRICT',
  98. E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
  99. 8192 => 'E_DEPRECATED',
  100. 16384 => 'E_USER_DEPRECATED',
  101. );
  102. /**
  103. * Register error handler callback
  104. *
  105. * @link http://www.php.net/manual/en/function.set-error-handler.php Custom error handler
  106. * @param int $errorLevel
  107. */
  108. public function registerErrorHandler($errorLevel = E_ALL)
  109. {
  110. // Only register once. Avoids loop issues if it gets registered twice.
  111. if ($this->_registeredErrorHandler) {
  112. return $this;
  113. }
  114. $this->_origErrorHandler = set_error_handler(array($this, 'errorHandler'), $errorLevel);
  115. $this->_registeredErrorHandler = true;
  116. return $this;
  117. }
  118. /**
  119. * Error Handler will convert error into log message, and then call the original error handler
  120. *
  121. * @link http://www.php.net/manual/en/function.set-error-handler.php Custom error handler
  122. * @param int $errno
  123. * @param string $errstr
  124. * @param string $errfile
  125. * @param int $errline
  126. * @param array $errcontext
  127. * @return boolean
  128. */
  129. public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
  130. {
  131. if ($this->_errorLog) {
  132. $hash_id = md5($errno . $errstr . $errfile . $errline);
  133. if (!isset($this->_errorList[$hash_id])) {
  134. $errna = isset($this->_errorLevelList[$errno]) ? $this->_errorLevelList[$errno] : '';
  135. $this->_errorList[$hash_id] = array(
  136. 'count' => 1,
  137. 'code' => $errno,
  138. 'name' => $errna,
  139. 'message' => $errstr,
  140. 'line' => $errline,
  141. 'file' => $errfile
  142. );
  143. } else {
  144. ++$this->_errorList[$hash_id]['count'];
  145. }
  146. }
  147. switch ($errno) {
  148. case 0:
  149. case E_NOTICE:
  150. case E_WARNING:
  151. case E_USER_NOTICE:
  152. case E_RECOVERABLE_ERROR:
  153. case E_STRICT:
  154. case defined('E_DEPRECATED') ? E_DEPRECATED : 0:
  155. case defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : 0:
  156. break;
  157. case E_CORE_WARNING:
  158. case E_USER_WARNING:
  159. case E_ERROR:
  160. case E_USER_ERROR:
  161. case E_CORE_ERROR:
  162. break;
  163. default:
  164. throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  165. break;
  166. }
  167. if ($this->_origErrorHandler !== null) {
  168. return call_user_func($this->_origErrorHandler, $errno, $errstr, $errfile, $errline, $errcontext);
  169. }
  170. return true;
  171. }
  172. /**
  173. * Returns error log list
  174. *
  175. * @return array
  176. */
  177. public function getErrorLog()
  178. {
  179. return $this->_errorList;
  180. }
  181. /**
  182. * Sets enabled log flag
  183. *
  184. * @param bool $value
  185. */
  186. public function setEnabledLog($value = true)
  187. {
  188. $this->_errorLog = $value ? true : false;
  189. return $this;
  190. }
  191. /**
  192. * Returns plugin capabilities
  193. */
  194. public function getCapabilities()
  195. {
  196. return array(
  197. 'install' => false,
  198. 'enable' => false,
  199. 'update' => true
  200. );
  201. }
  202. }