ActionFilter.php 2.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\base;

/**
11 12 13 14 15
 * ActionFilter provides a base implementation for action filters that can be added to a controller
 * to handle the `beforeAction` event.
 *
 * Check implementation of [[AccessControl]], [[PageCache]] and [[HttpCache]] as examples on how to use it.
 *
16 17 18
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
19
class ActionFilter extends Behavior
20
{
Qiang Xue committed
21 22 23
	/**
	 * @var array list of action IDs that this filter should apply to. If this property is not set,
	 * then the filter applies to all actions, unless they are listed in [[except]].
Qiang Xue committed
24 25
	 * If an action ID appears in both [[only]] and [[except]], this filter will NOT apply to it.
	 * @see except
Qiang Xue committed
26 27 28 29
	 */
	public $only;
	/**
	 * @var array list of action IDs that this filter should not apply to.
Qiang Xue committed
30
	 * @see only
Qiang Xue committed
31
	 */
Alexander Makarov committed
32
	public $except = [];
Qiang Xue committed
33

34 35 36 37 38 39
	/**
	 * Declares event handlers for the [[owner]]'s events.
	 * @return array events (array keys) and the corresponding event handler methods (array values).
	 */
	public function events()
	{
Alexander Makarov committed
40
		return [
41 42
			Controller::EVENT_BEFORE_ACTION => 'beforeFilter',
			Controller::EVENT_AFTER_ACTION => 'afterFilter',
Alexander Makarov committed
43
		];
44 45 46 47 48 49
	}

	/**
	 * @param ActionEvent $event
	 * @return boolean
	 */
Qiang Xue committed
50
	public function beforeFilter($event)
51
	{
Qiang Xue committed
52 53 54
		if ($this->isActive($event->action)) {
			$event->isValid = $this->beforeAction($event->action);
		}
55 56 57 58 59 60 61
		return $event->isValid;
	}

	/**
	 * @param ActionEvent $event
	 * @return boolean
	 */
Qiang Xue committed
62
	public function afterFilter($event)
63
	{
Qiang Xue committed
64
		if ($this->isActive($event->action)) {
65
			$this->afterAction($event->action, $event->result);
Qiang Xue committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
		}
	}

	/**
	 * This method is invoked right before an action is to be executed (after all possible filters.)
	 * You may override this method to do last-minute preparation for the action.
	 * @param Action $action the action to be executed.
	 * @return boolean whether the action should continue to be executed.
	 */
	public function beforeAction($action)
	{
		return true;
	}

	/**
	 * This method is invoked right after an action is executed.
	 * You may override this method to do some postprocessing for the action.
	 * @param Action $action the action just executed.
84
	 * @param mixed $result the action execution result
Qiang Xue committed
85
	 */
86
	public function afterAction($action, &$result)
Qiang Xue committed
87 88
	{
	}
89

Qiang Xue committed
90 91 92 93 94 95 96 97
	/**
	 * Returns a value indicating whether the filer is active for the given action.
	 * @param Action $action the action being filtered
	 * @return boolean whether the filer is active for the given action.
	 */
	protected function isActive($action)
	{
		return !in_array($action->id, $this->except, true) && (empty($this->only) || in_array($action->id, $this->only, true));
98
	}
Zander Baldwin committed
99
}