Commit 3a365dae by Qiang Xue

Added new events.

parent 611052c4
...@@ -19,6 +19,23 @@ use yii\web\HttpException; ...@@ -19,6 +19,23 @@ use yii\web\HttpException;
abstract class Application extends Module 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. * @var string the application name.
*/ */
public $name = 'My Application'; public $name = 'My Application';
...@@ -146,7 +163,9 @@ abstract class Application extends Module ...@@ -146,7 +163,9 @@ abstract class Application extends Module
*/ */
public function run() public function run()
{ {
$this->trigger(self::EVENT_BEFORE_REQUEST);
$response = $this->handleRequest($this->getRequest()); $response = $this->handleRequest($this->getRequest());
$this->trigger(self::EVENT_AFTER_REQUEST);
$response->send(); $response->send();
return $response->exitStatus; return $response->exitStatus;
} }
......
...@@ -18,16 +18,6 @@ use Yii; ...@@ -18,16 +18,6 @@ use Yii;
class Controller extends Component 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 * @var string the ID of this controller
*/ */
public $id; public $id;
...@@ -111,12 +101,15 @@ class Controller extends Component ...@@ -111,12 +101,15 @@ class Controller extends Component
$oldAction = $this->action; $oldAction = $this->action;
$this->action = $action; $this->action = $action;
$result = null; $result = null;
if ($this->module->beforeAction($action)) { $event = new ActionEvent($action);
if ($this->beforeAction($action)) { $this->trigger(Application::EVENT_BEFORE_ACTION, $event);
$result = $action->runWithParams($params); if ($event->isValid && $this->module->beforeAction($action) && $this->beforeAction($action)) {
$this->afterAction($action, $result); $result = $action->runWithParams($params);
} $this->afterAction($action, $result);
$this->module->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; $this->action = $oldAction;
return $result; return $result;
...@@ -199,9 +192,7 @@ class Controller extends Component ...@@ -199,9 +192,7 @@ class Controller extends Component
*/ */
public function beforeAction($action) public function beforeAction($action)
{ {
$event = new ActionEvent($action); return true;
$this->trigger(self::EVENT_BEFORE_ACTION, $event);
return $event->isValid;
} }
/** /**
...@@ -212,9 +203,6 @@ class Controller extends Component ...@@ -212,9 +203,6 @@ class Controller extends Component
*/ */
public function afterAction($action, &$result) public function afterAction($action, &$result)
{ {
$event = new ActionEvent($action);
$event->result = &$result;
$this->trigger(self::EVENT_AFTER_ACTION, $event);
} }
/** /**
......
...@@ -37,15 +37,6 @@ use Yii; ...@@ -37,15 +37,6 @@ use Yii;
abstract class Module extends Component 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). * @var array custom module parameters (name => value).
*/ */
public $params = array(); public $params = array();
...@@ -650,28 +641,25 @@ abstract class Module extends Component ...@@ -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. * 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. * @param Action $action the action to be executed.
* @return boolean whether the action should continue to be executed. * @return boolean whether the action should continue to be executed.
*/ */
public function beforeAction($action) public function beforeAction($action)
{ {
$event = new ActionEvent($action); return true;
$this->trigger(self::EVENT_BEFORE_ACTION, $event);
return $event->isValid;
} }
/** /**
* 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. * 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 Action $action the action just executed.
* @param mixed $result the action return result. * @param mixed $result the action return result.
*/ */
public function afterAction($action, &$result) public function afterAction($action, &$result)
{ {
$event = new ActionEvent($action);
$event->result = &$result;
$this->trigger(self::EVENT_AFTER_ACTION, $event);
} }
} }
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\debug;
use Yii;
use yii\base\Component;
use yii\base\View;
use yii\helpers\Html;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @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',
));
}
}
...@@ -55,9 +55,6 @@ class LogTarget extends Target ...@@ -55,9 +55,6 @@ class LogTarget extends Target
*/ */
public function collect($messages, $final) public function collect($messages, $final)
{ {
if (Yii::$app->getModule('debug', false) !== null) {
return;
}
$this->messages = array_merge($this->messages, $this->filterMessages($messages)); $this->messages = array_merge($this->messages, $this->filterMessages($messages));
if ($final) { if ($final) {
$this->export($this->messages); $this->export($this->messages);
......
...@@ -7,6 +7,10 @@ ...@@ -7,6 +7,10 @@
namespace yii\debug; namespace yii\debug;
use Yii;
use yii\base\View;
use yii\helpers\Html;
/** /**
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
...@@ -15,4 +19,34 @@ class Module extends \yii\base\Module ...@@ -15,4 +19,34 @@ class Module extends \yii\base\Module
{ {
public $controllerNamespace = 'yii\debug\controllers'; public $controllerNamespace = 'yii\debug\controllers';
public $panels; 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',
));
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment