1: <?php
2: /**
3: * CInlineFilter 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: * CInlineFilter represents a filter defined as a controller method.
13: *
14: * CInlineFilter executes the 'filterXYZ($action)' method defined
15: * in the controller, where the name 'XYZ' can be retrieved from the {@link name} property.
16: *
17: * @author Qiang Xue <qiang.xue@gmail.com>
18: * @package system.web.filters
19: * @since 1.0
20: */
21: class CInlineFilter extends CFilter
22: {
23: /**
24: * @var string name of the filter. It stands for 'XYZ' in the filter method name 'filterXYZ'.
25: */
26: public $name;
27:
28: /**
29: * Creates an inline filter instance.
30: * The creation is based on a string describing the inline method name
31: * and action names that the filter shall or shall not apply to.
32: * @param CController $controller the controller who hosts the filter methods
33: * @param string $filterName the filter name
34: * @return CInlineFilter the created instance
35: * @throws CException if the filter method does not exist
36: */
37: public static function create($controller,$filterName)
38: {
39: if(method_exists($controller,'filter'.$filterName))
40: {
41: $filter=new CInlineFilter;
42: $filter->name=$filterName;
43: return $filter;
44: }
45: else
46: throw new CException(Yii::t('yii','Filter "{filter}" is invalid. Controller "{class}" does not have the filter method "filter{filter}".',
47: array('{filter}'=>$filterName, '{class}'=>get_class($controller))));
48: }
49:
50: /**
51: * Performs the filtering.
52: * This method calls the filter method defined in the controller class.
53: * @param CFilterChain $filterChain the filter chain that the filter is on.
54: */
55: public function filter($filterChain)
56: {
57: $method='filter'.$this->name;
58: $filterChain->controller->$method($filterChain);
59: }
60: }
61: