Added yii controller-events extension

This commit is contained in:
Lucas Bartholemy 2014-05-05 20:39:06 +02:00
parent 1e4f45ef56
commit 813cdae234
7 changed files with 164 additions and 2 deletions

View File

@ -25,7 +25,7 @@
* @package humhub.components
* @since 0.5
*/
class Controller extends CController {
class Controller extends EController {
/**
* @var string the default layout for the controller view. Defaults to '//layouts/column1',

View File

@ -71,6 +71,7 @@ return array(
// 3rd Party Extensions
'ext.yii-mail.YiiMailMessage',
'ext.EZendAutoloader.EZendAutoloader',
'ext.controller-events.*'
),
// application-level parameters that can be accessed
// using Yii::app()->params['paramName']

View File

@ -21,4 +21,4 @@ Bundled Software / Libaries
* Highlight - http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html
* Timeago - http://timeago.yarp.com/
* NiceScroll - https://github.com/inuyaksa/jquery.nicescroll
* Controller-Events - http://www.yiiframework.com/extension/controller-events/

View File

@ -0,0 +1,101 @@
<?php
class EController extends CController {
/**
* This method is invoked at the beginning of {@link render()}.
*/
protected function beforeRender($view) {
if ($this->hasEventHandler('onBeforeRender')) {
$event = new EControllerEvent($this);
$event->view = $view;
$this->onBeforeRender($event);
return $event->isValid;
}
return true;
}
/**
* This method is invoked after the specified view is rendered by calling {@link render()}.
*/
protected function afterRender($view, &$output) {
if ($this->hasEventHandler('onAfterRender')) {
$event = new EControllerEvent($this);
$event->view = $view;
$event->output = &$output;
$this->onAfterRender($event);
}
}
/**
* This method is invoked right before an action is to be executed (after all possible filters.)
*/
protected function beforeAction($action) {
if ($this->hasEventHandler('onBeforeAction')) {
$event = new EControllerEvent($this);
$event->action = $action;
$this->onBeforeAction($event);
return $event->isValid;
}
return true;
}
/**
* This method is invoked right after an action is executed.
*/
protected function afterAction($action) {
if ($this->hasEventHandler('onAfterAction')) {
$event = new EControllerEvent($this);
$event->action = $action;
$this->onAfterAction($event);
}
}
/**
* This event is raised before the renderer.
* @param CEvent $event the event parameter
*/
public function onBeforeRender($event) {
$this->raiseEvent('onBeforeRender',$event);
}
/**
* This event is raised after the renderer.
* @param CEvent $event the event parameter
*/
public function onAfterRender($event) {
$this->raiseEvent('onAfterRender',$event);
}
/**
* This event is raised before an action is executed.
* @param CEvent $event the event parameter
*/
public function onBeforeAction($event) {
$this->raiseEvent('onBeforeAction',$event);
}
/**
* This event is raised after an action is executed.
* @param CEvent $event the event parameter
*/
public function onAfterAction($event) {
$this->raiseEvent('onAfterAction',$event);
}
/**
* Creates the action instance based on the action name.
* Additionally, this implementing checks attached behaviors for actions.
*/
public function createAction($actionID) {
$action = parent::createAction($actionID);
// search in behaviors
if ($action === null) {
foreach ($this->behaviors() as $behavior => $data) {
$behavior = $this->{$behavior};
if (is_subclass_of($behavior, 'IBehavior') && method_exists($behavior, 'action'.$actionID)) {
return new CInlineAction($behavior, $actionID);
}
}
}
return $action;
}
}

View File

@ -0,0 +1,19 @@
<?php
class EControllerBehavior extends CBehavior {
public function events() {
return array(
'onBeforeAction' => 'beforeAction',
'onAfterAction' => 'afterAction',
'onBeforeRender' => 'beforeRender',
'onAfterRender' => 'afterRender',
);
}
protected function beforeAction(EControllerEvent $event) {}
protected function afterAction(EControllerEvent $event) {}
protected function beforeRender(EControllerEvent $event) {}
protected function afterRender(EControllerEvent $event) {}
}

View File

@ -0,0 +1,7 @@
<?php
class EControllerEvent extends CEvent {
public $view;
public $output;
public $isValid = true;
public $action;
}

View File

@ -0,0 +1,34 @@
## controller-events
EControllerBehavior is a behavior that can handle controller events:
* beforeAction
* afterAction
* beforeRender
* afterRender
Your controller should extend EController.
You create a behavior by extending EControllerBehavior and implement one of above listed methods.
Inside a handler you have `$event` instance, which has following properties:
* in beforeRender: `$event->view`.
* in afterRender: `$event->view` and `$event->output`.
* in beforeAction: `$event->action`.
* in afterAction: `$event->action`.
Also, in beforeAction and beforeRender you can set `$event->isValid` property to allow or deny corresponding processing.
Also, it supports actions inside behaviors.
**Example**
```php
// main.php
'import' => array('ext.controller-events.*'),
// CrudController.php
<?php
class CrudController extends EControllerBehavior {
public function beforeAction(EControllerEvent $event) {
if (rand(0, 1))
$event->isValid = false;
}
}
```