~horux-dev/horux-webcli/thfo

« back to all changes in this revision

Viewing changes to yii/framework/web/filters/CFilterChain.php

  • Committer: Thierry Forchelet
  • Date: 2011-02-25 13:30:15 UTC
  • Revision ID: thierry.forchelet@letux.ch-20110225133015-zxyj9w7sqv8ly971
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CFilterChain class file.
 
4
 *
 
5
 * @author Qiang Xue <qiang.xue@gmail.com>
 
6
 * @link http://www.yiiframework.com/
 
7
 * @copyright Copyright &copy; 2008-2011 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
 * @version $Id: CFilterChain.php 2799 2011-01-01 19:31:13Z qiang.xue $
 
19
 * @package system.web.filters
 
20
 * @since 1.0
 
21
 */
 
22
class CFilterChain extends CList
 
23
{
 
24
        /**
 
25
         * @var CController the controller who executes the action.
 
26
         */
 
27
        public $controller;
 
28
        /**
 
29
         * @var CAction the action being filtered by this chain.
 
30
         */
 
31
        public $action;
 
32
        /**
 
33
         * @var integer the index of the filter that is to be executed when calling {@link run()}.
 
34
         */
 
35
        public $filterIndex=0;
 
36
 
 
37
 
 
38
        /**
 
39
         * Constructor.
 
40
         * @param CController $controller the controller who executes the action.
 
41
         * @param CAction $action the action being filtered by this chain.
 
42
         */
 
43
        public function __construct($controller,$action)
 
44
        {
 
45
                $this->controller=$controller;
 
46
                $this->action=$action;
 
47
        }
 
48
 
 
49
        /**
 
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.
 
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
                        else if(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
}
 
 
b'\\ No newline at end of file'