Source for file GlobalConfig.php

Documentation is available at GlobalConfig.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.  * GlobalConfig
  20.  * 
  21.  * プログラム全体の設定情報を扱うためのクラス
  22.  * PHPの定数を透過的に扱うことが出来る
  23.  * 
  24.  * preferConstantによってPHP定数の優先順位が変わる
  25.  * trueにすると
  26.  *   PHP定数 > setValueで設定された値 > importSectionsでまとめて読み込んだ値
  27.  * falseにすると
  28.  *   setValueで設定された値 > importSectionsでまとめて読み込んだ値 > PHP定数
  29.  * 
  30.  * @package     teeple
  31.  */
  32. {
  33.     /**
  34.      * @var  array  importSectionsで読み込まれた設定値が保存される
  35.      */
  36.     var $_values = array();
  37.  
  38.     /**
  39.      * @var  String  setValueで設定された値が保存される
  40.      */
  41.     var $_overwriteValues = array();
  42.     
  43.     /**
  44.      * @var  String  importSectionsで読み込んだ値を、セクション単位でまとめるための配列
  45.      */
  46.     var $_sections = array();
  47.  
  48.     /**
  49.      * @var  String  定数としてエクスポート可能な名前を表す正規表現
  50.      */
  51.     var $_exportableConstPattern = '/^[A-Z_][A-Z0-9_]*$/';
  52.  
  53.     /**
  54.      * @var  String  定数を他の値に対して優先するか否か
  55.      */
  56.     var $_preferConstant = false;
  57.  
  58.     /**
  59.      * コンストラクタ
  60.      * 
  61.      * @access public
  62.      * @param bool $preferConstant  [optional] if true, constants have priority over
  63.      *                                          any other values.
  64.      */
  65.     function __construct($preferConstant=false)
  66.     {
  67.         $this->_preferConstant = $preferConstant;
  68.     }
  69.  
  70.     /**
  71.      * 定数を優先するか
  72.      * 
  73.      * @access public
  74.      * @param  boolean    $bool 
  75.      */
  76.     function setPreferConstant($bool)
  77.     {
  78.         $this->_preferConstant = $bool;
  79.     }
  80.  
  81.     /**
  82.      * 値がsetValueメソッドによって上書きされているか
  83.      * どうかを調べる
  84.      * 
  85.      * @access private
  86.      * @param  String    $key 
  87.      * @return boolean 
  88.      * @see    setValue()
  89.      */
  90.     function _isOverwritten($key)
  91.     {
  92.         return array_key_exists($key$this->_overwriteValues);
  93.     }
  94.  
  95.     /**
  96.      * 定数が利用可能かどうかを調べる
  97.      * 
  98.      * @access private
  99.      * @param  String    $key 
  100.      * @return boolean 
  101.      */
  102.     function _canUseConstant($key)
  103.     {
  104.         return defined($key);
  105.     }
  106.  
  107.     /**
  108.      * 配列を一括して取り込む
  109.      * 自動的にupdateValuesを呼び出し、保持されている値を更新する
  110.      * 
  111.      * @access public
  112.      * @param  array    $arr 
  113.      */
  114.     function importSections($arr)
  115.     {
  116.         $this->_sections = $arr $this->_sections;
  117.         $this->updateValues();
  118.     }
  119.  
  120.  
  121.     /**
  122.      * 保持されている値を更新する
  123.      * 
  124.      * @access public
  125.      */
  126.     function updateValues()
  127.     {
  128.         foreach($this->_sections as $sec => $arr{
  129.             $prefix $this->getValue($sec"");
  130.             
  131.             foreach($arr as $k => $v{
  132.                 $this->_values[$k$prefix $v;
  133.             }
  134.         }
  135.     }
  136.  
  137.     /**
  138.      * 設定値を得る
  139.      * 定数として宣言されているか、
  140.      * 定数を優先するか、
  141.      * 上書きされているか、
  142.      * 等の要素を考慮して設定値は決まる
  143.      * 
  144.      * @access public
  145.      * @param  String    $key 
  146.      * @param  mixed    $default 
  147.      * @return mixed 
  148.      */
  149.     function getValue($key$default=null)
  150.     {
  151.         if($this->_preferConstant && $this->_canUseConstant($key)) {
  152.             return constant($key);
  153.             
  154.         elseif($this->_isOverwritten($key)) {
  155.             return $this->_overwriteValues[$key];
  156.             
  157.         elseif(isset($this->_values[$key])) {
  158.             return $this->_values[$key];
  159.             
  160.         elseif(!$this->_preferConstant && $this->_canUseConstant($key)) {
  161.             return constant($key);
  162.             
  163.         }
  164.         return $default;
  165.     }
  166.  
  167.     /**
  168.      * 値が設定されているかどうかを調べる
  169.      * 
  170.      * @access public
  171.      * @param  String    $key 
  172.      * @param  boolean    $checkConst [optional] 定数をチェックするか
  173.      * @return boolean 
  174.      */
  175.     function hasValue($key$checkConst=false)
  176.     {
  177.         return ($this->_isOverwritten($key||
  178.            isset($this->_values[$key]||
  179.            ($checkConst && $this->_canUseConstant($key)));
  180.     }
  181.  
  182.     /**
  183.      * 値を設定する
  184.      * このメソッドで設定した値は
  185.      * $_overwriteValuesとして独自に管理される
  186.      * 
  187.      * @access public
  188.      * @param  mixed    $key 
  189.      * @param  mixed    $value 
  190.      * @param  boolean  $autoUpdate [optional]
  191.      *                   if ture, call updateValues() after overwriting
  192.      */
  193.     function setValue($key$value$autoUpdate=false)
  194.     {
  195.         $this->_overwriteValues[$key$value;
  196.         if($autoUpdate{
  197.             $this->updateValues();
  198.         }
  199.     }
  200.  
  201.     /**
  202.      * セクション単位で設定値を配列の形で得る
  203.      * 
  204.      * @access public
  205.      * @param  String    $sec 
  206.      * @return array 
  207.      */
  208.     function getSection($sec)
  209.     {
  210.         if(!isset($this->_sections[$sec])) {
  211.             return null;
  212.         }
  213.         
  214.         $result array();
  215.         foreach($this->_sections[$secas $k => $v{
  216.             if($this->_isOverwritten($k||
  217.                ($this->_preferConstant && $this->_canUseConstant($k))) {
  218.                 //上書きされているか、
  219.                 //定数として独自に定義されている場合は、
  220.                 //セクション内に含めない
  221.                 continue;
  222.             else {
  223.                 $result[$k$this->getValue($k);
  224.             }
  225.         }
  226.         return $result;
  227.     }
  228.  
  229.     /**
  230.      * INIファイルから設定を読み込む
  231.      * 
  232.      * @access public
  233.      * @param  String    $filename 
  234.      * @return boolean 
  235.      */
  236.     function loadFromFile($filename)
  237.     {
  238.         if (defined('INI_SCANNER_RAW')) {
  239.             $arr @parse_ini_file($filenametrueINI_SCANNER_RAW);
  240.         else {
  241.             $arr @parse_ini_file($filenametrue);
  242.         }
  243.         if($arr{
  244.             throw new Teeple_Exception("iniファイルの読み込みに失敗しました。");
  245.         }
  246.         $this->importSections($arr);
  247.         return true;
  248.     }
  249.  
  250.     /**
  251.      * 設定値を定数としてエクスポートする
  252.      * 
  253.      * @access public
  254.      * @return String 
  255.      */
  256.     function exportConstants()
  257.     {
  258.         $allValues $this->_overwriteValues + $this->_values;
  259.         foreach($allValues as $k => $v{
  260.             if(!defined($k&& $this->isConstName($k)) {
  261.                 define($k$v);
  262.             }
  263.         }
  264.     }
  265.  
  266.     /**
  267.      * 定数としてエクスポート可能な名称か調べる
  268.      * 
  269.      * @access public
  270.      * @param  String    $key 
  271.      * @return String 
  272.      */
  273.     function isConstName($key)
  274.     {
  275.         return preg_match($this->_exportableConstPattern$key);
  276.     }
  277.  
  278.     /**
  279.      * ファイルから設定を読み込み、
  280.      * 定数としてのエクスポートまでを行うstaticメソッド
  281.      * 
  282.      * @static
  283.      * @access public
  284.      * @return boolean 
  285.      */
  286.     function loadConstantsFromFile($filename)
  287.     {
  288.         $config new Teeple_GlobalConfig(true);
  289.         if(!$config->loadFromFile($filename)) {
  290.             return false;
  291.         }
  292.         $config->exportConstants();
  293.     }
  294.  
  295. }
  296.  
  297. ?>

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