Source for file Filter.php

Documentation is available at Filter.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.  * 各フィルタはprefilter()とpostfilter()を実装する必要があります。
  21.  *
  22.  * @package     teeple
  23.  */
  24. abstract class Teeple_Filter
  25. {
  26.     /**
  27.      * 必要に応じて属性を持つことができる
  28.      * @var array 
  29.      */
  30.     protected $_attributes = array();
  31.  
  32.     /**
  33.      * @var Logger 
  34.      */
  35.     protected $log;
  36.     
  37.     /**
  38.      * @var Teeple_FilterChain 
  39.      */
  40.     protected $filterChain;
  41.     public function setComponent_Teeple_FilterChain($c{
  42.         $this->filterChain = $c;
  43.     }
  44.     
  45.     /**
  46.      * @var Teeple_ActionChain 
  47.      */
  48.     protected $actionChain;
  49.     public function setComponent_Teeple_ActionChain($c{
  50.         $this->actionChain = $c;
  51.     }
  52.     
  53.     /**
  54.      * @var Teeple_Request 
  55.      */
  56.     protected $request;
  57.     public function setComponent_Teeple_Request($c{
  58.         $this->request = $c;
  59.     }
  60.     
  61.     /**
  62.      * @var Teeple_Session 
  63.      */
  64.     protected $session;
  65.     public function setComponent_Teeple_Session($c{
  66.         $this->session = $c;
  67.     }
  68.     
  69.     /**
  70.      * コンストラクタ
  71.      *
  72.      */
  73.     public function __construct({
  74.         $this->log = LoggerManager::getLogger(get_class($this));
  75.     }
  76.  
  77.     /**
  78.      * Filterの処理を実行します。
  79.      *
  80.      */
  81.     public function execute({
  82.  
  83.         $clsname get_class($this);
  84.         
  85.         // 現在のアクション名を取得
  86.         $actionName $this->actionChain->getCurActionName();
  87.         
  88.         // 実行対象かどうかのチェック
  89.         $isTarget $this->isTarget($actionName);
  90.         if ($isTarget{
  91.             $this->log->info("{$clsname} は実行対象外のためスキップします。");
  92.         }
  93.         
  94.         // prefilterを実行
  95.         if ($isTarget{
  96.             $this->log->debug("{$clsname}のprefilterを実行します。");
  97.             $this->prefilter();
  98.             $this->log->debug("{$clsname}のprefilterを実行しました。");
  99.         }
  100.         
  101.         // filterChain
  102.         $this->filterChain->execute();
  103.         
  104.         // postfilterを実行
  105.         if ($isTarget{
  106.             $this->log->debug("{$clsname}のpostfilterを実行します。");
  107.             $this->postfilter();
  108.             $this->log->debug("{$clsname}のpostfilterを実行しました。");
  109.         }
  110.         
  111.         return;
  112.     }
  113.     
  114.     /**
  115.      * アクションの実行前に実行される処理を記述します。
  116.      *
  117.      */
  118.     abstract public function prefilter();
  119.     
  120.     /**
  121.      * アクションの実行後に実行される処理を記述します。
  122.      *
  123.      */
  124.     abstract public function postfilter();
  125.     
  126.     /**
  127.      * アクションの実行完了をセットします。
  128.      *
  129.      */
  130.     protected function completeAction({
  131.         $this->filterChain->completeAction();
  132.     }
  133.     
  134.     /**
  135.      * レスポンスの返却完了をセットします。
  136.      *
  137.      */
  138.     protected function completeResponse({
  139.         $this->filterChain->completeResponse();
  140.     }
  141.     
  142.     protected function isCompleteAction({
  143.         return $this->filterChain->isCompleteAction();
  144.     }
  145.     
  146.     protected function isCompleteResponse({
  147.         return $this->filterChain->isCompleteResponse();
  148.     }
  149.  
  150.     /**
  151.      * 属性の数を返却
  152.      *
  153.      * @return  integer 属性の数
  154.      */
  155.     public function getSize({
  156.         return count($this->_attributes);
  157.     }
  158.  
  159.     /**
  160.      * 指定された属性を返却
  161.      *
  162.      * @param   string  $key    属性名
  163.      * @param   mixed  $default 指定された属性がない場合のデフォルト値
  164.      * @return  string  属性の値
  165.      */
  166.     public function getAttribute($key$default null{
  167.         
  168.         return isset($this->_attributes[$key]?
  169.             $this->_attributes[$key$default;
  170.     }
  171.  
  172.     /**
  173.      * 指定された属性に値をセット
  174.      *
  175.      * @param   string  $key    属性名
  176.      * @param   string  $value  属性の値
  177.      */
  178.     public function setAttribute($key$value{
  179.         $this->_attributes[$key$value;
  180.     }
  181.  
  182.     /**
  183.      * 属性を配列で返却
  184.      *
  185.      * @return  array   属性の値(配列)
  186.      */
  187.     public function getAttributes({
  188.         return $this->_attributes;
  189.     }
  190.  
  191.     /**
  192.      * 指定された属性に値をセット(配列でまとめてセット)
  193.      *
  194.      * @param   array   $attributes 属性の値(配列)
  195.      */
  196.     public function setAttributes($attributes{
  197.  
  198.         if (is_array($attributes)) {
  199.             foreach ($attributes as $key => $value{
  200.                 $this->setAttribute($key$value);
  201.             }
  202.         }
  203.         return;
  204.     }
  205.     
  206.     /**
  207.      * Filter実行対象のアクションかどうかをチェックします。
  208.      *
  209.      * @param string $actionName 
  210.      * @return boolean 
  211.      */
  212.     private function isTarget($actionName{
  213.         
  214.         // excludesに含まれている場合は無条件に対象外
  215.         if (@is_array($this->_attributes['excludes'])) {
  216.             foreach($this->_attributes['excludes'as $pattern{
  217.                 if (preg_match("/^{$pattern}$/"$actionName)) {
  218.                     return FALSE;
  219.                 }
  220.             }
  221.         }
  222.         
  223.         // includeが設定されている場合は、includeに含まれていないとだめ。
  224.         if (@is_array($this->_attributes['includes'])) {
  225.             return TRUE;
  226.         else {
  227.             foreach($this->_attributes['includes'as $pattern{
  228.                 if (preg_match("/^{$pattern}$/"$actionName)) {
  229.                     return TRUE;
  230.                 }
  231.             }
  232.         }
  233.         return FALSE;
  234.     }
  235.     
  236. }
  237. ?>

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