From b8bc1f12aa8f16ce58aa89e0150a28dac4da969f Mon Sep 17 00:00:00 2001 From: Vasily Komrakov Date: Sat, 13 Feb 2016 19:40:08 +0300 Subject: [PATCH] #58 Expansion Zend Config --- library/config.php | 6 +- src/Config.php | 72 +++++++++++++++++++ .../ConfigServiceProvider.php | 5 +- src/ServiceProviders/DbServiceProvider.php | 9 ++- 4 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 src/Config.php diff --git a/library/config.php b/library/config.php index d3a47a32e..f219f35f6 100644 --- a/library/config.php +++ b/library/config.php @@ -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, diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 000000000..4ccea2368 --- /dev/null +++ b/src/Config.php @@ -0,0 +1,72 @@ +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; + } +} diff --git a/src/ServiceProviders/ConfigServiceProvider.php b/src/ServiceProviders/ConfigServiceProvider.php index bedc9ea41..6739530ab 100644 --- a/src/ServiceProviders/ConfigServiceProvider.php +++ b/src/ServiceProviders/ConfigServiceProvider.php @@ -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; diff --git a/src/ServiceProviders/DbServiceProvider.php b/src/ServiceProviders/DbServiceProvider.php index 23c398a3c..dc7c99ba9 100644 --- a/src/ServiceProviders/DbServiceProvider.php +++ b/src/ServiceProviders/DbServiceProvider.php @@ -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;