diff --git a/config/local.php.dist b/config/local.php.dist index 2195448..c4b7359 100644 --- a/config/local.php.dist +++ b/config/local.php.dist @@ -41,11 +41,11 @@ return array( // For now, XCP servers/pool masters must be defined in this file. 'xcp' => array( - 'pool 1' => array( - 'url' => 'https://xcp1.example.net', - 'username' => 'username', - 'password' => 'password' - ), + //'pool 1' => array( + // 'url' => 'https://xcp1.example.net', + // 'username' => 'username', + // 'password' => 'password' + //), ), ); diff --git a/lib/Application.php b/lib/Application.php index 11361d7..7979c13 100644 --- a/lib/Application.php +++ b/lib/Application.php @@ -556,7 +556,7 @@ final class Application extends Base //-------------------------------------- // Creates master sockets. - foreach ($config->get('listen') as $uri) + foreach ($config['listen'] as $uri) { $handle = self::_createServer($uri); $loop->addRead($handle, array($this, 'handleServer')); @@ -564,7 +564,7 @@ final class Application extends Base //-------------------------------------- - foreach ($config->get('xcp') as $_) + foreach ($config['xcp'] as $_) { $xcp = new XCP($loop, $_['url'], $_['username'], $_['password']); $xcp->queue( @@ -782,7 +782,7 @@ final class Application extends Base )); $bytes = @file_put_contents( - $this->_di->get('config')->get('database.json'), + $this->_di->get('config')['database.json'], $data ); if ($bytes === false) @@ -799,7 +799,7 @@ final class Application extends Base */ private function _loadDatabase() { - $file = $this->_di->get('config')->get('database.json'); + $file = $this->_di->get('config')['database.json']; if (!file_exists($file)) { trigger_error( @@ -824,7 +824,7 @@ final class Application extends Base } $data = @file_get_contents( - $this->_di->get('config')->get('database.json') + $this->_di->get('config')['database.json'] ); if (($data === false) || (($data = json_decode($data, true)) === null)) diff --git a/lib/Config.php b/lib/Config.php index cafe89c..7469452 100644 --- a/lib/Config.php +++ b/lib/Config.php @@ -25,7 +25,10 @@ /** * */ -final class Config extends Base +final class Config extends Base implements + ArrayAccess, + Countable, + IteratorAggregate { /** * @@ -76,6 +79,19 @@ final class Config extends Base return $this->_resolve($entry); } + /** + * + */ + function merge($entries) + { + if ($entries instanceof self) + { + $entries = $entries->_entries; + } + + $this->_entries = array_merge_recursive($this->_entries, $entries); + } + /** * * @@ -119,6 +135,66 @@ final class Config extends Base $entry = $value; } + //-------------------------------------- + + /** + * + */ + function offsetGet($offset) + { + return $this->get($offset); + } + + /** + * + */ + function offsetExists($offset) + { + return (isset($this->_entries[$offset]) + || array_key_exists($offset, $this->_entries)); + } + + /** + * + */ + function offsetSet($offset, $value) + { + $this->set($offset, $value); + } + + /** + * + */ + function offsetUnset($index) + { + trigger_error( + get_class($this).'['.var_export($index, true).'] is not deletable', + E_USER_ERROR + ); + } + + //-------------------------------------- + + /** + * @return integer + */ + function count() + { + return count($this->_entries); + } + + //-------------------------------------- + + /** + * + */ + function getIterator() + { + return \ArrayIterator($this->_entries); + } + + //-------------------------------------- + /** * @var array */ diff --git a/xo-server b/xo-server index a6ca94b..c185056 100755 --- a/xo-server +++ b/xo-server @@ -32,23 +32,22 @@ function _bootstrap() if (!isset($application)) { - // Variables definition. - $root_dir = defined('__DIR__') - ? __DIR__ - : dirname(__FILE__) - ; - // Class autoloading is done by composer. - require($root_dir.'/vendor/autoload.php'); + require(__DIR__.'/vendor/autoload.php'); // Reads configuration. - $config = new Config(array_merge_recursive( - require($root_dir.'/config/global.php'), - require($root_dir.'/config/local.php') - )); + $config = new Config; + foreach (array('global', 'local') as $file) + { + $file = __DIR__.'/config/'.$file.'.php'; + if (is_file($file)) + { + $config->merge(require($file)); + } + } // Injects some variables. - $config->set('root_dir', $root_dir); + $config['root_dir'] = __DIR__; // Dependency injector. $di = new DI;