Browse Source

Added new events.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
3a365dae49
  1. 19
      framework/yii/base/Application.php
  2. 30
      framework/yii/base/Controller.php
  3. 22
      framework/yii/base/Module.php
  4. 54
      framework/yii/debug/Debugger.php
  5. 3
      framework/yii/debug/LogTarget.php
  6. 34
      framework/yii/debug/Module.php

19
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;
}

30
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);
}
/**

22
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);
}
}

54
framework/yii/debug/Debugger.php

@ -1,54 +0,0 @@
<?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',
));
}
}

3
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);

34
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 <qiang.xue@gmail.com>
* @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',
));
}
}

Loading…
Cancel
Save