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

Commit 774c7a0

Browse files
author
Marc Aschmann
committed
Implementing inline import and default values
1 parent 2841c7d commit 774c7a0

File tree

5 files changed

+166
-5
lines changed

5 files changed

+166
-5
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Changelog
2+
3+
## v2.0.0
4+
5+
* Dropping php 5.3 support
6+
* architectural changes
7+
8+
## v1.2.0
9+
10+
* improved overall code quality (scrutinizer suggestions)
11+
* improved code coverage
12+
* reworked __construct() behavior of Config classes
13+
14+
## v1.1.0
15+
16+
* Moved to PSR-4, closed extension points
17+
18+
## v1.0.0
19+
20+
* First stable draft release

Config/ConfigAbstract.php

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ abstract class ConfigAbstract extends Data
2828
*/
2929
protected $filecheck = true;
3030

31+
/**
32+
* @var null
33+
*/
34+
protected $imports;
35+
36+
/**
37+
* @var null
38+
*/
39+
protected $default;
40+
3141
/**
3242
* Default constructor.
3343
*
@@ -61,6 +71,31 @@ public function addConfig($name, $file)
6171
* @return array config array
6272
*/
6373
public function readConfig($file)
74+
{
75+
$config = $this->readFile($file);
76+
$config = $this->extractDefault($config);
77+
$config = $this->extractImports($config);
78+
79+
return $config;
80+
}
81+
82+
/**
83+
* Add config to data storage.
84+
*
85+
* @param string $file absolute filepath/filename.ending
86+
*/
87+
public function setConfig($file)
88+
{
89+
$this->setByArray($this->readConfig($file));
90+
}
91+
92+
/**
93+
* Read yaml files.
94+
*
95+
* @param string $file path/filename
96+
* @return array
97+
*/
98+
private function readFile($file)
6499
{
65100
if ($this->filecheck && !is_file($file)) {
66101
throw new \InvalidArgumentException(
@@ -72,12 +107,32 @@ public function readConfig($file)
72107
}
73108

74109
/**
75-
* Add config to data storage.
76-
*
77-
* @param string $file absolute filepath/filename.ending
110+
* @param array $config
111+
* @return array
78112
*/
79-
public function setConfig($file)
113+
protected function extractImports(array $config)
80114
{
81-
$this->setByArray($this->readConfig($file));
115+
if (array_key_exists('imports', $config) && 0 < count($config['imports'])) {
116+
$this->imports = [];
117+
foreach ($config['imports'] as $import) {
118+
if (false !== empty($import['resource'])) {
119+
array_replace_recursive(
120+
$this->imports,
121+
$this->readFile($import['resource'])
122+
);
123+
}
124+
}
125+
}
126+
127+
return $config;
128+
}
129+
130+
/**
131+
* @param array $config
132+
* @return array
133+
*/
134+
protected function extractDefault($config)
135+
{
136+
return $config;
82137
}
83138
}

Config/ConfigEnvExtended.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/*
3+
* This file is part of the php-utilities package.
4+
*
5+
* (c) Marc Aschmann <maschmann@gmail.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Asm\Config;
11+
12+
use Asm\Data\Data;
13+
14+
/**
15+
* Class ConfigDefault
16+
*
17+
* @package Asm\Config
18+
* @author marc aschmann <maschmann@gmail.com>
19+
*/
20+
final class ConfigEnv extends ConfigAbstract implements ConfigInterface
21+
{
22+
/**
23+
* @var string
24+
*/
25+
private $defaultEnv = 'prod';
26+
27+
/**
28+
* Default constructor.
29+
*
30+
* @param array $param
31+
*/
32+
public function __construct(array $param)
33+
{
34+
if (isset($param['filecheck'])) {
35+
$this->filecheck = (bool)$param['filecheck'];
36+
}
37+
38+
if (!empty($param['defaultEnv'])) {
39+
$this->defaultEnv = $param['defaultEnv'];
40+
}
41+
42+
$this->mergeEnvironments($param);
43+
}
44+
45+
/**
46+
* Merge environments based on defaults array.
47+
*
48+
* Merge order is prod -> lesser environment.
49+
*
50+
* @param array $param
51+
*/
52+
private function mergeEnvironments($param)
53+
{
54+
$config = new Data();
55+
$config->setByArray(
56+
$this->readConfig($param['file'])
57+
);
58+
59+
if (!empty($param['env']) && $this->defaultEnv !== $param['env']) {
60+
$toMerge = $config->get($param['env'], []);
61+
$merged = array_replace_recursive(
62+
$config->get($this->defaultEnv),
63+
$toMerge
64+
);
65+
} else {
66+
$merged = $config->get($this->defaultEnv);
67+
}
68+
69+
$this->setByArray($merged);
70+
}
71+
}

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,19 @@ Here you get a static factory which is able to produce three types of configurat
2626
Available types are:
2727
* ConfigDefault - basic config object, stores the yaml content "as is", provides get/set.
2828
* ConfigEnv - provides the possibility to get an environment-merged config. Based on the currently provided env, you'll have e.g. prod -> dev merged, with prod node as a master.
29+
* ConfigEnvExtended - same as ConfigEnv, adds the possibility to have "default" node on env-level, which will be the merge-base before prod. Merge: default -> prod -> $env
2930
* ConfigTimer is a specialised for of config to provide pre-generated DateTime objects for the Timer class.
3031

32+
Configs now also support the "imports" syntax from symfony configs.
33+
34+
```
35+
imports:
36+
- { resource: defaults.yml }
37+
```
38+
39+
Imported configs will be treated just like the "default" node and become the base for merging.
40+
The order of merges is import -> default -> prod -> $env.
41+
3142
### Timer
3243
Provides functionality to check if there's a current holiday, has configurable "timers" to check uf e.g. your hotline should be available etc.
3344
Extensive examples can be found within both, the TestData and UnitTests :-)

Test/TestData.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class TestData
2020
public static function getYamlConfigFile()
2121
{
2222
return <<<EOT
23+
imports:
24+
- { resource: testimport.yml }
25+
default:
26+
testkey_4: 'default test'
2327
prod:
2428
testkey_1: 'testvalue'
2529
testkey_2:

0 commit comments

Comments
 (0)