ActionFilter.php 2.33 KB
Newer Older
Qiang Xue committed
1 2
<?php
/**
Qiang Xue committed
3
 * ActionFilter class file.
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
10 11
namespace yii\base;

Qiang Xue committed
12
/**
13 14 15 16
 * ActionFilter is the base class for all action filters.
 *
 * A filter can be applied to a controller action at different stages of its life cycle. In particular,
 * it responds to the following events that are raised when an action is being executed:
Qiang Xue committed
17
 *
18 19 20 21 22
 * 1. authorize
 * 2. beforeAction
 * 3. beforeRender
 * 4. afterRender
 * 5. afterAction
Qiang Xue committed
23
 *
24 25
 * Derived classes may respond to these events by overriding the corresponding methods in this class.
 * For example, to create an access control filter, one may override the [[authorize()]] method.
Qiang Xue committed
26 27
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
28
 * @since 2.0
Qiang Xue committed
29
 */
30
class ActionFilter extends Behavior
Qiang Xue committed
31 32
{
	/**
33
	 * @var Controller the owner of this behavior. For action filters, this should be a controller object.
Qiang Xue committed
34
	 */
35 36
	public $owner;
	/**
Qiang Xue committed
37
	 * @var array IDs of actions that this filter applies to.
38 39 40 41 42 43
	 * If this property is empty or not set, it means this filter applies to all actions.
	 * Note that if an action appears in [[except]], the filter will not apply to this action, even
	 * if the action also appears in [[only]].
	 * @see exception
	 */
	public $only;
Qiang Xue committed
44
	/**
Qiang Xue committed
45
	 * @var array IDs of actions that this filter does NOT apply to.
Qiang Xue committed
46
	 */
47 48
	public $except;

Qiang Xue committed
49 50
	public function init()
	{
51 52 53 54 55
		$this->owner->on('authorize', array($this, 'handleEvent'));
		$this->owner->on('beforeAction', array($this, 'handleEvent'));
		$this->owner->on('beforeRender', array($this, 'handleEvent'));
		$this->owner->getEventHandlers('afterRender')->insertAt(0, array($this, 'handleEvent'));
		$this->owner->getEventHandlers('afterAction')->insertAt(0, array($this, 'handleEvent'));
Qiang Xue committed
56 57
	}

Qiang Xue committed
58
	public function authorize($event)
Qiang Xue committed
59 60 61
	{
	}

Qiang Xue committed
62
	public function beforeAction($event)
63 64 65
	{
	}

Qiang Xue committed
66
	public function beforeRender($event)
67 68 69
	{
	}

Qiang Xue committed
70
	public function afterRender($event)
71 72 73
	{
	}

Qiang Xue committed
74
	public function afterAction($event)
75 76 77
	{
	}

Qiang Xue committed
78
	public function handleEvent($event)
79 80 81 82 83 84 85
	{
		if ($this->applyTo($event->action)) {
			$this->{$event->name}($event);
		}
	}

	public function applyTo(Action $action)
Qiang Xue committed
86
	{
Qiang Xue committed
87 88
		return (empty($this->only) || in_array($action->id, $this->only, false) !== false)
			&& (empty($this->except) || in_array($action->id, $this->except, false) === false);
Qiang Xue committed
89 90
	}
}