mirror of
https://github.com/torrentpier/torrentpier
synced 2025-08-22 22:33:55 -07:00
#58 Expansion Zend Config
This commit is contained in:
parent
9df0c8d9d3
commit
b8bc1f12aa
4 changed files with 86 additions and 6 deletions
|
@ -22,7 +22,7 @@ $config = [
|
||||||
// Database
|
// Database
|
||||||
'db' => [
|
'db' => [
|
||||||
'driver' => 'Pdo_Mysql',
|
'driver' => 'Pdo_Mysql',
|
||||||
'hostname' => 'localhost',
|
'hostname' => '127.0.0.1',
|
||||||
'database' => 'tp_220',
|
'database' => 'tp_220',
|
||||||
'username' => 'user',
|
'username' => 'user',
|
||||||
'password' => 'pass',
|
'password' => 'pass',
|
||||||
|
@ -31,8 +31,8 @@ $config = [
|
||||||
|
|
||||||
// Sphinx
|
// Sphinx
|
||||||
'sphinx' => [
|
'sphinx' => [
|
||||||
'driver' => 'Pdo_Mysql',
|
'driver' => '{self.db.driver}',
|
||||||
'hostname' => 'localhost',
|
'hostname' => '{self.db.hostname}',
|
||||||
'username' => 'user',
|
'username' => 'user',
|
||||||
'password' => 'pass',
|
'password' => 'pass',
|
||||||
'port' => 9306,
|
'port' => 9306,
|
||||||
|
|
72
src/Config.php
Normal file
72
src/Config.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ namespace TorrentPier\ServiceProviders;
|
||||||
|
|
||||||
use Pimple\Container;
|
use Pimple\Container;
|
||||||
use Pimple\ServiceProviderInterface;
|
use Pimple\ServiceProviderInterface;
|
||||||
|
use TorrentPier\Config;
|
||||||
use Zend\Config\Factory;
|
use Zend\Config\Factory;
|
||||||
|
|
||||||
class ConfigServiceProvider implements ServiceProviderInterface
|
class ConfigServiceProvider implements ServiceProviderInterface
|
||||||
|
@ -14,10 +15,10 @@ class ConfigServiceProvider implements ServiceProviderInterface
|
||||||
public function register(Container $container)
|
public function register(Container $container)
|
||||||
{
|
{
|
||||||
$container['config'] = function ($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'])) {
|
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;
|
return $config;
|
||||||
|
|
|
@ -4,6 +4,7 @@ namespace TorrentPier\ServiceProviders;
|
||||||
|
|
||||||
use Pimple\Container;
|
use Pimple\Container;
|
||||||
use Pimple\ServiceProviderInterface;
|
use Pimple\ServiceProviderInterface;
|
||||||
|
use TorrentPier\Config;
|
||||||
use Zend\Db\Adapter\Adapter;
|
use Zend\Db\Adapter\Adapter;
|
||||||
|
|
||||||
class DbServiceProvider implements ServiceProviderInterface
|
class DbServiceProvider implements ServiceProviderInterface
|
||||||
|
@ -14,7 +15,13 @@ class DbServiceProvider implements ServiceProviderInterface
|
||||||
public function register(Container $container)
|
public function register(Container $container)
|
||||||
{
|
{
|
||||||
$container['db'] = function ($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']);
|
unset($container['config.db']);
|
||||||
|
|
||||||
return $adapter;
|
return $adapter;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue