3
* CFilterChain class file.
5
* @author Qiang Xue <qiang.xue@gmail.com>
6
* @link http://www.yiiframework.com/
7
* @copyright Copyright © 2008-2011 Yii Software LLC
8
* @license http://www.yiiframework.com/license/
13
* CFilterChain represents a list of filters being applied to an action.
15
* CFilterChain executes the filter list by {@link run()}.
17
* @author Qiang Xue <qiang.xue@gmail.com>
18
* @version $Id: CFilterChain.php 2799 2011-01-01 19:31:13Z qiang.xue $
19
* @package system.web.filters
22
class CFilterChain extends CList
25
* @var CController the controller who executes the action.
29
* @var CAction the action being filtered by this chain.
33
* @var integer the index of the filter that is to be executed when calling {@link run()}.
35
public $filterIndex=0;
40
* @param CController $controller the controller who executes the action.
41
* @param CAction $action the action being filtered by this chain.
43
public function __construct($controller,$action)
45
$this->controller=$controller;
46
$this->action=$action;
50
* CFilterChain factory method.
51
* This method creates a CFilterChain instance.
52
* @param CController $controller the controller who executes the action.
53
* @param CAction $action the action being filtered by this chain.
54
* @param array $filters list of filters to be applied to the action.
56
public static function create($controller,$action,$filters)
58
$chain=new CFilterChain($controller,$action);
60
$actionID=$action->getId();
61
foreach($filters as $filter)
63
if(is_string($filter)) // filterName [+|- action1 action2]
65
if(($pos=strpos($filter,'+'))!==false || ($pos=strpos($filter,'-'))!==false)
67
$matched=preg_match("/\b{$actionID}\b/i",substr($filter,$pos+1))>0;
68
if(($filter[$pos]==='+')===$matched)
69
$filter=CInlineFilter::create($controller,trim(substr($filter,0,$pos)));
72
$filter=CInlineFilter::create($controller,$filter);
74
else if(is_array($filter)) // array('path.to.class [+|- action1, action2]','param1'=>'value1',...)
76
if(!isset($filter[0]))
77
throw new CException(Yii::t('yii','The first element in a filter configuration must be the filter class.'));
78
$filterClass=$filter[0];
80
if(($pos=strpos($filterClass,'+'))!==false || ($pos=strpos($filterClass,'-'))!==false)
82
$matched=preg_match("/\b{$actionID}\b/i",substr($filterClass,$pos+1))>0;
83
if(($filterClass[$pos]==='+')===$matched)
84
$filterClass=trim(substr($filterClass,0,$pos));
88
$filter['class']=$filterClass;
89
$filter=Yii::createComponent($filter);
92
if(is_object($filter))
102
* Inserts an item at the specified position.
103
* This method overrides the parent implementation by adding
104
* additional check for the item to be added. In particular,
105
* only objects implementing {@link IFilter} can be added to the list.
106
* @param integer $index the specified position.
107
* @param mixed $item new item
108
* @throws CException If the index specified exceeds the bound or the list is read-only, or the item is not an {@link IFilter} instance.
110
public function insertAt($index,$item)
112
if($item instanceof IFilter)
113
parent::insertAt($index,$item);
115
throw new CException(Yii::t('yii','CFilterChain can only take objects implementing the IFilter interface.'));
119
* Executes the filter indexed at {@link filterIndex}.
120
* After this method is called, {@link filterIndex} will be automatically incremented by one.
121
* This method is usually invoked in filters so that the filtering process
122
* can continue and the action can be executed.
124
public function run()
126
if($this->offsetExists($this->filterIndex))
128
$filter=$this->itemAt($this->filterIndex++);
129
Yii::trace('Running filter '.($filter instanceof CInlineFilter ? get_class($this->controller).'.filter'.$filter->name.'()':get_class($filter).'.filter()'),'system.web.filters.CFilterChain');
130
$filter->filter($this);
133
$this->controller->runAction($this->action);
b'\\ No newline at end of file'