Source for file Config.php

Documentation is available at Config.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  Config.php
  5.  *
  6.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  7.  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
  8.  *  @package    Ethna
  9.  *  @version    $Id: f48b583e876f0809c6ff9980349bc3924464a40c $
  10.  */
  11.  
  12. // {{{ Ethna_Config
  13. /**
  14.  *  設定クラス
  15.  *
  16.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  17.  *  @access     public
  18.  *  @package    Ethna
  19.  */
  20. {
  21.     /**#@+
  22.      *  @access private
  23.      */
  24.  
  25.     /** @protected    object  Ethna_Controller    controllerオブジェクト */
  26.     protected $controller;
  27.  
  28.     /** @FIXME @protected    array   設定内容 */
  29.     public $config = null;
  30.  
  31.     /**#@-*/
  32.  
  33.  
  34.     /**
  35.      *  Ethna_Configクラスのコンストラクタ
  36.      *
  37.      *  @access public
  38.      *  @param  object  Ethna_Controller    $controller    controllerオブジェクト
  39.      */
  40.     public function __construct($controller)
  41.     {
  42.         $this->controller = $controller;
  43.  
  44.         // 設定ファイルの読み込み
  45.         $r $this->_getConfig();
  46.         if (Ethna::isError($r)) {
  47.             // この時点ではlogging等は出来ない(Loggerオブジェクトが生成されていない)
  48.             $fp fopen("php://stderr""r");
  49.             fputs($fpsprintf("error occured while reading config file(s) [%s]\n")$r->getInfo(0));
  50.             fclose($fp);
  51.             $this->controller->fatal();
  52.         }
  53.     }
  54.  
  55.     /**
  56.      *  設定値へのアクセサ(R)
  57.      *
  58.      *  @access public
  59.      *  @param  string  $key    設定項目名
  60.      *  @return string  設定値
  61.      */
  62.     function get($key null)
  63.     {
  64.         if (is_null($key)) {
  65.             return $this->config;
  66.         }
  67.         if (isset($this->config[$key]== false{
  68.             return null;
  69.         }
  70.         return $this->config[$key];
  71.     }
  72.  
  73.     /**
  74.      *  設定値へのアクセサ(W)
  75.      *
  76.      *  @access public
  77.      *  @param  string  $key    設定項目名
  78.      *  @param  string  $value  設定値
  79.      */
  80.     function set($key$value)
  81.     {
  82.         $this->config[$key$value;
  83.     }
  84.  
  85.     /**
  86.      *  設定ファイルを更新する
  87.      *
  88.      *  @access public
  89.      *  @return mixed   0:正常終了 Ethna_Error:エラー
  90.      */
  91.     function update()
  92.     {
  93.         return $this->_setConfig();
  94.     }
  95.  
  96.     /**
  97.      *  設定ファイルを読み込む
  98.      *
  99.      *  @access private
  100.      *  @return mixed   0:正常終了 Ethna_Error:エラー
  101.      */
  102.     function _getConfig()
  103.     {
  104.         $config array();
  105.         $file $this->_getConfigFile();
  106.         if (file_exists($file)) {
  107.             $lh Ethna_Util::lockFile($file'r');
  108.             if (Ethna::isError($lh)) {
  109.                 return $lh;
  110.             }
  111.  
  112.             include($file);
  113.  
  114.             Ethna_Util::unlockFile($lh);
  115.         }
  116.  
  117.         // デフォルト値設定
  118.         if (isset($_SERVER['HTTP_HOST']&& isset($config['url']== false{
  119.             $config['url'sprintf("http://%s/"$_SERVER['HTTP_HOST']);
  120.         }
  121.         if (isset($config['dsn']== false{
  122.             $config['dsn'"";
  123.         }
  124.         if (isset($config['log_facility']== false{
  125.             $config['log_facility'"";
  126.         }
  127.         if (isset($config['log_level']== false{
  128.             $config['log_level'"";
  129.         }
  130.         if (isset($config['log_option']== false{
  131.             $config['log_option'"";
  132.         }
  133.  
  134.         $this->config = $config;
  135.  
  136.         return 0;
  137.     }
  138.  
  139.     /**
  140.      *  設定ファイルに書き込む
  141.      *
  142.      *  @access private
  143.      *  @return mixed   0:正常終了 Ethna_Error:エラー
  144.      */
  145.     function _setConfig()
  146.     {
  147.         $file $this->_getConfigFile();
  148.  
  149.         $lh Ethna_Util::lockFile($file'w');
  150.         if (Ethna::isError($lh)) {
  151.             return $lh;
  152.         }
  153.  
  154.         fwrite($lh"<?php\n");
  155.         fwrite($lhsprintf("/*\n * %s\n *\n * update: %s\n */\n"basename($file)strftime('%Y/%m/%d %H:%M:%S')));
  156.         fwrite($lh"\$config = array(\n");
  157.         foreach ($this->config as $key => $value{
  158.             $this->_setConfigValue($lh$key$value0);
  159.         }
  160.         fwrite($lh");\n");
  161.  
  162.         Ethna_Util::unlockFile($lh);
  163.  
  164.         return 0;
  165.     }
  166.  
  167.     /**
  168.      *  設定ファイルに設定値を書き込む
  169.      *
  170.      *  @access private
  171.      */
  172.     function _setConfigValue($fp$key$value$level)
  173.     {
  174.         fputs($fpsprintf("%s'%s' => "str_repeat("    "$level+1)$key));
  175.         if (is_array($value)) {
  176.             fputs($fpsprintf("array(\n"));
  177.             foreach ($value as $k => $v{
  178.                 $this->_setConfigValue($fp$k$v$level+1);
  179.             }
  180.             fputs($fpsprintf("%s),\n"str_repeat("    "$level+1)));
  181.         else {
  182.             fputs($fpsprintf("'%s',\n"$value));
  183.         }
  184.     }
  185.  
  186.     /**
  187.      *  設定ファイル名を取得する
  188.      *
  189.      *  @access private
  190.      *  @return string  設定ファイルへのフルパス名
  191.      */
  192.     function _getConfigFile()
  193.     {
  194.         return $this->controller->getDirectory('etc''/' strtolower($this->controller->getAppId()) '-ini.php';
  195.     }
  196. }
  197. // }}}

Documentation generated on Fri, 11 Nov 2011 03:57:46 +0900 by phpDocumentor 1.4.3