PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/weberlars/sot-shopware
PHP | 210 lines | 104 code | 15 blank | 91 comment | 7 complexity | 94f5345502c84d9d4fa4034a08ef8a9c 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
  27. * @copyright Copyright (c) 2012, shopware AG (http://www.shopware.de)
  28. * @version $Id$
  29. * @author $Author$
  30. */
  31. /**
  32. * Log 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. */
  39. class Shopware_Plugins_Core_Log_Bootstrap extends Shopware_Components_Plugin_Bootstrap
  40. {
  41. /**
  42. * @var Zend_Wildfire_Channel_HttpHeaders Contains an instance of the Zend_Wildfire_Channel_HttpHeaders
  43. */
  44. protected $channel;
  45. /**
  46. * @var Enlight_Components_Log Contains an instance of the Enlight_Components_Log.
  47. */
  48. protected $log;
  49. /**
  50. * Installs the log extension plugin.
  51. * Subscribes the init resource event to initial the log resource,
  52. * the Enlight_Controller_Front_RouteStartup event to startup the routing process and
  53. * the Enlight_Controller_Front_DispatchLoopShutdown event to flush the wildfire channel.
  54. *
  55. * @return bool
  56. */
  57. public function install()
  58. {
  59. $this->subscribeEvent(
  60. 'Enlight_Bootstrap_InitResource_Log',
  61. 'onInitResourceLog'
  62. );
  63. $this->subscribeEvent(
  64. 'Enlight_Controller_Front_RouteStartup',
  65. 'onRouteStartup'
  66. );
  67. $this->subscribeEvent(
  68. 'Enlight_Controller_Front_DispatchLoopShutdown',
  69. 'onDispatchLoopShutdown',
  70. 500
  71. );
  72. $form = $this->Form();
  73. $parent = $this->Forms()->findOneBy(array('name' => 'Core'));
  74. $form->setParent($parent);
  75. $form->setElement('checkbox', 'logDb', array('label'=>'Fehler in Datenbank schreiben', 'value'=>1));
  76. $form->setElement('checkbox', 'logMail', array('label'=>'Fehler an Shopbetreiber senden', 'value'=>0));
  77. return true;
  78. }
  79. /**
  80. * Sets the given Zend_Log object into the internal log property.
  81. * If no log given, a new instance with the internal configuration will be created.
  82. * @param Enlight_Components_Log|Zend_Log $log
  83. */
  84. public function setResource(Zend_Log $log = null)
  85. {
  86. if ($log === null) {
  87. $log = new Enlight_Components_Log();
  88. $log->setEventItem('date', Zend_Date::now());
  89. $log->addWriter(new Zend_Log_Writer_Null());
  90. $config = $this->Config();
  91. if(!empty($config->logDb)) {
  92. $writer = Zend_Log_Writer_Db::factory(array(
  93. 'db' => Shopware()->Db(),
  94. 'table' => 's_core_log',
  95. 'columnmap' => array(
  96. 'type' => 'priority',
  97. 'key' => 'priorityName',
  98. 'text' => 'message',
  99. 'date' => 'date',
  100. 'ip_address' => 'remote_address',
  101. 'user_agent' => 'user_agent',
  102. )
  103. ));
  104. $writer->addFilter(Enlight_Components_Log::WARN);
  105. $log->addWriter($writer);
  106. }
  107. if(!empty($config->logMail)) {
  108. $mail = new Enlight_Components_Mail();
  109. $mail->addTo(Shopware()->Config()->Mail);
  110. $writer = new Zend_Log_Writer_Mail($mail);
  111. $writer->setSubjectPrependText('Fehler im Shop "'.Shopware()->Config()->Shopname.'" aufgetreten!');
  112. $writer->addFilter(Enlight_Components_Log::WARN);
  113. $log->addWriter($writer);
  114. }
  115. }
  116. $this->log = $log;
  117. }
  118. /**
  119. * Setter method for the channel property. If no channel given
  120. * a new instance of the Zend_Wildfire_Channel_HttpHeaders will be used.
  121. *
  122. * @param Zend_Wildfire_Channel_HttpHeaders $channel
  123. */
  124. public function setFirebugChannel($channel = null)
  125. {
  126. if ($channel === null) {
  127. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  128. }
  129. $this->channel = $channel;
  130. }
  131. /**
  132. * Getter method for the log property which contains an instance of the Enlight_Components_Log.
  133. * @return Enlight_Components_Log
  134. */
  135. public function Resource()
  136. {
  137. if ($this->log === null) {
  138. $this->setResource();
  139. }
  140. return $this->log;
  141. }
  142. /**
  143. * Getter method of the channel property. If the channel isn't instantiated
  144. * a new instance of the Zend_Wildfire_Channel_HttpHeaders will be initial.
  145. * @return Zend_Wildfire_Channel_HttpHeaders
  146. */
  147. public function FirebugChannel()
  148. {
  149. if ($this->channel === null) {
  150. $this->setFirebugChannel();
  151. }
  152. return $this->channel;
  153. }
  154. /**
  155. * Resource handler for log plugin
  156. *
  157. * @param Enlight_Event_EventArgs $args
  158. * @return Enlight_Components_Log
  159. */
  160. public function onInitResourceLog(Enlight_Event_EventArgs $args)
  161. {
  162. return $this->Resource();
  163. }
  164. /**
  165. * Listener method for the Enlight_Controller_Front_RouteStartup event.
  166. * Adds the user-agent and the remote-address to the log component.
  167. * Sets the request and the response object into the Zend_Wildfire_Channel_HttpHeaders.
  168. *
  169. * @param Enlight_Event_EventArgs $args
  170. */
  171. public function onRouteStartup(Enlight_Controller_EventArgs $args)
  172. {
  173. $request = $args->getRequest();
  174. $response = $args->getResponse();
  175. /** @var $log Zend_Log */
  176. $log = $this->Resource();
  177. $log->setEventItem('remote_address', $request->getClientIp(false));
  178. $log->setEventItem('user_agent', $request->getHeader('USER_AGENT'));
  179. $channel = $this->FirebugChannel();
  180. $channel->setRequest($request);
  181. $channel->setResponse($response);
  182. }
  183. /**
  184. * Listener method for the Enlight_Controller_Front_DispatchLoopShutdown event.
  185. * On Dispatch Shutdown collect sql performance results and dump to log component.
  186. *
  187. * @param Enlight_Event_EventArgs $args
  188. */
  189. public function onDispatchLoopShutdown(Enlight_Event_EventArgs $args)
  190. {
  191. if ($this->channel !== null) {
  192. $this->channel->flush();
  193. }
  194. }
  195. }