PageRenderTime 61ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/weberlars/sot-shopware
PHP | 306 lines | 167 code | 28 blank | 111 comment | 15 complexity | 2c9b8763e956136f6b7707da489d343d 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 Debug
  27. * @copyright Copyright (c) 2012, shopware AG (http://www.shopware.de)
  28. * @version $Id$
  29. * @author $Author$
  30. */
  31. /**
  32. * Debug extension to support various writer.
  33. *
  34. * The log extension sets the log resource available.
  35. * It supports various writer, for example firebug, database tables and log files.
  36. * In additionally the Enlight_Extensions_Log_Bootstrap support to log the ip and the user agents.
  37. */
  38. class Shopware_Plugins_Core_Debug_Bootstrap extends Shopware_Components_Plugin_Bootstrap
  39. {
  40. /**
  41. * @var Enlight_Components_Log Contains an instance of the Enlight_Components_Log
  42. */
  43. protected $log;
  44. /**
  45. * Plugin install method
  46. * @return bool
  47. */
  48. public function install()
  49. {
  50. //todo@hl Change to Enlight_Controller_Front_RouteStartup event. Add block ip support
  51. $this->subscribeEvent(
  52. 'Enlight_Controller_Front_StartDispatch',
  53. 'onStartDispatch'
  54. );
  55. $form = $this->Form();
  56. $parent = $this->Forms()->findOneBy(array('name' => 'Core'));
  57. $form->setParent($parent);
  58. $form->setElement('text', 'AllowIP', array('label' => 'Auf IP beschr?¤nken', 'value' => ''));
  59. return true;
  60. }
  61. /**
  62. * Setter method for the log property. If no log are given the log resource of the
  63. * Enlight_Plugin_Namespace will be used.
  64. *
  65. * @param Enlight_Components_Log|Zend_Log $log
  66. */
  67. public function setLog(Zend_Log $log = null)
  68. {
  69. if ($log === null) {
  70. $log = $this->Collection()->Log()->Resource();
  71. }
  72. $this->log = $log;
  73. }
  74. /**
  75. * Getter method of the log property. If the log isn't set the log property will be initial over the
  76. * setLog method, which will use the log resource of the Enlight_Plugin_Namespace
  77. *
  78. * @return Enlight_Components_Log
  79. */
  80. public function Log()
  81. {
  82. if ($this->log === null) {
  83. $this->setLog();
  84. }
  85. return $this->log;
  86. }
  87. /**
  88. * Listener method of the Enlight_Controller_Front_StartDispatch event.
  89. * Registers the error handler of the Enlight_Plugin_Namespace.
  90. *
  91. * @param Enlight_Event_EventArgs $args
  92. */
  93. public function onStartDispatch(Enlight_Event_EventArgs $args)
  94. {
  95. // $request = $args->getRequest();
  96. // if ($request->getClientIp(false)
  97. // && !empty($this->Config()->allowIp)
  98. // && strpos($this->Config()->allowIp, $request->getClientIp(false))===false){
  99. // return;
  100. // }
  101. if($this->Log() === null){
  102. return;
  103. }
  104. if(!empty($_SERVER['HTTP_USER_AGENT'])
  105. && strpos($_SERVER['HTTP_USER_AGENT'], 'FirePHP/')!==false) {
  106. $writer = new Zend_Log_Writer_Firebug();
  107. $this->Log()->addWriter($writer);
  108. }
  109. /** @var $errorHandler Enlight_Extensions_ErrorHandler_Bootstrap */
  110. $errorHandler = $this->Collection()->ErrorHandler();
  111. $errorHandler->setEnabledLog(true);
  112. $errorHandler->registerErrorHandler(E_ALL | E_STRICT);
  113. $event = new Enlight_Event_EventHandler(
  114. 'Enlight_Controller_Front_DispatchLoopShutdown',
  115. array($this, 'onDispatchLoopShutdown')
  116. );
  117. Shopware()->Events()->registerListener($event);
  118. $event = new Enlight_Event_EventHandler(
  119. 'Enlight_Plugins_ViewRenderer_PreRender',
  120. array($this, 'onAfterRenderView')
  121. );
  122. Shopware()->Events()->registerListener($event);
  123. }
  124. /**
  125. * Listener method of the Enlight_Plugins_ViewRenderer_PostRender event.
  126. * Logs the template of the given Enlight_Event_EventArgs.
  127. *
  128. * @param Enlight_Event_EventArgs $args
  129. */
  130. public function onAfterRenderView(Enlight_Event_EventArgs $args)
  131. {
  132. $template = $args->getTemplate();
  133. $this->logTemplate($template);
  134. }
  135. /**
  136. * Listener method of the Enlight_Controller_Front_DispatchLoopShutdown event.
  137. * Logs the error handler and the response of the Enlight_Event_EventArgs.
  138. *
  139. * @param Enlight_Event_EventArgs $args
  140. */
  141. public function onDispatchLoopShutdown(Enlight_Event_EventArgs $args)
  142. {
  143. if (!$this->Log()) {
  144. return;
  145. }
  146. //$template = $this->Application()->Template();
  147. //$this->logTemplate($template);
  148. $errorHandler = $this->Collection()->ErrorHandler();
  149. $this->logError($errorHandler);
  150. $response = $args->getSubject()->Response();
  151. $this->logException($response);
  152. }
  153. /**
  154. * Iterate all logged errors of the given error handler and write them
  155. * into the internal log object.
  156. *
  157. * @param Enlight_Extensions_ErrorHandler_Bootstrap $errorHandler
  158. */
  159. public function logError($errorHandler)
  160. {
  161. $errors = $errorHandler->getErrorLog();
  162. if (empty($errors)) {
  163. return;
  164. }
  165. $counts = array();
  166. foreach ($errors as $errorKey => $error) {
  167. $counts[$errorKey] = $error['count'];
  168. }
  169. array_multisort($counts, SORT_NUMERIC, SORT_DESC, $errors);
  170. $rows = array();
  171. foreach ($errors as $error) {
  172. if (!$rows) {
  173. $rows[] = array_keys($error);
  174. }
  175. $rows[] = $this->encode(array_values($error));
  176. }
  177. $table = array('Error Log (' . count($errors) . ')', $rows);
  178. $this->Log()->table($table);
  179. }
  180. /**
  181. * Iterate all template and config variables of the given template object and write them
  182. * into the internal log object.
  183. *
  184. * @param Enlight_Template_Default|Enlight_Template_Manager $template
  185. */
  186. public function logTemplate($template)
  187. {
  188. $template_name = isset($template->template_resource) ? $template->template_resource : 'Global';
  189. $template_name = $this->encode($template_name, 30);
  190. $template_vars = (array) $template->getTemplateVars();
  191. unset($template_vars['smarty']);
  192. if (!empty($template_vars)) {
  193. $rows = array(array('spec', 'value'));
  194. foreach ($template_vars as $template_spec => $template_var) {
  195. $template_var = $this->encode($template_var);
  196. $rows[] = array($template_spec, $template_var);
  197. }
  198. $table = array('Template Vars > ' . $template_name . ' (' . (count($template_vars)) . ')', $rows);
  199. try {
  200. $this->Log()->table($table);
  201. } catch(Exception $e) {
  202. die((string) $e);
  203. }
  204. }
  205. $config_vars = (array) $template->getConfigVars();
  206. if (!empty($config_vars)) {
  207. $rows = array(array('spec', 'value'));
  208. foreach ($config_vars as $config_spec => $config_var) {
  209. $rows[] = array($config_spec, $config_var);
  210. }
  211. $table = array('Config Vars > ' . $template_name . ' (' . (count($config_vars)) . ')', $rows);
  212. $this->Log()->table($table);
  213. }
  214. }
  215. /**
  216. * Iterate all exceptions of the given response object and write them into internal log object.
  217. *
  218. * @param Enlight_Controller_Response_ResponseHttp $response
  219. */
  220. public function logException($response)
  221. {
  222. $exceptions = $response->getException();
  223. if (empty($exceptions)) {
  224. return;
  225. }
  226. $rows = array(array('code', 'name', 'message', 'line', 'file', 'trace'));
  227. /** @var $exception Exception */
  228. foreach ($exceptions as $exception) {
  229. $rows[] = $this->encode(array(
  230. $exception->getCode(),
  231. get_class($exception),
  232. $exception->getMessage(),
  233. $exception->getLine(),
  234. $exception->getFile(),
  235. explode("\n", $exception->getTraceAsString())
  236. ));
  237. }
  238. $table = array('Exception Log (' . count($exceptions) . ')', $rows);
  239. $this->log->table($table);
  240. foreach ($exceptions as $exception) {
  241. $this->log->err((string) $exception);
  242. }
  243. }
  244. /**
  245. * Encode data method
  246. *
  247. * @param $data
  248. * @param int $length
  249. * @return array|string
  250. */
  251. public function encode($data, $length = 250)
  252. {
  253. if (is_array($data)) {
  254. foreach ($data as $key => $value) {
  255. unset($data[$key]);
  256. $data[$this->encode($key)] = $this->encode($value);
  257. }
  258. } elseif (is_string($data)) {
  259. if (strlen($data) > $length) {
  260. $data = substr($data, 0, $length - 3) . '...';
  261. }
  262. //$data = utf8_encode($data);
  263. } elseif ($data instanceof ArrayObject) {
  264. /** @var $data ArrayObject */
  265. $data = $this->encode($data->getArrayCopy());
  266. } elseif ($data instanceof Zend_Config) {
  267. /** @var $data Zend_Config */
  268. $data = $this->encode($data->toArray());
  269. } elseif (method_exists($data, '__toArray') || $data instanceof stdClass) {
  270. $data = $this->encode((array) $data);
  271. } elseif(is_object($data)) {
  272. $data = $data instanceof Enlight_Hook_Proxy ? get_parent_class($data) : get_class($data);
  273. } else {
  274. $data = (string) $data;
  275. }
  276. return $data;
  277. }
  278. }