PageRenderTime 49ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/weberlars/sot-shopware
PHP | 204 lines | 69 code | 15 blank | 120 comment | 6 complexity | 764786554fe93d3868c588f03a2ba454 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 log extension to support various writer.
  25. *
  26. * The Enlight_Extensions_Log_Bootstrap sets the log resource available.
  27. * It supports various writer, for example firebug, database tables and log files.
  28. * In additionally the Enlight_Extensions_Log_Bootstrap support to log the ip and the user agents.
  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_Log_Bootstrap extends Enlight_Plugin_Bootstrap_Config
  36. {
  37. /**
  38. * @var Zend_Wildfire_Channel_HttpHeaders Contains an instance of the Zend_Wildfire_Channel_HttpHeaders
  39. */
  40. protected $channel;
  41. /**
  42. * @var Enlight_Components_Log Contains an instance of the Enlight_Components_Log.
  43. */
  44. protected $log;
  45. /**
  46. * Installs the log extension plugin.
  47. * Subscribes the init resource event to initial the log resource,
  48. * the Enlight_Controller_Front_RouteStartup event to startup the routing process and
  49. * the Enlight_Controller_Front_DispatchLoopShutdown event to flush the wildfire channel.
  50. *
  51. * @return bool
  52. */
  53. public function install()
  54. {
  55. $this->subscribeEvent('Enlight_Bootstrap_InitResource_Log', 'onInitResourceLog');
  56. $this->subscribeEvent('Enlight_Controller_Front_RouteStartup', 'onRouteStartup');
  57. $this->subscribeEvent('Enlight_Controller_Front_DispatchLoopShutdown', 'onDispatchLoopShutdown', 500);
  58. /*array(
  59. 'writerName' => 'Db',
  60. 'writerParams' => array(
  61. 'table' => 'log',
  62. 'db' => $this->Application()->Db(),
  63. 'columnMap' => array(
  64. 'priority' => 'priorityName',
  65. 'message' => 'message',
  66. 'date' => 'timestamp',
  67. 'remote_address' => 'remote_address',
  68. 'user_agent' => 'user_agent',
  69. )
  70. ),
  71. 'filterName' => 'Priority',
  72. 'filterParams' => array(
  73. 'priority' => Enlight_Components_Log::ERR
  74. )
  75. )
  76. array(
  77. 'writerName' => 'Mail',
  78. 'writerParams' => array(
  79. //'mail' => '',
  80. 'from' => 'info@shopware.de',
  81. 'to' => 'info@shopware.de',
  82. 'subjectPrependText' => 'Fehler: '
  83. ),
  84. 'filterName' => 'Priority',
  85. 'filterParams' => array(
  86. 'priority' => Enlight_Components_Log::WARN
  87. )
  88. )*/
  89. return true;
  90. }
  91. /**
  92. * Sets the given Zend_Log object into the internal log property.
  93. * If no log given, a new instance with the internal configuration will be created.
  94. * @param Enlight_Components_Log|Zend_Log $log
  95. */
  96. public function setResource(Zend_Log $log = null)
  97. {
  98. if ($log === null) {
  99. $config = $this->Config();
  100. if (count($config) === 0) {
  101. $config = new Enlight_Config(array(
  102. array('writerName' => 'Null'),
  103. array('writerName' => 'Firebug')
  104. ));
  105. }
  106. $log = Enlight_Components_Log::factory($config);
  107. }
  108. $this->log = $log;
  109. }
  110. /**
  111. * Setter method for the channel property. If no channel given
  112. * a new instance of the Zend_Wildfire_Channel_HttpHeaders will be used.
  113. *
  114. * @param Zend_Wildfire_Channel_HttpHeaders $channel
  115. */
  116. public function setFirebugChannel($channel = null)
  117. {
  118. if ($channel === null) {
  119. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  120. }
  121. $this->channel = $channel;
  122. }
  123. /**
  124. * Getter method for the log property which contains an instance of the Enlight_Components_Log.
  125. * @return Enlight_Components_Log
  126. */
  127. public function Resource()
  128. {
  129. if ($this->log === null) {
  130. $this->setResource();
  131. }
  132. return $this->log;
  133. }
  134. /**
  135. * Getter method of the channel property. If the channel isn't instantiated
  136. * a new instance of the Zend_Wildfire_Channel_HttpHeaders will be initial.
  137. * @return Zend_Wildfire_Channel_HttpHeaders
  138. */
  139. public function FirebugChannel()
  140. {
  141. if ($this->channel === null) {
  142. $this->setFirebugChannel();
  143. }
  144. return $this->channel;
  145. }
  146. /**
  147. * Resource handler for log plugin
  148. *
  149. * @param Enlight_Event_EventArgs $args
  150. * @return Enlight_Components_Log
  151. */
  152. public function onInitResourceLog(Enlight_Event_EventArgs $args)
  153. {
  154. return $this->Resource();
  155. }
  156. /**
  157. * Listener method for the Enlight_Controller_Front_RouteStartup event.
  158. * Adds the user-agent and the remote-address to the log component.
  159. * Sets the request and the response object into the Zend_Wildfire_Channel_HttpHeaders.
  160. *
  161. * @param Enlight_Event_EventArgs $args
  162. */
  163. public function onRouteStartup(Enlight_Event_EventArgs $args)
  164. {
  165. /** @var $request Enlight_Controller_Request_RequestHttp */
  166. $request = $args->getSubject()->Request();
  167. /** @var $request Enlight_Controller_Request_ResponseHttp */
  168. $response = $args->getSubject()->Response();
  169. /** @var $log Zend_Log */
  170. $log = $this->Resource();
  171. $log->setEventItem('remote_address', $request->getClientIp(false));
  172. $log->setEventItem('user_agent', $request->getHeader('USER_AGENT'));
  173. $channel = $this->FirebugChannel();
  174. $channel->setRequest($request);
  175. $channel->setResponse($response);
  176. }
  177. /**
  178. * Listener method for the Enlight_Controller_Front_DispatchLoopShutdown event.
  179. * On Dispatch Shutdown collect sql performance results and dump to log component.
  180. *
  181. * @param Enlight_Event_EventArgs $args
  182. */
  183. public function onDispatchLoopShutdown(Enlight_Event_EventArgs $args)
  184. {
  185. if ($this->channel !== null) {
  186. $this->channel->flush();
  187. }
  188. }
  189. }