From c0316a10dd2ad03bffc85b5c0106c71b66cb4a1e Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 29 Dec 2013 06:23:38 +0400 Subject: [PATCH 1/6] added mail events before/after send --- framework/yii/base/ActionEvent.php | 11 ++++----- framework/yii/base/Event.php | 5 ++++ framework/yii/base/MailEvent.php | 30 ++++++++++++++++++++++++ framework/yii/base/ModelEvent.php | 8 ++----- framework/yii/base/ViewEvent.php | 11 ++++----- framework/yii/mail/BaseMailer.php | 48 ++++++++++++++++++++++++++++++++++++-- 6 files changed, 92 insertions(+), 21 deletions(-) create mode 100644 framework/yii/base/MailEvent.php diff --git a/framework/yii/base/ActionEvent.php b/framework/yii/base/ActionEvent.php index 6e123a0..adfeddc 100644 --- a/framework/yii/base/ActionEvent.php +++ b/framework/yii/base/ActionEvent.php @@ -11,7 +11,10 @@ namespace yii\base; * ActionEvent represents the event parameter used for an action event. * * By setting the [[isValid]] property, one may control whether to continue running the action. - * + * @property boolean $isValid Event handlers of + * [[Controller::EVENT_BEFORE_ACTION]] may set this property to decide whether + * to continue running the current action. + * * @author Qiang Xue * @since 2.0 */ @@ -25,12 +28,6 @@ class ActionEvent extends Event * @var mixed the action result. Event handlers may modify this property to change the action result. */ public $result; - /** - * @var boolean whether to continue running the action. Event handlers of - * [[Controller::EVENT_BEFORE_ACTION]] may set this property to decide whether - * to continue running the current action. - */ - public $isValid = true; /** * Constructor. diff --git a/framework/yii/base/Event.php b/framework/yii/base/Event.php index 974a1a4..4c8ffcb 100644 --- a/framework/yii/base/Event.php +++ b/framework/yii/base/Event.php @@ -46,6 +46,11 @@ class Event extends Object */ public $data; + /** + * @var boolean whether to continue execution of process or not. + */ + public $isValid = true; + private static $_events = []; /** diff --git a/framework/yii/base/MailEvent.php b/framework/yii/base/MailEvent.php new file mode 100644 index 0000000..188c7d3 --- /dev/null +++ b/framework/yii/base/MailEvent.php @@ -0,0 +1,30 @@ + + * @since 2.0 + */ + +class MailEvent extends Event +{ + + /** + * @var \yii\mail\MessageInterface mail message being send + */ + public $message; + /** + * + */ + public $isSuccessful; +} diff --git a/framework/yii/base/ModelEvent.php b/framework/yii/base/ModelEvent.php index 57e41f9..b4b602d 100644 --- a/framework/yii/base/ModelEvent.php +++ b/framework/yii/base/ModelEvent.php @@ -11,15 +11,11 @@ namespace yii\base; * ModelEvent class. * * ModelEvent represents the parameter needed by model events. - * + * @property boolean $isValid A model is in valid status if it passes validations or certain checks. + * * @author Qiang Xue * @since 2.0 */ class ModelEvent extends Event { - /** - * @var boolean whether the model is in valid status. Defaults to true. - * A model is in valid status if it passes validations or certain checks. - */ - public $isValid = true; } diff --git a/framework/yii/base/ViewEvent.php b/framework/yii/base/ViewEvent.php index d02e180..70ba96d 100644 --- a/framework/yii/base/ViewEvent.php +++ b/framework/yii/base/ViewEvent.php @@ -10,6 +10,11 @@ namespace yii\base; /** * ViewEvent represents events triggered by the [[View]] component. * + * @property boolean $isValid whether to continue rendering the view file. Event handlers of + *[[View::EVENT_BEFORE_RENDER]] may set this property to decide whether + * to continue rendering the current view file. + * + * * @author Qiang Xue * @since 2.0 */ @@ -26,12 +31,6 @@ class ViewEvent extends Event * @var string the view file path that is being rendered by [[View::renderFile()]]. */ public $viewFile; - /** - * @var boolean whether to continue rendering the view file. Event handlers of - * [[View::EVENT_BEFORE_RENDER]] may set this property to decide whether - * to continue rendering the current view file. - */ - public $isValid = true; /** * Constructor. diff --git a/framework/yii/mail/BaseMailer.php b/framework/yii/mail/BaseMailer.php index 90565c9..1ed0071 100644 --- a/framework/yii/mail/BaseMailer.php +++ b/framework/yii/mail/BaseMailer.php @@ -12,6 +12,7 @@ use yii\base\Component; use yii\base\InvalidConfigException; use yii\base\ViewContextInterface; use yii\web\View; +use yii\base\MailEvent; /** * BaseMailer serves as a base class that implements the basic functions required by [[MailerInterface]]. @@ -28,6 +29,16 @@ use yii\web\View; */ abstract class BaseMailer extends Component implements MailerInterface, ViewContextInterface { + + /** + * @event ActionEvent an event raised right before executing a controller action. + * You may set [[ActionEvent::isValid]] to be false to cancel the action execution. + */ + const EVENT_BEFORE_SEND = 'beforeSend'; + /** + * @event ActionEvent an event raised right after executing a controller action. + */ + const EVENT_AFTER_SEND = 'afterSend'; /** * @var string directory containing view files for this email messages. * This can be specified as an absolute path or path alias. @@ -206,6 +217,10 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont */ public function send($message) { + if (!$this->beforeSend($message)) { + return false; + } + $address = $message->getTo(); if (is_array($address)) { $address = implode(', ', array_keys($address)); @@ -213,10 +228,12 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont Yii::info('Sending email "' . $message->getSubject() . '" to "' . $address . '"', __METHOD__); if ($this->useFileTransport) { - return $this->saveMessage($message); + $isSuccessful = $this->saveMessage($message); } else { - return $this->sendMessage($message); + $isSuccessful = $this->sendMessage($message); } + $this->afterSend($message, $isSuccessful); + return $isSuccessful; } /** @@ -297,4 +314,31 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont { return Yii::getAlias($this->viewPath) . DIRECTORY_SEPARATOR . $view; } + + /** + * This method is invoked right before mail send. + * You may override this method to do last-minute preparation for the message. + * If you override this method, please make sure you call the parent implementation first. + * @param MessageInterface $message + */ + public function beforeSend($message) + { + $event = new MailEvent(['message' => $message]); + $this->trigger(self::EVENT_BEFORE_SEND, $event); + return $event->isValid; + } + + /** + * This method is invoked right after mail was send. + * You may override this method to do some postprocessing or logging based on mail send status. + * If you override this method, please make sure you call the parent implementation first. + * @param MessageInterface $message + * @param boolean $isSuccessful + */ + public function afterSend($message, $isSuccessful) + { + $event = new MailEvent(['message' => $message, 'isSuccessful' => $isSuccessful]); + $this->trigger(self::EVENT_AFTER_SEND, $event); + } + } From 791d8124ad23b9eac612650e4ef7893450109f8f Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 29 Dec 2013 06:23:52 +0400 Subject: [PATCH 2/6] added test --- tests/unit/framework/mail/BaseMailerTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/unit/framework/mail/BaseMailerTest.php b/tests/unit/framework/mail/BaseMailerTest.php index 1c3ee22..54a7952 100644 --- a/tests/unit/framework/mail/BaseMailerTest.php +++ b/tests/unit/framework/mail/BaseMailerTest.php @@ -230,6 +230,17 @@ class BaseMailerTest extends TestCase $this->assertTrue(is_file($file)); $this->assertEquals($message->toString(), file_get_contents($file)); } + + public function testBeforeSendEvent() + { + $message = new Message(); + + $mailerMock = $this->getMockBuilder('yiiunit\framework\mail\Mailer')->setMethods(['beforeSend','afterSend'])->getMock(); + $mailerMock->expects($this->once())->method('beforeSend')->with($message)->will($this->returnValue(true)); + $mailerMock->expects($this->once())->method('afterSend')->with($message,true); + $mailerMock->send($message); + } + } /** @@ -243,6 +254,7 @@ class Mailer extends BaseMailer protected function sendMessage($message) { $this->sentMessages[] = $message; + return true; } } From 59535d38cf971f2f47aa613f44cf6ae2296ee8d5 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 29 Dec 2013 07:13:35 +0400 Subject: [PATCH 3/6] doc fix --- framework/yii/mail/BaseMailer.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/framework/yii/mail/BaseMailer.php b/framework/yii/mail/BaseMailer.php index 1ed0071..baccbe2 100644 --- a/framework/yii/mail/BaseMailer.php +++ b/framework/yii/mail/BaseMailer.php @@ -31,12 +31,12 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont { /** - * @event ActionEvent an event raised right before executing a controller action. - * You may set [[ActionEvent::isValid]] to be false to cancel the action execution. + * @event \yii\base\MailEvent an event raised right before send. + * You may set [[\yii\base\MailEvent::isValid]] to be false to cancel the send. */ const EVENT_BEFORE_SEND = 'beforeSend'; /** - * @event ActionEvent an event raised right after executing a controller action. + * @event \yii\base\MailEvent an event raised right after send. */ const EVENT_AFTER_SEND = 'afterSend'; /** From f267f4623fdf53b0182aa65d4bbe5aa08af51b2a Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 30 Dec 2013 18:25:38 +0400 Subject: [PATCH 4/6] reverted $isValid for events. --- framework/yii/base/ActionEvent.php | 13 ++++++++----- framework/yii/base/Event.php | 7 +------ framework/yii/base/MailEvent.php | 8 +++++++- framework/yii/base/ModelEvent.php | 10 +++++++--- framework/yii/base/ViewEvent.php | 13 +++++++------ 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/framework/yii/base/ActionEvent.php b/framework/yii/base/ActionEvent.php index adfeddc..293d5bd 100644 --- a/framework/yii/base/ActionEvent.php +++ b/framework/yii/base/ActionEvent.php @@ -11,10 +11,7 @@ namespace yii\base; * ActionEvent represents the event parameter used for an action event. * * By setting the [[isValid]] property, one may control whether to continue running the action. - * @property boolean $isValid Event handlers of - * [[Controller::EVENT_BEFORE_ACTION]] may set this property to decide whether - * to continue running the current action. - * + * * @author Qiang Xue * @since 2.0 */ @@ -28,6 +25,12 @@ class ActionEvent extends Event * @var mixed the action result. Event handlers may modify this property to change the action result. */ public $result; + /** + * @var boolean whether to continue running the action. Event handlers of + * [[Controller::EVENT_BEFORE_ACTION]] may set this property to decide whether + * to continue running the current action. + */ + public $isValid = true; /** * Constructor. @@ -39,4 +42,4 @@ class ActionEvent extends Event $this->action = $action; parent::__construct($config); } -} +} \ No newline at end of file diff --git a/framework/yii/base/Event.php b/framework/yii/base/Event.php index 4c8ffcb..ebd7193 100644 --- a/framework/yii/base/Event.php +++ b/framework/yii/base/Event.php @@ -46,11 +46,6 @@ class Event extends Object */ public $data; - /** - * @var boolean whether to continue execution of process or not. - */ - public $isValid = true; - private static $_events = []; /** @@ -185,4 +180,4 @@ class Event extends Object } } while (($class = get_parent_class($class)) !== false); } -} +} \ No newline at end of file diff --git a/framework/yii/base/MailEvent.php b/framework/yii/base/MailEvent.php index 188c7d3..3e0ae76 100644 --- a/framework/yii/base/MailEvent.php +++ b/framework/yii/base/MailEvent.php @@ -24,7 +24,13 @@ class MailEvent extends Event */ public $message; /** - * + * @var boolean if message send was successful */ public $isSuccessful; + /** + * @var boolean whether to continue send. Event handlers of + * [[\yii\mail\BaseMailer::EVENT_BEFORE_SEND]] may set this property to decide whether + * to continue send or not. + */ + public $isValid = true; } diff --git a/framework/yii/base/ModelEvent.php b/framework/yii/base/ModelEvent.php index b4b602d..f10f83a 100644 --- a/framework/yii/base/ModelEvent.php +++ b/framework/yii/base/ModelEvent.php @@ -11,11 +11,15 @@ namespace yii\base; * ModelEvent class. * * ModelEvent represents the parameter needed by model events. - * @property boolean $isValid A model is in valid status if it passes validations or certain checks. - * + * * @author Qiang Xue * @since 2.0 */ class ModelEvent extends Event { -} + /** + * @var boolean whether the model is in valid status. Defaults to true. + * A model is in valid status if it passes validations or certain checks. + */ + public $isValid = true; +} \ No newline at end of file diff --git a/framework/yii/base/ViewEvent.php b/framework/yii/base/ViewEvent.php index 70ba96d..ec4d461 100644 --- a/framework/yii/base/ViewEvent.php +++ b/framework/yii/base/ViewEvent.php @@ -10,11 +10,6 @@ namespace yii\base; /** * ViewEvent represents events triggered by the [[View]] component. * - * @property boolean $isValid whether to continue rendering the view file. Event handlers of - *[[View::EVENT_BEFORE_RENDER]] may set this property to decide whether - * to continue rendering the current view file. - * - * * @author Qiang Xue * @since 2.0 */ @@ -31,6 +26,12 @@ class ViewEvent extends Event * @var string the view file path that is being rendered by [[View::renderFile()]]. */ public $viewFile; + /** + * @var boolean whether to continue rendering the view file. Event handlers of + * [[View::EVENT_BEFORE_RENDER]] may set this property to decide whether + * to continue rendering the current view file. + */ + public $isValid = true; /** * Constructor. @@ -42,4 +43,4 @@ class ViewEvent extends Event $this->viewFile = $viewFile; parent::__construct($config); } -} +} \ No newline at end of file From c0a2201143e3f9f681d94b078fca6d54edcc5fa6 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 30 Dec 2013 18:30:43 +0400 Subject: [PATCH 5/6] end of line fix --- framework/yii/base/ActionEvent.php | 2 +- framework/yii/base/Event.php | 2 +- framework/yii/base/ModelEvent.php | 2 +- framework/yii/base/ViewEvent.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/framework/yii/base/ActionEvent.php b/framework/yii/base/ActionEvent.php index 293d5bd..6e123a0 100644 --- a/framework/yii/base/ActionEvent.php +++ b/framework/yii/base/ActionEvent.php @@ -42,4 +42,4 @@ class ActionEvent extends Event $this->action = $action; parent::__construct($config); } -} \ No newline at end of file +} diff --git a/framework/yii/base/Event.php b/framework/yii/base/Event.php index ebd7193..974a1a4 100644 --- a/framework/yii/base/Event.php +++ b/framework/yii/base/Event.php @@ -180,4 +180,4 @@ class Event extends Object } } while (($class = get_parent_class($class)) !== false); } -} \ No newline at end of file +} diff --git a/framework/yii/base/ModelEvent.php b/framework/yii/base/ModelEvent.php index f10f83a..57e41f9 100644 --- a/framework/yii/base/ModelEvent.php +++ b/framework/yii/base/ModelEvent.php @@ -22,4 +22,4 @@ class ModelEvent extends Event * A model is in valid status if it passes validations or certain checks. */ public $isValid = true; -} \ No newline at end of file +} diff --git a/framework/yii/base/ViewEvent.php b/framework/yii/base/ViewEvent.php index ec4d461..d02e180 100644 --- a/framework/yii/base/ViewEvent.php +++ b/framework/yii/base/ViewEvent.php @@ -43,4 +43,4 @@ class ViewEvent extends Event $this->viewFile = $viewFile; parent::__construct($config); } -} \ No newline at end of file +} From 12e46921b36fc13841fbaa74ed605b8d29edf2d6 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 30 Dec 2013 18:40:10 +0400 Subject: [PATCH 6/6] line fix --- framework/yii/base/MailEvent.php | 1 - 1 file changed, 1 deletion(-) diff --git a/framework/yii/base/MailEvent.php b/framework/yii/base/MailEvent.php index 3e0ae76..6e9da2e 100644 --- a/framework/yii/base/MailEvent.php +++ b/framework/yii/base/MailEvent.php @@ -15,7 +15,6 @@ namespace yii\base; * @author Mark Jebri * @since 2.0 */ - class MailEvent extends Event {