PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/weberlars/sot-shopware
PHP | 281 lines | 146 code | 23 blank | 112 comment | 10 complexity | 9c1deca03129ff6e4d9e660261ef0b17 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 debug extension to log exceptions and display the logged exceptions.
  25. *
  26. * The Enlight_Extensions_Debug_Bootstrap allows to debug the enlight application.
  27. * It logs the exceptions into the log and allows to output them into the console or write them into the database
  28. * or into log files.
  29. *
  30. * @category Enlight
  31. * @package Enlight_Extensions
  32. * @copyright Copyright (c) 2011, shopware AG (http://www.shopware.de)
  33. * @license http://enlight.de/license New BSD License
  34. */
  35. class Enlight_Extensions_Debug_Bootstrap extends Enlight_Plugin_Bootstrap_Config
  36. {
  37. /**
  38. * @var Enlight_Components_Log Contains an instance of the Enlight_Components_Log
  39. */
  40. protected $log;
  41. /**
  42. * Plugin install method.
  43. * Subscribes the Enlight_Controller_Front_StartDispatch event to register the error handler,
  44. * the Enlight_Controller_Front_DispatchLoopShutdown event to log the error handler and the exception,
  45. * the Enlight_Plugins_ViewRenderer_PostRender event to log the template.
  46. *
  47. * @return bool
  48. */
  49. public function install()
  50. {
  51. $this->subscribeEvent(
  52. 'Enlight_Controller_Front_StartDispatch',
  53. 'onStartDispatch'
  54. );
  55. $this->subscribeEvent(
  56. 'Enlight_Controller_Front_DispatchLoopShutdown',
  57. 'onDispatchLoopShutdown'
  58. );
  59. $this->subscribeEvent(
  60. 'Enlight_Plugins_ViewRenderer_PostRender',
  61. 'onAfterRenderView'
  62. );
  63. return true;
  64. }
  65. /**
  66. * Setter method for the log property. If no log are given the log resource of the
  67. * Enlight_Plugin_Namespace will be used.
  68. *
  69. * @param Enlight_Components_Log|Zend_Log $log
  70. */
  71. public function setLog(Zend_Log $log = null)
  72. {
  73. if ($log === null) {
  74. $log = $this->Collection()->Log()->Resource();
  75. }
  76. $this->log = $log;
  77. }
  78. /**
  79. * Getter method of the log property. If the log isn't set the log property will be initial over the
  80. * setLog method, which will use the log resource of the Enlight_Plugin_Namespace
  81. *
  82. * @return Enlight_Components_Log
  83. */
  84. public function Log()
  85. {
  86. if ($this->log === null) {
  87. $this->setLog();
  88. }
  89. return $this->log;
  90. }
  91. /**
  92. * Listener method of the Enlight_Controller_Front_StartDispatch event.
  93. * Registers the error handler of the Enlight_Plugin_Namespace.
  94. *
  95. * @param Enlight_Event_EventArgs $args
  96. */
  97. public function onStartDispatch(Enlight_Event_EventArgs $args)
  98. {
  99. /*
  100. $request = $args->getSubject()->Request();
  101. if ($request->getClientIp(false)
  102. && !empty($config->allowIp)
  103. && strpos($config->allowIp, $request->getClientIp(false))===false){
  104. return;
  105. }
  106. */
  107. /** @var $errorHandler Enlight_Extensions_ErrorHandler_Bootstrap */
  108. $errorHandler = $this->Collection()->ErrorHandler();
  109. $errorHandler->setEnabledLog(true);
  110. $errorHandler->registerErrorHandler(E_ALL | E_STRICT);
  111. }
  112. /**
  113. * Listener method of the Enlight_Plugins_ViewRenderer_PostRender event.
  114. * Logs the template of the given Enlight_Event_EventArgs.
  115. *
  116. * @param Enlight_Event_EventArgs $args
  117. */
  118. public function onAfterRenderView(Enlight_Event_EventArgs $args)
  119. {
  120. $template = $args->getTemplate();
  121. $this->logTemplate($template);
  122. }
  123. /**
  124. * Listener method of the Enlight_Controller_Front_DispatchLoopShutdown event.
  125. * Logs the error handler and the response of the Enlight_Event_EventArgs.
  126. *
  127. * @param Enlight_Event_EventArgs $args
  128. */
  129. public function onDispatchLoopShutdown(Enlight_Event_EventArgs $args)
  130. {
  131. if (!$this->Log()) {
  132. return;
  133. }
  134. //$template = $this->Application()->Template();
  135. //$this->logTemplate($template);
  136. $errorHandler = $this->Collection()->ErrorHandler();
  137. $this->logError($errorHandler);
  138. $response = $args->getSubject()->Response();
  139. $this->logException($response);
  140. }
  141. /**
  142. * Iterate all logged errors of the given error handler and write them
  143. * into the internal log object.
  144. *
  145. * @param Enlight_Extensions_ErrorHandler_Bootstrap $errorHandler
  146. */
  147. public function logError($errorHandler)
  148. {
  149. $errors = $errorHandler->getErrorLog();
  150. if (empty($errors)) {
  151. return;
  152. }
  153. $counts = array();
  154. foreach ($errors as $errorKey => $error) {
  155. $counts[$errorKey] = $error['count'];
  156. }
  157. array_multisort($counts, SORT_NUMERIC, SORT_DESC, $errors);
  158. $rows = array();
  159. foreach ($errors as $error) {
  160. if (!$rows) {
  161. $rows[] = array_keys($error);
  162. }
  163. $rows[] = array_values($error);
  164. }
  165. $table = array('Error Log (' . count($errors) . ')', $rows);
  166. $this->Log()->table($table);
  167. }
  168. /**
  169. * Iterate all template and config variables of the given template object and write them
  170. * into the internal log object.
  171. *
  172. * @param Enlight_Template_Default|Enlight_Template_Manager $template
  173. */
  174. public function logTemplate($template)
  175. {
  176. $template_name = isset($template->template_resource) ? $template->template_resource : 'Global';
  177. $template_name = $this->encode($template_name);
  178. $template_vars = (array) $template->getTemplateVars();
  179. unset($template_vars['smarty']);
  180. if (!empty($template_vars)) {
  181. $rows = array(array('spec', 'value'));
  182. foreach ($template_vars as $template_spec => $template_var) {
  183. $template_var = $this->encode($template_var);
  184. $rows[] = array($template_spec, $template_var);
  185. }
  186. $table = array('Template Vars > ' . $template_name . ' (' . (count($template_vars)) . ')', $rows);
  187. $this->Log()->table($table);
  188. }
  189. $config_vars = (array) $template->getConfigVars();
  190. if (!empty($config_vars)) {
  191. $rows = array(array('spec', 'value'));
  192. foreach ($config_vars as $config_spec => $config_var) {
  193. $rows[] = array($config_spec, $config_var);
  194. }
  195. $table = array('Config Vars > ' . $template_name . ' (' . (count($config_vars)) . ')', $rows);
  196. $this->Log()->table($table);
  197. }
  198. }
  199. /**
  200. * Iterate all exceptions of the given response object and write them into internal log object.
  201. *
  202. * @param Enlight_Controller_Response_ResponseHttp $response
  203. */
  204. public function logException($response)
  205. {
  206. $exceptions = $response->getException();
  207. if (empty($exceptions)) {
  208. return;
  209. }
  210. $rows = array(array('code', 'name', 'message', 'line', 'file', 'trace'));
  211. /** @var $exception Exception */
  212. foreach ($exceptions as $exception) {
  213. $rows[] = array(
  214. $exception->getCode(),
  215. get_class($exception),
  216. $exception->getMessage(),
  217. $exception->getLine(),
  218. $exception->getFile(),
  219. explode("\n", $exception->getTraceAsString())
  220. );
  221. }
  222. $table = array('Exception Log (' . count($exceptions) . ')', $rows);
  223. $this->log->table($table);
  224. foreach ($exceptions as $exception) {
  225. $this->log->err((string)$exception);
  226. }
  227. }
  228. /**
  229. * Encode data method
  230. *
  231. * @param $data
  232. * @return array|string
  233. */
  234. public function encode($data)
  235. {
  236. if (is_array($data)) {
  237. foreach ($data as $key => $value) {
  238. unset($data[$key]);
  239. $data[$this->encode($key)] = $this->encode($value);
  240. }
  241. } elseif (is_string($data)) {
  242. if (strlen($data) > 250) {
  243. $data = substr($data, 0, 250) . '...';
  244. }
  245. $data = utf8_encode($data);
  246. } elseif ($data instanceof ArrayObject) {
  247. /** @var $data ArrayObject */
  248. $data = $this->encode($data->getArrayCopy());
  249. } elseif ($data instanceof Zend_Config) {
  250. /** @var $data Zend_Config */
  251. $data = $this->encode($data->toArray());
  252. } elseif (is_object($data)) {
  253. $data = get_class($data);
  254. }
  255. return $data;
  256. }
  257. }