0 follower

CFilterChain

Package system.web.filters
Inheritance class CFilterChain » CList » CComponent
Implements Countable, ArrayAccess, Traversable, IteratorAggregate
Since 1.0
Version $Id$
Source Code framework/web/filters/CFilterChain.php
CFilterChain represents a list of filters being applied to an action.

CFilterChain executes the filter list by run().

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
action CAction the action being filtered by this chain. CFilterChain
controller CController the controller who executes the action. CFilterChain
count integer the number of items in the list CList
filterIndex integer the index of the filter that is to be executed when calling run(). CFilterChain
iterator Iterator Returns an iterator for traversing the items in the list. CList
readOnly boolean whether this list is read-only or not. CList

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CFilterChain
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
add() Appends an item at the end of the list. CList
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
clear() Removes all items in the list. CList
contains() CList
copyFrom() Copies iterable data into the list. CList
count() Returns the number of items in the list. CList
create() CFilterChain factory method. CFilterChain
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
getCount() Returns the number of items in the list CList
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getIterator() Returns an iterator for traversing the items in the list. CList
getReadOnly() Returns whether this list is read-only or not. Defaults to false. CList
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
indexOf() CList
insertAt() Inserts an item at the specified position. CFilterChain
itemAt() Returns the item at the specified offset. CList
mergeWith() Merges iterable data into the map. CList
offsetExists() Returns whether there is an item at the specified offset. CList
offsetGet() Returns the item at the specified offset. CList
offsetSet() Sets the item at the specified offset. CList
offsetUnset() Unsets the item at the specified offset. CList
raiseEvent() Raises an event. CComponent
remove() Removes an item from the list. CList
removeAt() Removes an item at the specified position. CList
run() Executes the filter indexed at filterIndex. CFilterChain
toArray() CList

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
setReadOnly() Sets whether this list is read-only or not CList

Property Details

action property
public CAction $action;

the action being filtered by this chain.

controller property
public CController $controller;

the controller who executes the action.

filterIndex property
public integer $filterIndex;

the index of the filter that is to be executed when calling run().

Method Details

__construct() method
public void __construct(CController $controller, CAction $action)
$controller CController the controller who executes the action.
$action CAction the action being filtered by this chain.
Source Code: framework/web/filters/CFilterChain.php#43 (show)
public function __construct($controller,$action)
{
    
$this->controller=$controller;
    
$this->action=$action;
}

Constructor.

create() method
public static void create(CController $controller, CAction $action, array $filters)
$controller CController the controller who executes the action.
$action CAction the action being filtered by this chain.
$filters array list of filters to be applied to the action.
Source Code: framework/web/filters/CFilterChain.php#56 (show)
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)
                    
$chain->add(CInlineFilter::create($controller,trim(substr($filter,0,$pos))));
            }
            else
                
$chain->add(CInlineFilter::create($controller,$filter));
        }
        else if(
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;
            
$chain->add(Yii::createComponent($filter));
        }
        else
            
$chain->add($filter);
    }
    return 
$chain;
}

CFilterChain factory method. This method creates a CFilterChain instance.

insertAt() method
public void insertAt(integer $index, mixed $item)
$index integer the specified position.
$item mixed new item
Source Code: framework/web/filters/CFilterChain.php#106 (show)
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.'));
}

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 IFilter can be added to the list.

run() method
public void run()
Source Code: framework/web/filters/CFilterChain.php#120 (show)
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);
}

Executes the filter indexed at filterIndex. After this method is called, 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.