瀏覽代碼

Merge pull request #26 from stof/swiftmailer-callback

Added the possibility to configure the message using a callback in SwiftMailerHandler
Jordi Boggiano 14 年之前
父節點
當前提交
0ebdf69daa
共有 2 個文件被更改,包括 13 次插入7 次删除
  1. 5 5
      README.mdown
  2. 8 2
      src/Monolog/Handler/SwiftMailerHandler.php

+ 5 - 5
README.mdown

@@ -34,16 +34,16 @@ Notable Features (non-exhaustive and incomplete)
 - _FingersCrossedHandler_: A very interesting handler. It takes a logger as parameter and will accumulate log records of all levels until a record exceeds the defined severity level. At which point it delivers all records, including those of lower severity, to the handler it wraps. This means that until an error actually happens you will not see anything in your logs, but when it happens you will have the full information, including debug and info records. This provides you with the info you need, only when you need it.
 - _FirePHPHandler_: Handler for [FirePHP](http://www.firephp.org/), providing inline `console` messages within [FireBug](http://getfirebug.com/).
 
-Todo
-----
-
-- MailHandler
-
 Requirements
 ------------
 
 Any flavor of PHP 5.3 should do
 
+Submitting bugs and feature requests
+------------------------------------
+
+Bugs and feature request are tracked on [Github](https://github.com/Seldaek/monolog/issues)
+
 Author
 ------
 

+ 8 - 2
src/Monolog/Handler/SwiftMailerHandler.php

@@ -25,14 +25,20 @@ class SwiftMailerHandler extends MailHandler
 
     /**
      * @param \Swift_Mailer $mailer The mailer to use
-     * @param \Swift_Message $message An example message for real messages, only the body will be replaced
+     * @param callback|\Swift_Message $message An example message for real messages, only the body will be replaced
      * @param integer $level The minimum logging level at which this handler will be triggered
      * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
      */
-    public function __construct(\Swift_Mailer $mailer, \Swift_Message $message, $level = Logger::ERROR, $bubble = false)
+    public function __construct(\Swift_Mailer $mailer, $message, $level = Logger::ERROR, $bubble = false)
     {
         parent::__construct($level, $bubble);
         $this->mailer  = $mailer;
+        if (!$message instanceof \Swift_Message && is_callable($message)) {
+            $message = call_user_func($message);
+        }
+        if (!$message instanceof \Swift_Message) {
+            throw new \InvalidArgumentException('You must provide either a Swift_Message instance or a callback returning it');
+        }
         $this->message = $message;
     }