Merge pull request #68 from diolektor/58-expansion_zend_config

#58 Expansion Zend Config
This commit is contained in:
Exile 2016-02-13 20:01:22 +03:00
commit 334262e9f7
4 changed files with 86 additions and 6 deletions

View file

@ -22,7 +22,7 @@ $config = [
// Database
'db' => [
'driver' => 'Pdo_Mysql',
'hostname' => 'localhost',
'hostname' => '127.0.0.1',
'database' => 'tp_220',
'username' => 'user',
'password' => 'pass',
@ -31,8 +31,8 @@ $config = [
// Sphinx
'sphinx' => [
'driver' => 'Pdo_Mysql',
'hostname' => 'localhost',
'driver' => '{self.db.driver}',
'hostname' => '{self.db.hostname}',
'username' => 'user',
'password' => 'pass',
'port' => 9306,

72
src/Config.php Normal file
View file

@ -0,0 +1,72 @@
<?php
namespace TorrentPier;
use Zend\Config\Config as ZendConfig;
class Config extends ZendConfig
{
protected $root;
/**
* Config constructor.
*
* @param array $array
* @param bool $allowModifications
* @param Config|null $root
*/
public function __construct(array $array, $allowModifications = false, Config $root = null)
{
$this->allowModifications = (bool) $allowModifications;
$this->root = $root;
foreach ($array as $key => $value) {
if (is_array($value)) {
$this->data[$key] = new static($value, $this->allowModifications, $this);
} else {
$this->data[$key] = $value;
}
}
}
/**
* @inheritdoc
*/
public function get($name, $default = null)
{
$result = parent::get($name, null);
if ($result === null) {
if (strpos($name, '.')) {
$keys = explode('.', $name);
$result = $this;
foreach ($keys as $key) {
$result = $result->get($key);
if ($result === null) {
break;
}
}
}
$result = $result ?: $default;
}
return $this->prepareValue($result);
}
protected function prepareValue($value)
{
if (is_string($value)) {
$strPos = strpos($value, '{self.');
if ($strPos !== false) {
$strPos += 6;
$key = substr($value, $strPos, (strpos($value, '}') - $strPos));
$value = str_replace('{self.'.$key.'}', $this->root->get($key, ''), $value);
}
}
return $value;
}
}

View file

@ -4,6 +4,7 @@ namespace TorrentPier\ServiceProviders;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use TorrentPier\Config;
use Zend\Config\Factory;
class ConfigServiceProvider implements ServiceProviderInterface
@ -14,10 +15,10 @@ class ConfigServiceProvider implements ServiceProviderInterface
public function register(Container $container)
{
$container['config'] = function ($container) {
$config = Factory::fromFile($container['config.file.system.main'], true);
$config = new Config(Factory::fromFile($container['config.file.system.main']));
if (isset($container['config.file.local.main']) && file_exists($container['config.file.local.main'])) {
$config->merge(Factory::fromFile($container['config.file.local.main'], true));
$config->merge(new Config(Factory::fromFile($container['config.file.local.main'])));
}
return $config;

View file

@ -4,6 +4,7 @@ namespace TorrentPier\ServiceProviders;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use TorrentPier\Config;
use Zend\Db\Adapter\Adapter;
class DbServiceProvider implements ServiceProviderInterface
@ -14,7 +15,13 @@ class DbServiceProvider implements ServiceProviderInterface
public function register(Container $container)
{
$container['db'] = function ($container) {
$adapter = new Adapter($container['config.db']);
$config = $container['config.db'];
if ($config instanceof Config) {
$config = $config->toArray();
}
$adapter = new Adapter($config);
unset($container['config.db']);
return $adapter;