mirror of
https://github.com/humhub/humhub.git
synced 2025-01-17 22:28:51 +01:00
Added yii controller-events extension
This commit is contained in:
parent
1e4f45ef56
commit
813cdae234
@ -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',
|
||||
|
@ -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']
|
||||
|
@ -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/
|
||||
|
101
protected/extensions/controller-events/EController.php
Normal file
101
protected/extensions/controller-events/EController.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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) {}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
class EControllerEvent extends CEvent {
|
||||
public $view;
|
||||
public $output;
|
||||
public $isValid = true;
|
||||
public $action;
|
||||
}
|
34
protected/extensions/controller-events/README.md
Normal file
34
protected/extensions/controller-events/README.md
Normal 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;
|
||||
}
|
||||
}
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user