From 3a365dae49d24b20228c83b256d700c254f061c1 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sun, 30 Jun 2013 18:35:21 -0400 Subject: [PATCH] Added new events. --- framework/yii/base/Application.php | 19 ++++++++++++++ framework/yii/base/Controller.php | 30 +++++++-------------- framework/yii/base/Module.php | 22 ++++------------ framework/yii/debug/Debugger.php | 54 -------------------------------------- framework/yii/debug/LogTarget.php | 3 --- framework/yii/debug/Module.php | 34 ++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 95 deletions(-) delete mode 100644 framework/yii/debug/Debugger.php diff --git a/framework/yii/base/Application.php b/framework/yii/base/Application.php index 41df6c0..2d2157c 100644 --- a/framework/yii/base/Application.php +++ b/framework/yii/base/Application.php @@ -19,6 +19,23 @@ use yii\web\HttpException; abstract class Application extends Module { /** + * @event Event an event raised before the application starts to handle a request. + */ + const EVENT_BEFORE_REQUEST = 'beforeRequest'; + /** + * @event Event an event raised after the application successfully handles a request (before the response is sent out). + */ + const EVENT_AFTER_REQUEST = 'afterRequest'; + /** + * @event ActionEvent an event raised before executing a controller action. + * You may set [[ActionEvent::isValid]] to be false to cancel the action execution. + */ + const EVENT_BEFORE_ACTION = 'beforeAction'; + /** + * @event ActionEvent an event raised after executing a controller action. + */ + const EVENT_AFTER_ACTION = 'afterAction'; + /** * @var string the application name. */ public $name = 'My Application'; @@ -146,7 +163,9 @@ abstract class Application extends Module */ public function run() { + $this->trigger(self::EVENT_BEFORE_REQUEST); $response = $this->handleRequest($this->getRequest()); + $this->trigger(self::EVENT_AFTER_REQUEST); $response->send(); return $response->exitStatus; } diff --git a/framework/yii/base/Controller.php b/framework/yii/base/Controller.php index 9b2b22b..3ed6736 100644 --- a/framework/yii/base/Controller.php +++ b/framework/yii/base/Controller.php @@ -18,16 +18,6 @@ use Yii; class Controller extends Component { /** - * @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_ACTION = 'beforeAction'; - /** - * @event ActionEvent an event raised right after executing a controller action. - */ - const EVENT_AFTER_ACTION = 'afterAction'; - - /** * @var string the ID of this controller */ public $id; @@ -111,12 +101,15 @@ class Controller extends Component $oldAction = $this->action; $this->action = $action; $result = null; - if ($this->module->beforeAction($action)) { - if ($this->beforeAction($action)) { - $result = $action->runWithParams($params); - $this->afterAction($action, $result); - } + $event = new ActionEvent($action); + $this->trigger(Application::EVENT_BEFORE_ACTION, $event); + if ($event->isValid && $this->module->beforeAction($action) && $this->beforeAction($action)) { + $result = $action->runWithParams($params); + $this->afterAction($action, $result); $this->module->afterAction($action, $result); + $event = new ActionEvent($action); + $event->result = &$result; + Yii::$app->trigger(Application::EVENT_AFTER_ACTION, $event); } $this->action = $oldAction; return $result; @@ -199,9 +192,7 @@ class Controller extends Component */ public function beforeAction($action) { - $event = new ActionEvent($action); - $this->trigger(self::EVENT_BEFORE_ACTION, $event); - return $event->isValid; + return true; } /** @@ -212,9 +203,6 @@ class Controller extends Component */ public function afterAction($action, &$result) { - $event = new ActionEvent($action); - $event->result = &$result; - $this->trigger(self::EVENT_AFTER_ACTION, $event); } /** diff --git a/framework/yii/base/Module.php b/framework/yii/base/Module.php index 795ab1f..3463474 100644 --- a/framework/yii/base/Module.php +++ b/framework/yii/base/Module.php @@ -37,15 +37,6 @@ use Yii; abstract class Module extends Component { /** - * @event ActionEvent an event raised before executing a controller action. - * You may set [[ActionEvent::isValid]] to be false to cancel the action execution. - */ - const EVENT_BEFORE_ACTION = 'beforeAction'; - /** - * @event ActionEvent an event raised after executing a controller action. - */ - const EVENT_AFTER_ACTION = 'afterAction'; - /** * @var array custom module parameters (name => value). */ public $params = array(); @@ -650,28 +641,25 @@ abstract class Module extends Component } /** - * This method is invoked right before an action is to be executed (after all possible filters.) + * This method is invoked right before an action of this module is to be executed (after all possible filters.) * You may override this method to do last-minute preparation for the action. + * Make sure you call the parent implementation so that the relevant event is triggered. * @param Action $action the action to be executed. * @return boolean whether the action should continue to be executed. */ public function beforeAction($action) { - $event = new ActionEvent($action); - $this->trigger(self::EVENT_BEFORE_ACTION, $event); - return $event->isValid; + return true; } /** - * This method is invoked right after an action is executed. + * This method is invoked right after an action of this module has been executed. * You may override this method to do some postprocessing for the action. + * Make sure you call the parent implementation so that the relevant event is triggered. * @param Action $action the action just executed. * @param mixed $result the action return result. */ public function afterAction($action, &$result) { - $event = new ActionEvent($action); - $event->result = &$result; - $this->trigger(self::EVENT_AFTER_ACTION, $event); } } diff --git a/framework/yii/debug/Debugger.php b/framework/yii/debug/Debugger.php deleted file mode 100644 index 93ccc26..0000000 --- a/framework/yii/debug/Debugger.php +++ /dev/null @@ -1,54 +0,0 @@ - - * @since 2.0 - */ -class Debugger extends Component -{ - public $debugAction = 'debug/default/toolbar'; - public $panels; - - public function init() - { - parent::init(); - Yii::$app->setModule('debug', array( - 'class' => 'yii\debug\Module', - 'panels' => $this->panels, - )); - Yii::$app->log->targets[] = new LogTarget; - Yii::$app->getView()->on(View::EVENT_END_BODY, array($this, 'renderToolbar')); - } - - public function renderToolbar($event) - { - if (Yii::$app->getModule('debug', false) !== null) { - return; - } - - /** @var View $view */ - $id = 'yii-debug-toolbar'; - $url = Yii::$app->getUrlManager()->createUrl($this->debugAction, array( - 'tag' => Yii::getLogger()->tag, - )); - $view = $event->sender; - $view->registerJs("yii.debug.load('$id', '$url');"); - $view->registerAssetBundle('yii/debug'); - echo Html::tag('div', '', array( - 'id' => $id, - 'style' => 'display: none', - )); - } -} diff --git a/framework/yii/debug/LogTarget.php b/framework/yii/debug/LogTarget.php index 1192cb3..584da6c 100644 --- a/framework/yii/debug/LogTarget.php +++ b/framework/yii/debug/LogTarget.php @@ -55,9 +55,6 @@ class LogTarget extends Target */ public function collect($messages, $final) { - if (Yii::$app->getModule('debug', false) !== null) { - return; - } $this->messages = array_merge($this->messages, $this->filterMessages($messages)); if ($final) { $this->export($this->messages); diff --git a/framework/yii/debug/Module.php b/framework/yii/debug/Module.php index a0cf883..84bf399 100644 --- a/framework/yii/debug/Module.php +++ b/framework/yii/debug/Module.php @@ -7,6 +7,10 @@ namespace yii\debug; +use Yii; +use yii\base\View; +use yii\helpers\Html; + /** * @author Qiang Xue * @since 2.0 @@ -15,4 +19,34 @@ class Module extends \yii\base\Module { public $controllerNamespace = 'yii\debug\controllers'; public $panels; + + public function init() + { + parent::init(); + Yii::$app->log->targets['debug'] = new LogTarget; + Yii::$app->getView()->on(View::EVENT_END_BODY, array($this, 'renderToolbar')); + } + + public function beforeAction($action) + { + Yii::$app->getView()->off(View::EVENT_END_BODY, array($this, 'renderToolbar')); + unset(Yii::$app->log->targets['debug']); + return parent::beforeAction($action); + } + + public function renderToolbar($event) + { + /** @var View $view */ + $id = 'yii-debug-toolbar'; + $url = Yii::$app->getUrlManager()->createUrl('debug/default/toolbar', array( + 'tag' => Yii::getLogger()->tag, + )); + $view = $event->sender; + $view->registerJs("yii.debug.load('$id', '$url');"); + $view->registerAssetBundle('yii/debug'); + echo Html::tag('div', '', array( + 'id' => $id, + 'style' => 'display: none', + )); + } }