CrossPHP
  • Namespace
  • Class
  • Download

Namespaces

  • Cross
    • Auth
    • Cache
      • Driver
      • Request
    • Core
    • DB
      • Connecter
      • Drivers
      • SQLAssembler
    • Exception
    • Http
    • I
    • Lib
      • Document
    • Module
    • MVC
    • Runtime
  • None

Classes

  • Cross\Auth\CookieAuth
  • Cross\Auth\SessionAuth
  • Cross\Cache\Driver\FileCacheDriver
  • Cross\Cache\Driver\MemcacheDriver
  • Cross\Cache\Driver\RedisDriver
  • Cross\Cache\Request\FileCache
  • Cross\Cache\Request\Memcache
  • Cross\Cache\Request\RedisCache
  • Cross\Cache\RequestCache
  • Cross\Core\Annotate
  • Cross\Core\Application
  • Cross\Core\ArrayMap
  • Cross\Core\Config
  • Cross\Core\CrossArray
  • Cross\Core\Delegate
  • Cross\Core\FrameBase
  • Cross\Core\Helper
  • Cross\Core\HttpAuth
  • Cross\Core\Loader
  • Cross\Core\Rest
  • Cross\Core\Router
  • Cross\DB\Connecter\BaseConnecter
  • Cross\DB\Connecter\MySQLConnecter
  • Cross\DB\Connecter\PgSQLConnecter
  • Cross\DB\Connecter\SQLiteConnecter
  • Cross\DB\DBFactory
  • Cross\DB\Drivers\CouchDriver
  • Cross\DB\Drivers\MongoDriver
  • Cross\DB\Drivers\PDOSqlDriver
  • Cross\DB\SQLAssembler\MySQLAssembler
  • Cross\DB\SQLAssembler\PgSQLAssembler
  • Cross\DB\SQLAssembler\SQLAssembler
  • Cross\DB\SQLAssembler\SQLiteAssembler
  • Cross\Http\Request
  • Cross\Http\Response
  • Cross\Lib\Array2XML
  • Cross\Lib\Document\CallTree
  • Cross\Lib\Document\CallTreeToHTML
  • Cross\Lib\Document\HTML
  • Cross\Lib\StringToPHPStream
  • Cross\Module\SQLModule
  • Cross\MVC\Controller
  • Cross\MVC\Module
  • Cross\MVC\View
  • Cross\Runtime\ClosureContainer

Interfaces

  • Cross\I\CacheInterface
  • Cross\I\HttpAuthInterface
  • Cross\I\PDOConnecter
  • Cross\I\RequestCacheInterface
  • Cross\I\RouterInterface
  • Cross\I\SqlInterface

Exceptions

  • Cross\Exception\CacheException
  • Cross\Exception\CoreException
  • Cross\Exception\CrossException
  • Cross\Exception\FrontException

Functions

  • ascLogo
  • line
  • tBody
  • th
  • tHead
  1 <?php
  2 /**
  3  * Cross - a micro PHP 5 framework
  4  *
  5  * @link        http://www.crossphp.com
  6  * @license     MIT License
  7  */
  8 
  9 namespace Cross\Core;
 10 
 11 use Cross\Exception\CoreException;
 12 
 13 /**
 14  * @author wonli <wonli@live.com>
 15  * Class Config
 16  * @package Cross\Core
 17  */
 18 class Config
 19 {
 20     /**
 21      * @var string
 22      */
 23     private $res_file;
 24 
 25     /**
 26      * @var array
 27      */
 28     private $config_data;
 29 
 30     /**
 31      * @var self
 32      */
 33     private static $instance;
 34 
 35     /**
 36      * @var CrossArray
 37      */
 38     private $ca;
 39 
 40     /**
 41      * 查询缓存
 42      *
 43      * @var array
 44      */
 45     private static $cache;
 46 
 47     /**
 48      * 读取配置
 49      *
 50      * @param string $res_file 配置文件绝对路径
 51      * @throws CoreException
 52      */
 53     private function __construct($res_file)
 54     {
 55         $this->res_file = $res_file;
 56         $this->config_data = Loader::read($res_file);
 57 
 58         $this->ca = CrossArray::init($this->config_data, $this->res_file);
 59     }
 60 
 61     /**
 62      * 实例化配置类
 63      *
 64      * @param string $file
 65      * @return Config
 66      * @throws CoreException
 67      */
 68     static function load($file)
 69     {
 70         if (!isset(self::$instance[$file])) {
 71             self::$instance[$file] = new self($file);
 72         }
 73 
 74         return self::$instance[$file];
 75     }
 76 
 77     /**
 78      * 合并附加数组到源数组
 79      *
 80      * @param array $append_config
 81      * @return $this
 82      */
 83     function combine(array $append_config = array())
 84     {
 85         if (!empty($append_config)) {
 86             foreach ($append_config as $key => $value) {
 87                 if (isset($this->config_data[$key]) && is_array($value)) {
 88                     $this->config_data[$key] = array_merge($this->config_data[$key], $value);
 89                 } else {
 90                     $this->config_data[$key] = $value;
 91                 }
 92 
 93                 $this->clearIndexCache($key);
 94             }
 95         }
 96 
 97         return $this;
 98     }
 99 
100     /**
101      * @see CrossArray::get()
102      *
103      * @param string $index
104      * @param string|array $options
105      * @return string|array
106      */
107     function get($index, $options = '')
108     {
109         $key = $this->getIndexCacheKey($index);
110         if (is_array($options)) {
111             $opk = implode('.', $options);
112         } elseif ($options) {
113             $opk = $options;
114         } else {
115             $opk = '-###-';
116         }
117 
118         if (!isset(self::$cache[$key][$opk])) {
119             self::$cache[$key][$opk] = $this->ca->get($index, $options);
120         }
121 
122         return self::$cache[$key][$opk];
123     }
124 
125     /**
126      * @see CrossArray::get()
127      *
128      * @param string $index
129      * @param array|string $values
130      * @return bool
131      */
132     function set($index, $values = '')
133     {
134         $result = $this->ca->set($index, $values);
135         $this->clearIndexCache($index);
136 
137         return $result;
138     }
139 
140     /**
141      * @see CrossArray::getAll()
142      *
143      * @param bool $obj 是否返回对象
144      * @return array|object
145      */
146     function getAll($obj = false)
147     {
148         if ($obj) {
149             return CrossArray::arrayToObject($this->config_data);
150         }
151 
152         return $this->config_data;
153     }
154 
155     /**
156      * 获取数组索引缓存key
157      *
158      * @param string $index
159      * @return string
160      */
161     protected function getIndexCacheKey($index)
162     {
163         return $this->res_file . '.' . $index;
164     }
165 
166     /**
167      * 清除缓存
168      *
169      * @param string $index
170      */
171     protected function clearIndexCache($index)
172     {
173         $key = $this->getIndexCacheKey($index);
174         unset(self::$cache[$key]);
175     }
176 }
177 
CrossPHP API documentation generated by ApiGen