1: <?php
2: /**
3: * CFilterChain class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11:
12: /**
13: * CFilterChain represents a list of filters being applied to an action.
14: *
15: * CFilterChain executes the filter list by {@link run()}.
16: *
17: * @author Qiang Xue <qiang.xue@gmail.com>
18: * @package system.web.filters
19: * @since 1.0
20: */
21: class CFilterChain extends CList
22: {
23: /**
24: * @var CController the controller who executes the action.
25: */
26: public $controller;
27: /**
28: * @var CAction the action being filtered by this chain.
29: */
30: public $action;
31: /**
32: * @var integer the index of the filter that is to be executed when calling {@link run()}.
33: */
34: public $filterIndex=0;
35:
36:
37: /**
38: * Constructor.
39: * @param CController $controller the controller who executes the action.
40: * @param CAction $action the action being filtered by this chain.
41: */
42: public function __construct($controller,$action)
43: {
44: $this->controller=$controller;
45: $this->action=$action;
46: }
47:
48: /**
49: * CFilterChain factory method.
50: * This method creates a CFilterChain instance.
51: * @param CController $controller the controller who executes the action.
52: * @param CAction $action the action being filtered by this chain.
53: * @param array $filters list of filters to be applied to the action.
54: * @return CFilterChain
55: */
56: public static function create($controller,$action,$filters)
57: {
58: $chain=new CFilterChain($controller,$action);
59:
60: $actionID=$action->getId();
61: foreach($filters as $filter)
62: {
63: if(is_string($filter)) // filterName [+|- action1 action2]
64: {
65: if(($pos=strpos($filter,'+'))!==false || ($pos=strpos($filter,'-'))!==false)
66: {
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)));
70: }
71: else
72: $filter=CInlineFilter::create($controller,$filter);
73: }
74: elseif(is_array($filter)) // array('path.to.class [+|- action1, action2]','param1'=>'value1',...)
75: {
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];
79: unset($filter[0]);
80: if(($pos=strpos($filterClass,'+'))!==false || ($pos=strpos($filterClass,'-'))!==false)
81: {
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));
85: else
86: continue;
87: }
88: $filter['class']=$filterClass;
89: $filter=Yii::createComponent($filter);
90: }
91:
92: if(is_object($filter))
93: {
94: $filter->init();
95: $chain->add($filter);
96: }
97: }
98: return $chain;
99: }
100:
101: /**
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.
109: */
110: public function insertAt($index,$item)
111: {
112: if($item instanceof IFilter)
113: parent::insertAt($index,$item);
114: else
115: throw new CException(Yii::t('yii','CFilterChain can only take objects implementing the IFilter interface.'));
116: }
117:
118: /**
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.
123: */
124: public function run()
125: {
126: if($this->offsetExists($this->filterIndex))
127: {
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);
131: }
132: else
133: $this->controller->runAction($this->action);
134: }
135: }