1: <?php
 2: /**
 3:  * CFilter 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:  * CFilter is the base class for all filters.
13:  *
14:  * A filter can be applied before and after an action is executed.
15:  * It can modify the context that the action is to run or decorate the result that the
16:  * action generates.
17:  *
18:  * Override {@link preFilter()} to specify the filtering logic that should be applied
19:  * before the action, and {@link postFilter()} for filtering logic after the action.
20:  *
21:  * @author Qiang Xue <qiang.xue@gmail.com>
22:  * @package system.web.filters
23:  * @since 1.0
24:  */
25: class CFilter extends CComponent implements IFilter
26: {
27:     /**
28:      * Performs the filtering.
29:      * The default implementation is to invoke {@link preFilter}
30:      * and {@link postFilter} which are meant to be overridden
31:      * child classes. If a child class needs to override this method,
32:      * make sure it calls <code>$filterChain->run()</code>
33:      * if the action should be executed.
34:      * @param CFilterChain $filterChain the filter chain that the filter is on.
35:      */
36:     public function filter($filterChain)
37:     {
38:         if($this->preFilter($filterChain))
39:         {
40:             $filterChain->run();
41:             $this->postFilter($filterChain);
42:         }
43:     }
44: 
45:     /**
46:      * Initializes the filter.
47:      * This method is invoked after the filter properties are initialized
48:      * and before {@link preFilter} is called.
49:      * You may override this method to include some initialization logic.
50:      * @since 1.1.4
51:      */
52:     public function init()
53:     {
54:     }
55: 
56:     /**
57:      * Performs the pre-action filtering.
58:      * @param CFilterChain $filterChain the filter chain that the filter is on.
59:      * @return boolean whether the filtering process should continue and the action
60:      * should be executed.
61:      */
62:     protected function preFilter($filterChain)
63:     {
64:         return true;
65:     }
66: 
67:     /**
68:      * Performs the post-action filtering.
69:      * @param CFilterChain $filterChain the filter chain that the filter is on.
70:      */
71:     protected function postFilter($filterChain)
72:     {
73:     }
74: }