PageRenderTime 36ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Log/Writer/Mail.php

https://github.com/taste/zf2
PHP | 305 lines | 110 code | 29 blank | 166 comment | 11 complexity | 441d70adad2cf557748a3909b35b99ff MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  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://framework.zend.com/license/new-bsd
  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@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Log
  17. * @subpackage Writer
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Log\Writer;
  25. use Zend\Log;
  26. /**
  27. * Class used for writing log messages to email via Zend_Mail.
  28. *
  29. * Allows for emailing log messages at and above a certain level via a
  30. * Zend_Mail object. Note that this class only sends the email upon
  31. * completion, so any log entries accumulated are sent in a single email.
  32. *
  33. * @uses \Zend\Log\Exception
  34. * @uses \Zend\Log\Formatter\Simple
  35. * @uses \Zend\Log\Writer\AbstractWriter
  36. * @category Zend
  37. * @package Zend_Log
  38. * @subpackage Writer
  39. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Mail extends AbstractWriter
  43. {
  44. /**
  45. * Array of formatted events to include in message body.
  46. *
  47. * @var array
  48. */
  49. protected $_eventsToMail = array();
  50. /**
  51. * Array of formatted lines for use in an HTML email body; these events
  52. * are formatted with an optional formatter if the caller is using
  53. * Zend_Layout.
  54. *
  55. * @var array
  56. */
  57. protected $_layoutEventsToMail = array();
  58. /**
  59. * Zend_Mail instance to use
  60. *
  61. * @var \Zend\Mail\Mail
  62. */
  63. protected $_mail;
  64. /**
  65. * Zend_Layout instance to use; optional.
  66. *
  67. * @var \Zend\Layout\Layout
  68. */
  69. protected $_layout;
  70. /**
  71. * Optional formatter for use when rendering with Zend_Layout.
  72. *
  73. * @var \Zend\Log\Formatter
  74. */
  75. protected $_layoutFormatter;
  76. /**
  77. * Array keeping track of the number of entries per priority level.
  78. *
  79. * @var array
  80. */
  81. protected $_numEntriesPerPriority = array();
  82. /**
  83. * Subject prepend text.
  84. *
  85. * Can only be used of the Zend_Mail object has not already had its
  86. * subject line set. Using this will cause the subject to have the entry
  87. * counts per-priority level appended to it.
  88. *
  89. * @var string|null
  90. */
  91. protected $_subjectPrependText;
  92. /**
  93. * Class constructor.
  94. *
  95. * Constructs the mail writer; requires a Zend_Mail instance, and takes an
  96. * optional Zend_Layout instance. If Zend_Layout is being used,
  97. * $this->_layout->events will be set for use in the layout template.
  98. *
  99. * @param \Zend\Mail\Mail $mail Mail instance
  100. * @param \Zend\Layout\Layout $layout Layout instance; optional
  101. * @return void
  102. */
  103. public function __construct(\Zend\Mail\Mail $mail, \Zend\Layout\Layout $layout = null)
  104. {
  105. $this->_mail = $mail;
  106. $this->_layout = $layout;
  107. $this->_formatter = new Log\Formatter\Simple();
  108. }
  109. /**
  110. * Create a new instance of Zend_Log_Writer_Mail
  111. *
  112. * @param array|\Zend\Config\Config $config
  113. * @return \Zend\Log\Writer\Mail
  114. * @throws \Zend\Log\Exception
  115. */
  116. static public function factory($config = array())
  117. {
  118. throw new Log\Exception('Zend\\Log\\Writer\\Mail does not currently implement a factory');
  119. }
  120. /**
  121. * Places event line into array of lines to be used as message body.
  122. *
  123. * Handles the formatting of both plaintext entries, as well as those
  124. * rendered with Zend_Layout.
  125. *
  126. * @param array $event Event data
  127. * @return void
  128. */
  129. protected function _write($event)
  130. {
  131. // Track the number of entries per priority level.
  132. if (!isset($this->_numEntriesPerPriority[$event['priorityName']])) {
  133. $this->_numEntriesPerPriority[$event['priorityName']] = 1;
  134. } else {
  135. $this->_numEntriesPerPriority[$event['priorityName']]++;
  136. }
  137. $formattedEvent = $this->_formatter->format($event);
  138. // All plaintext events are to use the standard formatter.
  139. $this->_eventsToMail[] = $formattedEvent;
  140. // If we have a Zend_Layout instance, use a specific formatter for the
  141. // layout if one exists. Otherwise, just use the event with its
  142. // default format.
  143. if ($this->_layout) {
  144. if ($this->_layoutFormatter) {
  145. $this->_layoutEventsToMail[] =
  146. $this->_layoutFormatter->format($event);
  147. } else {
  148. $this->_layoutEventsToMail[] = $formattedEvent;
  149. }
  150. }
  151. }
  152. /**
  153. * Gets instance of Zend_Log_Formatter_Instance used for formatting a
  154. * message using Zend_Layout, if applicable.
  155. *
  156. * @return \Zend\Log\Formatter|null The formatter, or null.
  157. */
  158. public function getLayoutFormatter()
  159. {
  160. return $this->_layoutFormatter;
  161. }
  162. /**
  163. * Sets a specific formatter for use with Zend_Layout events.
  164. *
  165. * Allows use of a second formatter on lines that will be rendered with
  166. * Zend_Layout. In the event that Zend_Layout is not being used, this
  167. * formatter cannot be set, so an exception will be thrown.
  168. *
  169. * @param \Zend\Log\Formatter\FormatterInterface $formatter
  170. * @return \Zend\Log\Writer\Mail
  171. * @throws \Zend\Log\Exception
  172. */
  173. public function setLayoutFormatter(Log\Formatter $formatter)
  174. {
  175. if (!$this->_layout) {
  176. throw new Log\Exception(
  177. 'cannot set formatter for layout; ' .
  178. 'a Zend_Layout instance is not in use');
  179. }
  180. $this->_layoutFormatter = $formatter;
  181. return $this;
  182. }
  183. /**
  184. * Allows caller to have the mail subject dynamically set to contain the
  185. * entry counts per-priority level.
  186. *
  187. * Sets the text for use in the subject, with entry counts per-priority
  188. * level appended to the end. Since a Zend_Mail subject can only be set
  189. * once, this method cannot be used if the Zend_Mail object already has a
  190. * subject set.
  191. *
  192. * @param string $subject Subject prepend text.
  193. * @return \Zend\Log\Writer\Mail
  194. */
  195. public function setSubjectPrependText($subject)
  196. {
  197. if ($this->_mail->getSubject()) {
  198. throw new Log\Exception(
  199. 'subject already set on mail; ' .
  200. 'cannot set subject prepend text');
  201. }
  202. $this->_subjectPrependText = (string) $subject;
  203. return $this;
  204. }
  205. /**
  206. * Sends mail to recipient(s) if log entries are present. Note that both
  207. * plaintext and HTML portions of email are handled here.
  208. *
  209. * @return void
  210. */
  211. public function shutdown()
  212. {
  213. // If there are events to mail, use them as message body. Otherwise,
  214. // there is no mail to be sent.
  215. if (empty($this->_eventsToMail)) {
  216. return;
  217. }
  218. if ($this->_subjectPrependText !== null) {
  219. // Tack on the summary of entries per-priority to the subject
  220. // line and set it on the Zend_Mail object.
  221. $numEntries = $this->_getFormattedNumEntriesPerPriority();
  222. $this->_mail->setSubject(
  223. "{$this->_subjectPrependText} ({$numEntries})");
  224. }
  225. // Always provide events to mail as plaintext.
  226. $this->_mail->setBodyText(implode('', $this->_eventsToMail));
  227. // If a Zend_Layout instance is being used, set its "events"
  228. // value to the lines formatted for use with the layout.
  229. if ($this->_layout) {
  230. // Set the required "messages" value for the layout. Here we
  231. // are assuming that the layout is for use with HTML.
  232. $this->_layout->events =
  233. implode('', $this->_layoutEventsToMail);
  234. // If an exception occurs during rendering, convert it to a notice
  235. // so we can avoid an exception thrown without a stack frame.
  236. try {
  237. $this->_mail->setBodyHtml($this->_layout->render());
  238. } catch (\Exception $e) {
  239. trigger_error(
  240. "exception occurred when rendering layout; " .
  241. "unable to set html body for message; " .
  242. "message = {$e->getMessage()}; " .
  243. "code = {$e->getCode()}; " .
  244. "exception class = " . get_class($e),
  245. E_USER_NOTICE);
  246. }
  247. }
  248. // Finally, send the mail. If an exception occurs, convert it into a
  249. // warning-level message so we can avoid an exception thrown without a
  250. // stack frame.
  251. try {
  252. $this->_mail->send();
  253. } catch (\Exception $e) {
  254. trigger_error(
  255. "unable to send log entries via email; " .
  256. "message = {$e->getMessage()}; " .
  257. "code = {$e->getCode()}; " .
  258. "exception class = " . get_class($e),
  259. E_USER_WARNING);
  260. }
  261. }
  262. /**
  263. * Gets a string of number of entries per-priority level that occurred, or
  264. * an emptry string if none occurred.
  265. *
  266. * @return string
  267. */
  268. protected function _getFormattedNumEntriesPerPriority()
  269. {
  270. $strings = array();
  271. foreach ($this->_numEntriesPerPriority as $priority => $numEntries) {
  272. $strings[] = "{$priority}={$numEntries}";
  273. }
  274. return implode(', ', $strings);
  275. }
  276. }