*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Log / Writer / Mail.php
blob068bd33dc104364b1c7b204fb89e62379c892cdc
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Log
17 * @subpackage Writer
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Mail.php 16219 2009-06-21 19:45:39Z thomas $
23 /** Zend_Log_Writer_Abstract */
24 require_once 'Zend/Log/Writer/Abstract.php';
26 /** Zend_Log_Exception */
27 require_once 'Zend/Log/Exception.php';
29 /** Zend_Log_Formatter_Simple*/
30 require_once 'Zend/Log/Formatter/Simple.php';
32 /**
33 * Class used for writing log messages to email via Zend_Mail.
35 * Allows for emailing log messages at and above a certain level via a
36 * Zend_Mail object. Note that this class only sends the email upon
37 * completion, so any log entries accumulated are sent in a single email.
39 * @category Zend
40 * @package Zend_Log
41 * @subpackage Writer
42 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
43 * @license http://framework.zend.com/license/new-bsd New BSD License
44 * @version $Id: Mail.php 16219 2009-06-21 19:45:39Z thomas $
46 class Zend_Log_Writer_Mail extends Zend_Log_Writer_Abstract
48 /**
49 * Array of formatted events to include in message body.
51 * @var array
53 protected $_eventsToMail = array();
55 /**
56 * Array of formatted lines for use in an HTML email body; these events
57 * are formatted with an optional formatter if the caller is using
58 * Zend_Layout.
60 * @var array
62 protected $_layoutEventsToMail = array();
64 /**
65 * Zend_Mail instance to use
67 * @var Zend_Mail
69 protected $_mail;
71 /**
72 * Zend_Layout instance to use; optional.
74 * @var Zend_Layout
76 protected $_layout;
78 /**
79 * Optional formatter for use when rendering with Zend_Layout.
81 * @var Zend_Log_Formatter_Interface
83 protected $_layoutFormatter;
85 /**
86 * Array keeping track of the number of entries per priority level.
88 * @var array
90 protected $_numEntriesPerPriority = array();
92 /**
93 * Subject prepend text.
95 * Can only be used of the Zend_Mail object has not already had its
96 * subject line set. Using this will cause the subject to have the entry
97 * counts per-priority level appended to it.
99 * @var string|null
101 protected $_subjectPrependText;
104 * Class constructor.
106 * Constructs the mail writer; requires a Zend_Mail instance, and takes an
107 * optional Zend_Layout instance. If Zend_Layout is being used,
108 * $this->_layout->events will be set for use in the layout template.
110 * @param Zend_Mail $mail Mail instance
111 * @param Zend_Layout $layout Layout instance; optional
112 * @return void
114 public function __construct(Zend_Mail $mail, Zend_Layout $layout = null)
116 $this->_mail = $mail;
117 $this->_layout = $layout;
118 $this->_formatter = new Zend_Log_Formatter_Simple();
122 * Places event line into array of lines to be used as message body.
124 * Handles the formatting of both plaintext entries, as well as those
125 * rendered with Zend_Layout.
127 * @param array $event Event data
128 * @return void
130 protected function _write($event)
132 // Track the number of entries per priority level.
133 if (!isset($this->_numEntriesPerPriority[$event['priorityName']])) {
134 $this->_numEntriesPerPriority[$event['priorityName']] = 1;
135 } else {
136 $this->_numEntriesPerPriority[$event['priorityName']]++;
139 $formattedEvent = $this->_formatter->format($event);
141 // All plaintext events are to use the standard formatter.
142 $this->_eventsToMail[] = $formattedEvent;
144 // If we have a Zend_Layout instance, use a specific formatter for the
145 // layout if one exists. Otherwise, just use the event with its
146 // default format.
147 if ($this->_layout) {
148 if ($this->_layoutFormatter) {
149 $this->_layoutEventsToMail[] =
150 $this->_layoutFormatter->format($event);
151 } else {
152 $this->_layoutEventsToMail[] = $formattedEvent;
158 * Gets instance of Zend_Log_Formatter_Instance used for formatting a
159 * message using Zend_Layout, if applicable.
161 * @return Zend_Log_Formatter_Interface|null The formatter, or null.
163 public function getLayoutFormatter()
165 return $this->_layoutFormatter;
169 * Sets a specific formatter for use with Zend_Layout events.
171 * Allows use of a second formatter on lines that will be rendered with
172 * Zend_Layout. In the event that Zend_Layout is not being used, this
173 * formatter cannot be set, so an exception will be thrown.
175 * @param Zend_Log_Formatter_Interface $formatter
176 * @return Zend_Log_Writer_Mail
177 * @throws Zend_Log_Exception
179 public function setLayoutFormatter(Zend_Log_Formatter_Interface $formatter)
181 if (!$this->_layout) {
182 throw new Zend_Log_Exception(
183 'cannot set formatter for layout; ' .
184 'a Zend_Layout instance is not in use');
187 $this->_layoutFormatter = $formatter;
188 return $this;
192 * Allows caller to have the mail subject dynamically set to contain the
193 * entry counts per-priority level.
195 * Sets the text for use in the subject, with entry counts per-priority
196 * level appended to the end. Since a Zend_Mail subject can only be set
197 * once, this method cannot be used if the Zend_Mail object already has a
198 * subject set.
200 * @param string $subject Subject prepend text.
201 * @return Zend_Log_Writer_Mail
203 public function setSubjectPrependText($subject)
205 if ($this->_mail->getSubject()) {
206 throw new Zend_Log_Exception(
207 'subject already set on mail; ' .
208 'cannot set subject prepend text');
211 $this->_subjectPrependText = (string) $subject;
212 return $this;
216 * Sends mail to recipient(s) if log entries are present. Note that both
217 * plaintext and HTML portions of email are handled here.
219 * @return void
221 public function shutdown()
223 // If there are events to mail, use them as message body. Otherwise,
224 // there is no mail to be sent.
225 if (empty($this->_eventsToMail)) {
226 return;
229 if ($this->_subjectPrependText !== null) {
230 // Tack on the summary of entries per-priority to the subject
231 // line and set it on the Zend_Mail object.
232 $numEntries = $this->_getFormattedNumEntriesPerPriority();
233 $this->_mail->setSubject(
234 "{$this->_subjectPrependText} ({$numEntries})");
238 // Always provide events to mail as plaintext.
239 $this->_mail->setBodyText(implode('', $this->_eventsToMail));
241 // If a Zend_Layout instance is being used, set its "events"
242 // value to the lines formatted for use with the layout.
243 if ($this->_layout) {
244 // Set the required "messages" value for the layout. Here we
245 // are assuming that the layout is for use with HTML.
246 $this->_layout->events =
247 implode('', $this->_layoutEventsToMail);
249 // If an exception occurs during rendering, convert it to a notice
250 // so we can avoid an exception thrown without a stack frame.
251 try {
252 $this->_mail->setBodyHtml($this->_layout->render());
253 } catch (Exception $e) {
254 trigger_error(
255 "exception occurred when rendering layout; " .
256 "unable to set html body for message; " .
257 "message = {$e->getMessage()}; " .
258 "code = {$e->getCode()}; " .
259 "exception class = " . get_class($e),
260 E_USER_NOTICE);
264 // Finally, send the mail. If an exception occurs, convert it into a
265 // warning-level message so we can avoid an exception thrown without a
266 // stack frame.
267 try {
268 $this->_mail->send();
269 } catch (Exception $e) {
270 trigger_error(
271 "unable to send log entries via email; " .
272 "message = {$e->getMessage()}; " .
273 "code = {$e->getCode()}; " .
274 "exception class = " . get_class($e),
275 E_USER_WARNING);
280 * Gets a string of number of entries per-priority level that occurred, or
281 * an emptry string if none occurred.
283 * @return string
285 protected function _getFormattedNumEntriesPerPriority()
287 $strings = array();
289 foreach ($this->_numEntriesPerPriority as $priority => $numEntries) {
290 $strings[] = "{$priority}={$numEntries}";
293 return implode(', ', $strings);