Source for file ConfigUtils.php

Documentation is available at ConfigUtils.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.  * 設定ファイルの内容を保持する
  20.  *
  21.  * @package     teeple
  22.  */
  23. {
  24.     /**
  25.      * @var 各セクションの値を保持する 
  26.      *
  27.      * @access  private
  28.      * @since   3.0.0
  29.      */
  30.     var $_config;
  31.  
  32.     /**
  33.      * 設定情報を一時的に保存する
  34.      * 
  35.      * @var  String  $_configPool 
  36.      * @since 3.2.0
  37.      */
  38.     var $_configPool;
  39.  
  40.     /**
  41.      * Actionフィルタの名称
  42.      * 
  43.      * @var  String  $_actionKey 
  44.      * @since 3.2.0
  45.      */
  46.     var $_actionKey;
  47.  
  48.     /**
  49.      * Loggerを格納する
  50.      * 
  51.      * @var Logger 
  52.      */
  53.     var $log;
  54.     
  55.     /**
  56.      * @var Teeple_ActionChain 
  57.      */
  58.     private $actionChain;
  59.     public function setComponent_Teeple_ActionChain($c{
  60.         $this->actionChain $c;
  61.     }
  62.     
  63.     /**
  64.      * コンストラクタ
  65.      *
  66.      */
  67.     function __construct()
  68.     {
  69.         $this->clear();
  70.         $this->log = LoggerManager::getLogger(get_class($this));
  71.     }
  72.  
  73.     /**
  74.      * 設定情報をクリア
  75.      *
  76.      * @access  public
  77.      * @since   3.0.0
  78.      */
  79.     function clear()
  80.     {
  81.         $this->_config     array();
  82.         $this->_configPool = array();
  83.         $this->_actionKey  = "Action";
  84.     }
  85.  
  86.     /**
  87.      * 一時保存された情報を取得する
  88.      * 存在しない場合は空の配列を返す
  89.      * 
  90.      * @since  3.2.0
  91.      * @param  String    $key 
  92.      * @return array 
  93.      */
  94.     function _getPreserved($key)
  95.     {
  96.         return isset($this->_configPool[$key]$this->_configPool[$keyarray();
  97.     }
  98.  
  99.     /**
  100.      * 設定情報を一時的に保存する
  101.      * 
  102.      * @since  3.2.0
  103.      * @param  String    $key 
  104.      * @param  array     $values 
  105.      */
  106.     function _preserve($key$values)
  107.     {
  108.         //To keep the order of keys, can't use array operator '+'.
  109.         foreach($values as $k => $v{
  110.             $this->_configPool[$key][$k$v;
  111.         }
  112.     }
  113.  
  114.     /**
  115.      * 既に追加されている場合はマージ、
  116.      * そうでなければ一時保存
  117.      * 
  118.      * @since  3.2.0
  119.      * @param  String    $key 
  120.      * @param  array     $values 
  121.      */
  122.     function _mergeOrPreserve($key$values)
  123.     {
  124.         if(isset($this->_config[$key])) {
  125.             $this->_mergeOrAdd($key$values);
  126.         else {
  127.             $this->_preserve($key$values);
  128.         }
  129.     }
  130.  
  131.     /**
  132.      * 既に追加されていたらマージ、
  133.      * そうでなければ新規追加
  134.      * 
  135.      * @since  3.2.0
  136.      * @param  String    $key 
  137.      * @param  array     $values 
  138.      */
  139.     function _mergeOrAdd($key$values)
  140.     {
  141.         if(!isset($this->_config[$key])) {
  142.             $this->_config[$key$this->_getPreserved($key);
  143.         }
  144.         //To keep the order of keys, can't use array operator '+'.
  145.         foreach($values as $k => $v{
  146.             $this->_config[$key][$k$v;
  147.         }
  148.     }
  149.  
  150.     /**
  151.      * シンプルに設定を読み込む
  152.      * オプションで読み込むキーを指定することができる
  153.      * 
  154.      * このメソッドではActionフィルタは一時保存されるだけで
  155.      * 登録されない
  156.      * 
  157.      * @since  3.2.0
  158.      * @param  array   $config 
  159.      * @param  array   $keys [optional]  keys to be read
  160.      */
  161.     function readSimpleConfig($config$keys=null)
  162.     {
  163.         if(!is_array($keys)) {
  164.             $keys array_keys($config);
  165.         }
  166.  
  167.         foreach($keys as $key{
  168.             if(!isset($config[$key])) {
  169.                 throw new Teeple_Exception("keyの値が不正です。($key)");
  170.             }
  171.             
  172.             if($this->_isActionFilter($key)) {
  173.                 $this->_preserve($key$config[$key]);
  174.                 $this->_actionKey = $key;
  175.             else {
  176.                 $this->_mergeOrAdd($key$config[$key]);
  177.             }
  178.         }
  179.     }
  180.  
  181.     /**
  182.      * GlobalFilterの処理も含め、
  183.      * 最下層かどうかも加味して、設定を読み込む
  184.      * 
  185.      * このメソッドではActionフィルタは一時保存されるだけで
  186.      * 登録されない
  187.      * 
  188.      * @since  3.2.0
  189.      * @param  array      $config 
  190.      * @param  boolean    $isDeepest 
  191.      */
  192.     function readConfig($config$isDeepest)
  193.     {
  194.         $globalFilter null;
  195.         if(isset($config['GlobalFilter'])) {
  196.             $globalFilter $config['GlobalFilter'];
  197.             unset($config['GlobalFilter']);
  198.         }
  199.  
  200.         if($globalFilter === null || $isDeepest{
  201.             //globalfilterが無い、もしくは最下層
  202.             $this->readSimpleConfig($config);
  203.             return;
  204.         }
  205.  
  206.         //globalFilter処理
  207.         foreach($config as $key => $values{
  208.             //ここではActionフィルタかどうかは調べなくて良い
  209.             if(!isset($globalFilter[$key])) {
  210.                 $this->_mergeOrPreserve($key$values);
  211.             }
  212.         }
  213.         $this->readSimpleConfig($configarray_keys($globalFilter));
  214.     }
  215.  
  216.     /**
  217.      * 設定ファイルを読み込む
  218.      * 
  219.      * このメソッドではActionフィルタは一時保存されるだけで
  220.      * 登録されない
  221.      * 
  222.      * @since  3.2.0
  223.      * @param  String    $filename 
  224.      * @param  boolean    $isDeepest 
  225.      */
  226.     function readConfigFile($filename$isDeepest)
  227.     {
  228.         if(file_exists($filename&&
  229.            ($config parse_ini_file($filenametrue))) {
  230.             
  231.             if (CONFIG_CODE != INTERNAL_CODE{
  232.                 mb_convert_variables(INTERNAL_CODECONFIG_CODE$config);
  233.             }
  234.             $this->readConfig($config$isDeepest);
  235.         }
  236.     }
  237.  
  238.     /**
  239.      * アクションに対する全ての設定ファイルを読み込む
  240.      * Debugフィルタは最初に、
  241.      * Actionフィルタは最後に登録する
  242.      * 
  243.      * $readerFuncはテスタビリティのための存在
  244.      * 
  245.      * @since  3.2.0
  246.      * @param  String    $actionName 
  247.      * @param  array or string     $readerFunc
  248.      */
  249.     function readConfigFiles($actionName$readerFunc='readConfigFile')
  250.     {
  251.         $obj $this;
  252.         $method $readerFunc;
  253.         if(is_array($readerFunc&& is_callable($readerFunc)) {
  254.             $obj $readerFunc[0];
  255.             $method $readerFunc[1];
  256.         }
  257.  
  258.         $paths    array_merge(array("")explode('_'$actionName));
  259.         //$basename = array_pop($paths);
  260.         $crrPath  MODULE_DIR;
  261.         $depth    0;
  262.         $maxDepth count($paths1;
  263.  
  264.         $this->log->debug("アクション $actionName のconfigを読み込みます。");
  265.         foreach($paths as $p{
  266.             $crrPath .= "{$p}/";
  267.             $configPath $maxDepth == $depth ?
  268.                 substr($crrPath0-1).".".CONFIG_FILE "{$crrPath}"CONFIG_FILE;
  269.                 // "{$crrPath}{$basename}.maple.ini" : "{$crrPath}". CONFIG_FILE;
  270.             $this->log->debug("configPath: {$configPath}");
  271.             $obj->$method($configPath($maxDepth == $depth++));
  272.         }
  273.  
  274.         $this->_mergeOrAdd($this->_actionKeyarray());
  275.     }
  276.     
  277.     /**
  278.      * 設定ファイルを読み込む
  279.      *
  280.      * @access  public
  281.      * @since   3.0.0
  282.      */
  283.     function execute()
  284.     {
  285.         $this->readConfigFiles($this->actionChain->getCurActionName());
  286.     }
  287.  
  288.     /**
  289.      * Actionフィルタの一種か調べる
  290.      * 
  291.      * @since  3.2.0
  292.      * @param  String    $key 
  293.      * @return boolean 
  294.      */
  295.     function _isActionFilter($key)
  296.     {
  297.         return preg_match('/Action$/'$key);
  298.     }
  299.     
  300.     /**
  301.      * セクションの設定情報を返却
  302.      *
  303.      * @return  array   セクションの設定情報の配列
  304.      * @access  public
  305.      * @since   3.0.0
  306.      */
  307.     function getConfig()
  308.     {
  309.         return $this->_config;
  310.     }
  311.  
  312.     /**
  313.      * 指定されたセクションの設定情報を返却
  314.      *
  315.      * @param   string  $section    セクション名
  316.      * @return  array   設定情報の配列
  317.      * @access  public
  318.      * @since   3.0.0
  319.      */
  320.     function getSectionConfig($section)
  321.     {
  322.         if (isset($this->_config[$section])) {
  323.             return $this->_config[$section];
  324.         }
  325.     }
  326. }
  327. ?>

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