• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List

E:/E/GEAMP/www/openbiz/openbiz/others/Zend/Log/Writer/Mail.php

00001 <?php
00024 require_once 'Zend/Log/Writer/Abstract.php';
00025 
00027 require_once 'Zend/Log/Exception.php';
00028 
00030 require_once 'Zend/Log/Formatter/Simple.php';
00031 
00046 class Zend_Log_Writer_Mail extends Zend_Log_Writer_Abstract
00047 {
00053     protected $_eventsToMail = array();
00054 
00062     protected $_layoutEventsToMail = array();
00063 
00069     protected $_mail;
00070 
00076     protected $_layout;
00077 
00083     protected $_layoutFormatter;
00084 
00090     protected $_numEntriesPerPriority = array();
00091 
00101     protected $_subjectPrependText;
00102 
00114     public function __construct(Zend_Mail $mail, Zend_Layout $layout = null)
00115     {
00116         $this->_mail      = $mail;
00117         $this->_layout    = $layout;
00118         $this->_formatter = new Zend_Log_Formatter_Simple();
00119     }
00120 
00130     protected function _write($event)
00131     {
00132         // Track the number of entries per priority level.
00133         if (!isset($this->_numEntriesPerPriority[$event['priorityName']])) {
00134             $this->_numEntriesPerPriority[$event['priorityName']] = 1;
00135         } else {
00136             $this->_numEntriesPerPriority[$event['priorityName']]++;
00137         }
00138 
00139         $formattedEvent = $this->_formatter->format($event);
00140 
00141         // All plaintext events are to use the standard formatter.
00142         $this->_eventsToMail[] = $formattedEvent;
00143 
00144         // If we have a Zend_Layout instance, use a specific formatter for the
00145         // layout if one exists.  Otherwise, just use the event with its
00146         // default format.
00147         if ($this->_layout) {
00148             if ($this->_layoutFormatter) {
00149                 $this->_layoutEventsToMail[] =
00150                     $this->_layoutFormatter->format($event);
00151             } else {
00152                 $this->_layoutEventsToMail[] = $formattedEvent;
00153             }
00154         }
00155     }
00156 
00163     public function getLayoutFormatter()
00164     {
00165         return $this->_layoutFormatter;
00166     }
00167 
00179     public function setLayoutFormatter(Zend_Log_Formatter_Interface $formatter)
00180     {
00181         if (!$this->_layout) {
00182             throw new Zend_Log_Exception(
00183                 'cannot set formatter for layout; ' .
00184                     'a Zend_Layout instance is not in use');
00185         }
00186 
00187         $this->_layoutFormatter = $formatter;
00188         return $this;
00189     }
00190 
00203     public function setSubjectPrependText($subject)
00204     {
00205         if ($this->_mail->getSubject()) {
00206             throw new Zend_Log_Exception(
00207                 'subject already set on mail; ' .
00208                     'cannot set subject prepend text');
00209         }
00210 
00211         $this->_subjectPrependText = (string) $subject;
00212         return $this;
00213     }
00214 
00221     public function shutdown()
00222     {
00223         // If there are events to mail, use them as message body.  Otherwise,
00224         // there is no mail to be sent.
00225         if (empty($this->_eventsToMail)) {
00226             return;
00227         }
00228 
00229         if ($this->_subjectPrependText !== null) {
00230             // Tack on the summary of entries per-priority to the subject
00231             // line and set it on the Zend_Mail object.
00232             $numEntries = $this->_getFormattedNumEntriesPerPriority();
00233             $this->_mail->setSubject(
00234                 "{$this->_subjectPrependText} ({$numEntries})");
00235         }
00236 
00237 
00238         // Always provide events to mail as plaintext.
00239         $this->_mail->setBodyText(implode('', $this->_eventsToMail));
00240 
00241         // If a Zend_Layout instance is being used, set its "events"
00242         // value to the lines formatted for use with the layout.
00243         if ($this->_layout) {
00244             // Set the required "messages" value for the layout.  Here we
00245             // are assuming that the layout is for use with HTML.
00246             $this->_layout->events =
00247                 implode('', $this->_layoutEventsToMail);
00248 
00249             // If an exception occurs during rendering, convert it to a notice
00250             // so we can avoid an exception thrown without a stack frame.
00251             try {
00252                 $this->_mail->setBodyHtml($this->_layout->render());
00253             } catch (Exception $e) {
00254                 trigger_error(
00255                     "exception occurred when rendering layout; " .
00256                         "unable to set html body for message; " .
00257                         "message = {$e->getMessage()}; " .
00258                         "code = {$e->getCode()}; " .
00259                         "exception class = " . get_class($e),
00260                     E_USER_NOTICE);
00261             }
00262         }
00263 
00264         // Finally, send the mail.  If an exception occurs, convert it into a
00265         // warning-level message so we can avoid an exception thrown without a
00266         // stack frame.
00267         try {
00268             $this->_mail->send();
00269         } catch (Exception $e) {
00270             trigger_error(
00271                 "unable to send log entries via email; " .
00272                     "message = {$e->getMessage()}; " .
00273                     "code = {$e->getCode()}; " .
00274                         "exception class = " . get_class($e),
00275                 E_USER_WARNING);
00276         }
00277     }
00278 
00285     protected function _getFormattedNumEntriesPerPriority()
00286     {
00287         $strings = array();
00288 
00289         foreach ($this->_numEntriesPerPriority as $priority => $numEntries) {
00290             $strings[] = "{$priority}={$numEntries}";
00291         }
00292 
00293         return implode(', ', $strings);
00294     }
00295 }

Generated on Thu Apr 19 2012 17:01:18 for openbiz by  doxygen 1.7.2