Skip to content
This repository was archived by the owner on Oct 26, 2024. It is now read-only.

Commit 38bc0ec

Browse files
committed
Refactoring of AbstractConfig and virtualized file handling
Refactored ConfigAbstract to AbstractConfig to keep PSR compliance. Switched from bare testdata to virtualized files. Added file importing and base merging of default and import nodes.
1 parent 774c7a0 commit 38bc0ec

13 files changed

+181
-138
lines changed

Config/ConfigAbstract.php renamed to Config/AbstractConfig.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,30 @@
1313
use Symfony\Component\Yaml\Yaml;
1414

1515
/**
16-
* Class ConfigAbstract
16+
* Class AbstractConfig
1717
*
1818
* @package Asm\Config
1919
* @author marc aschmann <[email protected]>
2020
* @codeCoverageIgnore
2121
* @uses Asm\Data\Data
2222
* @uses Symfony\Component\Yaml\Yaml
2323
*/
24-
abstract class ConfigAbstract extends Data
24+
abstract class AbstractConfig extends Data
2525
{
2626
/**
2727
* @var bool
2828
*/
2929
protected $filecheck = true;
3030

3131
/**
32-
* @var null
32+
* @var array
3333
*/
34-
protected $imports;
34+
protected $imports = [];
3535

3636
/**
37-
* @var null
37+
* @var array
3838
*/
39-
protected $default;
39+
protected $default = [];
4040

4141
/**
4242
* Default constructor.
@@ -73,8 +73,9 @@ public function addConfig($name, $file)
7373
public function readConfig($file)
7474
{
7575
$config = $this->readFile($file);
76-
$config = $this->extractDefault($config);
7776
$config = $this->extractImports($config);
77+
$config = $this->extractDefault($config);
78+
$this->mergeDefault();
7879

7980
return $config;
8081
}
@@ -86,7 +87,12 @@ public function readConfig($file)
8687
*/
8788
public function setConfig($file)
8889
{
89-
$this->setByArray($this->readConfig($file));
90+
$this->setByArray(
91+
array_replace_recursive(
92+
$this->default,
93+
$this->readConfig($file)
94+
)
95+
);
9096
}
9197

9298
/**
@@ -107,19 +113,23 @@ private function readFile($file)
107113
}
108114

109115
/**
116+
* get all import files from config, if set and remove node.
117+
*
110118
* @param array $config
111119
* @return array
112120
*/
113-
protected function extractImports(array $config)
121+
private function extractImports(array $config)
114122
{
115123
if (array_key_exists('imports', $config) && 0 < count($config['imports'])) {
116124
$this->imports = [];
117-
foreach ($config['imports'] as $import) {
118-
if (false !== empty($import['resource'])) {
119-
array_replace_recursive(
125+
foreach ($config['imports'] as $key => $import) {
126+
if (false === empty($import['resource'])) {
127+
$this->imports = array_replace_recursive(
120128
$this->imports,
121129
$this->readFile($import['resource'])
122130
);
131+
132+
unset($config['resource'][$key]);
123133
}
124134
}
125135
}
@@ -128,11 +138,26 @@ protected function extractImports(array $config)
128138
}
129139

130140
/**
141+
* Get default values if set and remove node from config.
142+
*
131143
* @param array $config
132144
* @return array
133145
*/
134-
protected function extractDefault($config)
146+
private function extractDefault($config)
135147
{
148+
if (array_key_exists('default', $config)) {
149+
$this->default = $config['default'];
150+
unset($config['default']);
151+
}
152+
136153
return $config;
137154
}
155+
156+
/**
157+
* Prepare the defaults and replace recursively.
158+
*/
159+
private function mergeDefault()
160+
{
161+
$this->default = array_replace_recursive($this->imports, $this->default);
162+
}
138163
}

Config/Config.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@
1717
*/
1818
final class Config
1919
{
20-
/**
21-
* @var array
22-
*/
23-
private static $whitelist = [
24-
'ConfigDefault',
25-
'ConfigEnv',
26-
'ConfigTimer',
27-
];
28-
2920
/**
3021
* Get object of specific class.
3122
*
@@ -37,11 +28,11 @@ final class Config
3728
*/
3829
public static function factory(array $param, $class = 'ConfigDefault')
3930
{
40-
if (in_array($class, self::$whitelist)) {
41-
if (false === strpos($class, 'Asm')) {
42-
$class = __NAMESPACE__ . '\\' . $class;
43-
}
31+
if (false === strpos($class, 'Asm')) {
32+
$class = __NAMESPACE__ . '\\' . $class;
33+
}
4434

35+
if (class_exists($class)) {
4536
// allow config names without ending
4637
if (empty($param['file'])) {
4738
throw new \InvalidArgumentException('Config::factory() - config filename missing in param array!');

Config/ConfigDefault.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
* @package Asm\Config
1616
* @author marc aschmann <[email protected]>
1717
*/
18-
final class ConfigDefault extends ConfigAbstract implements ConfigInterface
18+
final class ConfigDefault extends AbstractConfig implements ConfigInterface
1919
{
2020
}

Config/ConfigEnv.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @package Asm\Config
1818
* @author marc aschmann <[email protected]>
1919
*/
20-
final class ConfigEnv extends ConfigAbstract implements ConfigInterface
20+
final class ConfigEnv extends AbstractConfig implements ConfigInterface
2121
{
2222
/**
2323
* @var string
@@ -66,6 +66,11 @@ private function mergeEnvironments($param)
6666
$merged = $config->get($this->defaultEnv);
6767
}
6868

69-
$this->setByArray($merged);
69+
$this->setByArray(
70+
array_replace_recursive(
71+
$this->default,
72+
$merged
73+
)
74+
);
7075
}
7176
}

Config/ConfigEnvExtended.php

Lines changed: 0 additions & 71 deletions
This file was deleted.

Config/ConfigTimer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @package Asm\Config
1616
* @author marc aschmann <[email protected]>
1717
*/
18-
final class ConfigTimer extends ConfigAbstract implements ConfigInterface
18+
final class ConfigTimer extends AbstractConfig implements ConfigInterface
1919
{
2020
/**
2121
* Convert config date strings to \DateTime objects or \DateIntervals.

Test/BaseConfigTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/*
3+
* This file is part of the php-utilities package.
4+
*
5+
* (c) Marc Aschmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Asm\Test;
12+
13+
use org\bovigo\vfs\vfsStream;
14+
15+
/**
16+
* Class BaseConfigTest
17+
*
18+
* @package Asm\Test
19+
* @author Marc Aschmann <[email protected]>
20+
*/
21+
class BaseConfigTest extends \PHPUnit_Framework_TestCase
22+
{
23+
private $root;
24+
private $configFile;
25+
private $configImportFile;
26+
private $configTimerFile;
27+
28+
/**
29+
* default setup
30+
*/
31+
public function setUp()
32+
{
33+
parent::setUp();
34+
35+
$this->root = vfsStream::setup('configs');
36+
$this->configFile = vfsStream::newFile('default.yml')->at($this->root);
37+
$this->configFile->setContent(TestData::getYamlConfigFile());
38+
39+
$this->configImportFile = vfsStream::newFile('testimport.yml')->at($this->root);
40+
$this->configImportFile->setContent(TestData::getYamlImportFile());
41+
42+
$this->configTimerFile = vfsStream::newFile('testTimer.yml')->at($this->root);
43+
$this->configTimerFile->setContent(TestData::getYamlTimerConfigFile());
44+
}
45+
46+
/**
47+
* @return mixed
48+
*/
49+
public function getTestYaml()
50+
{
51+
return $this->configFile->url();
52+
}
53+
54+
/**
55+
* @return mixed
56+
*/
57+
public function getTimerYaml()
58+
{
59+
return $this->configTimerFile->url();
60+
}
61+
}

Test/TestData.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
*/
1313
class TestData
1414
{
15+
public static function getYamlImportFile()
16+
{
17+
return <<<EOT
18+
testkey_5:
19+
default: yaddayadda
20+
my_test: is testing hard
21+
EOT;
22+
}
23+
1524
/**
1625
* provide yaml schema testdata
1726
*
@@ -21,7 +30,7 @@ public static function getYamlConfigFile()
2130
{
2231
return <<<EOT
2332
imports:
24-
- { resource: testimport.yml }
33+
- { resource: 'vfs://configs/testimport.yml' }
2534
default:
2635
testkey_4: 'default test'
2736
prod:

Tests/Config/ConfigDefaultTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212
use Asm\Config\Config;
1313
use Asm\Config\ConfigDefault;
14-
use Asm\Test\TestData;
14+
use Asm\Test\BaseConfigTest;
1515

1616
/**
1717
* Class ConfigDefaultTest
1818
*
1919
* @package Asm\Tests\Config
2020
* @author marc aschmann <[email protected]>
2121
*/
22-
class ConfigDefaultTest extends \PHPUnit_Framework_TestCase
22+
class ConfigDefaultTest extends BaseConfigTest
2323
{
2424
/**
2525
* @covers \Asm\Config\ConfigAbstract::readConfig
@@ -29,7 +29,7 @@ public function testFactory()
2929
{
3030
$config = Config::factory(
3131
[
32-
'file' => TestData::getYamlConfigFile(),
32+
'file' => $this->getTestYaml(),
3333
'filecheck' => false,
3434
],
3535
'ConfigDefault'

0 commit comments

Comments
 (0)