Cgiapp2
[ class tree: Cgiapp2 ] [ index: Cgiapp2 ] [ all elements ]

Source for file Mail.class.php

Documentation is available at Mail.class.php

  1. <?php
  2. /**
  3. * Cgiapp2 - Framework for building reusable web-applications
  4. *
  5. * A PHP5 port of perl's CGI::Application, a framework for building reusable web
  6. * applications.
  7. *
  8. * @package Cgiapp2
  9. * @author Matthew Weier O'Phinney <mweierophinney@gmail.com>; based on
  10. * CGI::Application, by Jesse Erlbaum <jesse@erlbaum.net>, et. al.
  11. * @copyright (c) 2004 - present, Matthew Weier O'Phinney
  12. * @license BSD License (http://www.opensource.org/licenses/bsd-license.php)
  13. * @category Tools and Utilities
  14. * @tutorial Cgiapp2/Cgiapp2.cls
  15. * @version $Id:$
  16. */
  17.  
  18. /**
  19. * Observes Cgiapp2_Exception
  20. */
  21. require_once 'Cgiapp2/Exception.class.php';
  22.  
  23. /**
  24. * Implements Cgiapp2_Exception_Observer_Interface
  25. */
  26. require_once 'Cgiapp2/Exception/Observer/Interface.class.php';
  27.  
  28. /**
  29. * Cgiapp2_Exception_Observer_Mail
  30. *
  31. * {@link Cgiapp2_Exception} observer. Mails exception information to a user.
  32. *
  33. * Sample usage:
  34. * <code>
  35. * require_once 'Cgiapp2/Exception/Observer/Mail.class.php';
  36. *
  37. * // Set the mail recipient to 'mweierophinney@gmail.com'
  38. * Cgiapp2_Exception_Observer_Mail:setRecipient('mweierophinney@gmail.com');
  39. *
  40. * // Set the mail subject to 'Exception in application code'
  41. * Cgiapp2_Exception_Observer_Mail:setSubject('Exception in application code');
  42. *
  43. * try {
  44. * throw new Cgiapp2_Exception('Mail this...');
  45. * } catch (Cgiapp2_Exception $e) {
  46. * // do something
  47. * }
  48. * </code>
  49. *
  50. * @package Cgiapp2
  51. * @author Matthew Weier O'Phinney <mweierophinney@gmail.com>
  52. * @copyright (c) 2006 - Present, Matthew Weier O'Phinney
  53. * <mweierophinney@gmail.com>
  54. * @version @release-version@
  55. */
  56. class Cgiapp2_Exception_Observer_Mail implements Cgiapp2_Exception_Observer_Interface
  57. {
  58. /**
  59. * Email recipient
  60. * @var string
  61. * @access public
  62. */
  63. public $to;
  64.  
  65. /**
  66. * Email subject
  67. * @var string
  68. * @access public
  69. */
  70. public $subject;
  71.  
  72. /**
  73. * Singleton instance
  74. * @var bool|Cgiapp2_Exception_Observer_Mail
  75. * @static
  76. * @access private
  77. */
  78. private static $instance = false;
  79.  
  80. /**
  81. * Constructor
  82. *
  83. * @param mixed $to
  84. * @param mixed $subject
  85. * @access private
  86. * @return void
  87. */
  88. private function __construct($subject, $to = null)
  89. {
  90. $this->to = $to;
  91. $this->subject = $subject;
  92. }
  93.  
  94. /**
  95. * Singleton
  96. *
  97. * Accepts recipient address and subject; uses sane default for subject
  98. * ('Exception occurred in application').
  99. *
  100. * @static
  101. * @access public
  102. * @param string $to
  103. * @param string $subject
  104. * @return Cgiapp2_Exception_Observer_Mail
  105. */
  106. public static function getInstance($to = null, $subject = '')
  107. {
  108. if (self::$instance) {
  109. return self::$instance;
  110. }
  111.  
  112. if (empty($subject)) {
  113. $subject = 'Exception in application occurred';
  114. }
  115.  
  116. return new Cgiapp2_Exception_Observer_Mail($subject, $to);
  117. }
  118.  
  119. /**
  120. * Set mail recipient
  121. *
  122. * @static
  123. * @access public
  124. * @param string $to
  125. * @return void
  126. */
  127. public static function setRecipient($to)
  128. {
  129. self::getInstance()->to = $to;
  130. }
  131.  
  132. /**
  133. * Set mail subject
  134. *
  135. * @static
  136. * @access public
  137. * @param string $subject
  138. * @return void
  139. */
  140. public static function setSubject($subject)
  141. {
  142. self::getInstance()->subject = $subject;
  143. }
  144.  
  145. /**
  146. * Mail a report
  147. *
  148. * If no {@link $to} address is defined in the singleton instance, nothing
  149. * is done. Otherwise, an email is sent with details of the exception.
  150. *
  151. * @access public
  152. * @param Cgiapp2_Exception $e
  153. * @return void
  154. */
  155. public static function event(Cgiapp2_Exception $e)
  156. {
  157. $handler = self::getInstance();
  158. if (empty($handler->to)) {
  159. return;
  160. }
  161.  
  162. $file = $e->getFile();
  163. $line = $e->getLine();
  164. $code = $e->getCode();
  165. $msg = $e->getMessage();
  166. $trace = $e->getTraceAsString();
  167.  
  168. $body=<<<EOT
  169. An error occurred in your application:
  170.  
  171. File: $file
  172. Line: $line
  173. Error Number: $code
  174. Error Message:
  175. $msg
  176.  
  177. Backtrace:
  178. $trace
  179. EOT;
  180. @mail($handler->to, $handler->subject, $body);
  181. }
  182. }
  183.  
  184. /**
  185. * Observe Cgiapp2_Exception
  186. */
  187. Cgiapp2_Exception::attach('Cgiapp2_Exception_Observer_Mail');

Documentation generated on Sat, 03 Jun 2006 10:48:52 -0400 by phpDocumentor 1.3.0RC5