[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/filter_chain/src/ -> lmbFilterChain.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/filter_chain/src/lmbInterceptingFilter.interface.php');
  10  
  11  /**

  12   *  lmbFilterChain is an implementation of InterceptinfFilter design pattern.

  13   *

  14   *  lmbFilterChain contains registered filters and controls execution of the chain.

  15   *  Usually used as a FrontController in Limb based web applications (see web_app package)

  16   *

  17   *  lmbFilterChain can be an intercepting filter in its turn as well.

  18   *

  19   *  The best way to think about filters is as of a "russian nested doll", e.g:

  20   *  <code>

  21   *  // +-Filter A

  22   *  // | +-Filter B

  23   *  // | | +-Filter C

  24   *  // | | |_

  25   *  // | |_

  26   *  // |_

  27   *  </code>

  28   *  To achieve this sample structure you should write the following code:

  29   *  <code>

  30   *  $chain = new lmbFilterChain();

  31   *  $chain->registerFilter(new A());

  32   *  $chain->registerFilter(new B());

  33   *  $chain->registerFilter(new C());

  34   *  </code>

  35   *

  36   *  Remember, it's the filter that decides whether to pass control to the

  37   *  underlying filter, this is done by calling filter chain instance next()

  38   *  method.

  39   *

  40   *  Usage example:

  41   *  <code>

  42   *  lmb_require('limb/filter_chain/src/lmbFilterChain.class.php');

  43   *  //create new chain

  44   *  $chain = new lmbFilterChain();

  45   *  //register filter object in the chain

  46   *  $chain->registerFilter(new MyFilter());

  47   *  //register a handle for a filter in the chain

  48   *  //in this case we can avoid PHP code parsing if

  49   *  //this filter won't be processed

  50   *  $chain->registerFilter(new lmbHandle('/path/to/MyFilter'));

  51   *  //executes the chain

  52   *  $chain->process();

  53   *  </code>

  54   *

  55   * @version $Id: lmbFilterChain.class.php 5945 2007-06-06 08:31:43Z pachanga $

  56   * @package filter_chain
  57   */
  58  class lmbFilterChain implements lmbInterceptingFilter
  59  {
  60    /**

  61     * @var array registered filters (or filter handles (see {@link lmbHandle}))

  62     */
  63    protected $filters = array();
  64    /**

  65     * @var integer Index of the current active filter while running the chain

  66     */
  67    protected $counter = -1;
  68  
  69    function __construct(){}
  70  
  71    /**

  72     * Registers filter (or handle on a filter) in the chain.

  73     *

  74     * @return void

  75     */
  76    function registerFilter($filter)
  77    {
  78      $this->filters[] = $filter;
  79    }
  80    /**

  81     * Returns registered filters

  82     *

  83     * @return array

  84     */
  85    function getFilters()
  86    {
  87      return $this->filters;
  88    }
  89    /**

  90     * Runs next filter in the chain.

  91     *

  92     * @return void

  93     */
  94    function next()
  95    {
  96      $this->counter++;
  97  
  98      if(isset($this->filters[$this->counter]))
  99      {
 100        $this->filters[$this->counter]->run($this);
 101      }
 102    }
 103    /**

 104     * Executes the chain

 105     *

 106     * @return void

 107     */
 108    function process()
 109    {
 110      $this->counter = -1;
 111      $this->next();
 112    }
 113    /**

 114     * Implements lmbInterceptingFilter interface.

 115     * Filter chain can be an intercepting filter.

 116     *

 117     * @param object Filter chain instance

 118     * @return void

 119     */
 120    function run($filter_chain)
 121    {
 122      $this->process();
 123      $filter_chain->next();
 124    }
 125  }
 126  
 127  ?>


Generated: Thu Aug 28 04:51:15 2008 Cross-referenced by PHPXref 0.7