/
CFilterChain.php
136 lines (127 loc) · 4.14 KB
/
CFilterChain.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* CFilterChain class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008-2013 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CFilterChain represents a list of filters being applied to an action.
*
* CFilterChain executes the filter list by {@link run()}.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @package system.web.filters
* @since 1.0
*/
class CFilterChain extends CList
{
/**
* @var CController the controller who executes the action.
*/
public $controller;
/**
* @var CAction the action being filtered by this chain.
*/
public $action;
/**
* @var integer the index of the filter that is to be executed when calling {@link run()}.
*/
public $filterIndex=0;
/**
* Constructor.
* @param CController $controller the controller who executes the action.
* @param CAction $action the action being filtered by this chain.
*/
public function __construct($controller,$action)
{
$this->controller=$controller;
$this->action=$action;
}
/**
* CFilterChain factory method.
* This method creates a CFilterChain instance.
* @param CController $controller the controller who executes the action.
* @param CAction $action the action being filtered by this chain.
* @param array $filters list of filters to be applied to the action.
* @return CFilterChain
* @throws CException
*/
public static function create($controller,$action,$filters)
{
$chain=new CFilterChain($controller,$action);
$actionID=$action->getId();
foreach($filters as $filter)
{
if(is_string($filter)) // filterName [+|- action1 action2]
{
if(($pos=strpos($filter,'+'))!==false || ($pos=strpos($filter,'-'))!==false)
{
$matched=preg_match("/\b{$actionID}\b/i",substr($filter,$pos+1))>0;
if(($filter[$pos]==='+')===$matched)
$filter=CInlineFilter::create($controller,trim(substr($filter,0,$pos)));
}
else
$filter=CInlineFilter::create($controller,$filter);
}
elseif(is_array($filter)) // array('path.to.class [+|- action1, action2]','param1'=>'value1',...)
{
if(!isset($filter[0]))
throw new CException(Yii::t('yii','The first element in a filter configuration must be the filter class.'));
$filterClass=$filter[0];
unset($filter[0]);
if(($pos=strpos($filterClass,'+'))!==false || ($pos=strpos($filterClass,'-'))!==false)
{
$matched=preg_match("/\b{$actionID}\b/i",substr($filterClass,$pos+1))>0;
if(($filterClass[$pos]==='+')===$matched)
$filterClass=trim(substr($filterClass,0,$pos));
else
continue;
}
$filter['class']=$filterClass;
$filter=Yii::createComponent($filter);
}
if(is_object($filter))
{
$filter->init();
$chain->add($filter);
}
}
return $chain;
}
/**
* Inserts an item at the specified position.
* This method overrides the parent implementation by adding
* additional check for the item to be added. In particular,
* only objects implementing {@link IFilter} can be added to the list.
* @param integer $index the specified position.
* @param mixed $item new item
* @throws CException If the index specified exceeds the bound or the list is read-only, or the item is not an {@link IFilter} instance.
*/
public function insertAt($index,$item)
{
if($item instanceof IFilter)
parent::insertAt($index,$item);
else
throw new CException(Yii::t('yii','CFilterChain can only take objects implementing the IFilter interface.'));
}
/**
* Executes the filter indexed at {@link filterIndex}.
* After this method is called, {@link filterIndex} will be automatically incremented by one.
* This method is usually invoked in filters so that the filtering process
* can continue and the action can be executed.
*/
public function run()
{
if($this->offsetExists($this->filterIndex))
{
$filter=$this->itemAt($this->filterIndex++);
Yii::trace('Running filter '.($filter instanceof CInlineFilter ? get_class($this->controller).'.filter'.$filter->name.'()':get_class($filter).'.filter()'),'system.web.filters.CFilterChain');
$filter->filter($this);
}
else
$this->controller->runAction($this->action);
}
}