Source for file FilterChain.php

Documentation is available at FilterChain.php

  1. <?php
  2. /**
  3.  * Teeple2 - PHP5 Web Application Framework inspired by Seasar2
  4.  *
  5.  * PHP versions 5
  6.  *
  7.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  8.  * that is available through the world-wide-web at the following URI:
  9.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  10.  * the PHP License and are unable to obtain it through the web, please
  11.  * send a note to license@php.net so we can mail you a copy immediately.
  12.  *
  13.  * @package     teeple
  14.  * @author      Mitsutaka Sato <miztaka@gmail.com>
  15.  * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
  16.  */
  17.  
  18. /**
  19.  * Filterを保持するクラス
  20.  *
  21.  * @package     teeple
  22.  */
  23. {
  24.     /**
  25.      * Filterを保持する
  26.      * @var array 
  27.      */
  28.     protected $_list = array();
  29.  
  30.     /**
  31.      * Filterの位置を保持する
  32.      * @var array 
  33.      */
  34.     protected $_position = array();
  35.  
  36.     /**
  37.      * 現在実行されているFilterの位置を保持する
  38.      * @var int 
  39.      */
  40.     protected $_index = -1;
  41.  
  42.     /**
  43.      * @var Logger 
  44.      */
  45.     protected $log;
  46.     
  47.     
  48.     /**
  49.      * @var Teeple_Container 
  50.      */
  51.     private $container;
  52.     public function setComponent_container($c{
  53.         $this->container $c;
  54.     }
  55.     
  56.     /**
  57.      * @var Teeple_Request 
  58.      */
  59.     private $request;
  60.     public function setComponent_Teeple_Request($c{
  61.         $this->request $c;
  62.     }
  63.         
  64.     /**
  65.      * コンストラクタ
  66.      *
  67.      */
  68.     public function __construct({
  69.         $this->log = LoggerManager::getLogger(get_class($this));
  70.     }
  71.  
  72.     /**
  73.      * FilterChainの最後にFilterを追加
  74.      *
  75.      * @param   string  $name   Filterのクラス名
  76.      * @param   string  $alias  Filterのエイリアス名
  77.      * @param   array   $attributes Filterの属性値
  78.      */
  79.     public function add($name$alias ''$attributes NULL{
  80.  
  81.         // エイリアス名が指定されていない場合はクラス名をセット
  82.         if (empty($alias)) {
  83.             $alias $name;
  84.         }
  85.  
  86.         // Filterの実行が既に始まっていたらエラー(実行後の追加はエラー)
  87.         if ($this->_index > -1{
  88.             throw new Teeple_Exception("既にフィルターが実行されています。");
  89.         }
  90.  
  91.         // Filterのクラス名が不正だったらエラー
  92.         if (!preg_match("/^[0-9a-zA-Z_]+$/"$name)) {
  93.             throw new Teeple_Exception("フィルターのクラス名が不正です。({$name})");
  94.         }
  95.  
  96.         // 既に同名のFilterが追加されていたら何もしない
  97.         if (isset($this->_list[$alias]&& is_object($this->_list[$alias])) {
  98.             $this->log->info("このFilterは既に登録されています(${name}[alias:${alias}])");
  99.             return;
  100.         }
  101.  
  102.         // オブジェクトの生成に失敗していたらエラー
  103.         $className "Teeple_Filter_" ucfirst($name);
  104.         $filter $this->container->getComponent($className);
  105.         if (!is_object($filter)) {
  106.             throw new Teeple_Exception("Filterオブジェクトの生成に失敗しました。({$name})");
  107.         }
  108.         if (is_array($attributes)) {
  109.             $filter->setAttributes($attributes);
  110.         }        
  111.  
  112.         $this->_list[$alias$filter;
  113.         $this->_position[$alias;
  114.  
  115.         return;
  116.     }
  117.  
  118.     /**
  119.      * FilterChainをクリア
  120.      *
  121.      */
  122.     public function clear({
  123.  
  124.         $this->_list     = array();
  125.         $this->_position = array();
  126.         $this->_index    = -1;
  127.     }
  128.  
  129.     /**
  130.      * FilterChainの長さを返却
  131.      *
  132.      * @return  integer FilterChainの長さ
  133.      */
  134.     public function getSize({
  135.         return count($this->_list);
  136.     }
  137.  
  138.     /**
  139.      * FilterChainを組み立てる
  140.      *
  141.      * @param string $configfile 
  142.      */
  143.     public function build($configfile TEEPLE_FILTER_CONFIG{
  144.         
  145.         $this->log->debug("FilterChainをセットアップします。");
  146.         
  147.         $config Teeple_Util::readIniFile($configfile);
  148.         //$this->log->debug(var_export($config, true));
  149.         if (is_array($config)) {
  150.             foreach ($config as $section => $value_ar{
  151.                 // フィルタ名とエイリアス名を取得
  152.                 $sections explode(':'$section);
  153.                 $filterName $sections[0]// フィルタ名
  154.                 $alias $filterName;
  155.                 if (isset($sections[1]&& $sections[1]{
  156.                     $alias $sections[1]// エイリアス名
  157.                 }
  158.                 // FilterChainに追加
  159.                 $this->add($filterName$alias$value_ar);
  160.             }
  161.         }
  162.         
  163.         // 最後にActionとViewを追加する。
  164.         $this->add('Action');
  165.         $this->add('View');
  166.         
  167.         return;
  168.     }
  169.  
  170.     /**
  171.      * FilterChainの中の次のFilterを実行
  172.      *
  173.      * このメソッドはクラスメソッド
  174.      *
  175.      * @access  public
  176.      * @since   3.0.0
  177.      */
  178.     public function execute({
  179.  
  180.         if ($this->getSize(1{
  181.             throw new Teeple_Exception("実行するフィルタがありません。");
  182.         }
  183.  
  184.         if ($this->_index < ($this->getSize(1)) {
  185.             $this->_index++;
  186.             $name $this->_position[$this->_index];
  187.             $filter $this->_list[$name];
  188.             if (!is_object($filter)) {
  189.                 throw new Teeple_Exception("Filterオブジェクトの取得に失敗しました。({$name})");
  190.             }
  191.             $filter->execute();
  192.         }
  193.         
  194.         return;
  195.     }
  196.     
  197.     /**
  198.      * Actionの完了をセットします。
  199.      *
  200.      */
  201.     public function completeAction({
  202.         $this->request->completeAction();
  203.     }
  204.     
  205.     /**
  206.      * Responseの完了をセットします。
  207.      *
  208.      */
  209.     public function completeResponse({
  210.         $this->request->completeResponse();
  211.     }
  212.     
  213.     /**
  214.      * Actionが完了したかどうか?
  215.      * @return boolean 
  216.      */
  217.     public function isCompleteAction({
  218.         return $this->request->isCompleteAction();
  219.     }
  220.     
  221.     /**
  222.      * Responseが完了したかどうか?
  223.      * @return boolean 
  224.      */
  225.     public function isCompleteResponse({
  226.         return $this->request->isCompleteResponse();
  227.     }
  228.     
  229. }
  230. ?>

Documentation generated on Mon, 26 Apr 2010 08:59:49 +0900 by phpDocumentor 1.4.3