1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Veto. |
4
|
|
|
* PHP Microframework. |
5
|
|
|
* |
6
|
|
|
* @author Damien Walsh <[email protected]> |
7
|
|
|
* @copyright Damien Walsh 2013-2014 |
8
|
|
|
* @version 0.1 |
9
|
|
|
* @package veto |
10
|
|
|
*/ |
11
|
|
|
namespace Veto\Configuration; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\Yaml\Parser; |
14
|
|
|
use Veto\Collection\Tree; |
15
|
|
|
use Veto\Exception\ConfigurationException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Hive |
19
|
|
|
* Implements a configuration hive that can handle loading from YAML config files. |
20
|
|
|
* |
21
|
|
|
* @since 0.1 |
22
|
|
|
*/ |
23
|
|
|
class Hive extends Tree |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Load configuration data from a YAML file. |
27
|
|
|
* |
28
|
|
|
* @todo Decouple this from the filesystem - make it accept a YAML string or stream instead |
29
|
|
|
* @throws ConfigurationException |
30
|
|
|
* @param string $path The path to the YAML configuration file. |
31
|
|
|
*/ |
32
|
|
|
public function load($path) |
33
|
|
|
{ |
34
|
|
|
$parser = new Parser(); |
35
|
|
|
$configYAML = file_get_contents($path); |
36
|
|
|
$config = $parser->parse($configYAML); |
37
|
|
|
|
38
|
|
|
// Process any config import directives |
39
|
|
|
$config = $this->processImports($path, $config); |
|
|
|
|
40
|
|
|
|
41
|
|
|
// Merge the configuration hive with this file |
42
|
|
|
$this->merge($config); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Process any @import directives that occur at the top-level of the configuration array. |
47
|
|
|
* |
48
|
|
|
* @param string $basePath The base path used for relative @import directives |
49
|
|
|
* @param array $config The configuration array being processed |
50
|
|
|
* @return array |
51
|
|
|
* @throws ConfigurationException |
52
|
|
|
*/ |
53
|
|
|
private function processImports($basePath, array $config) |
54
|
|
|
{ |
55
|
|
|
// Process any @import directives |
56
|
|
|
if (array_key_exists('@import', $config)) { |
57
|
|
|
|
58
|
|
|
if (!is_array($config['@import'])) { |
59
|
|
|
throw new ConfigurationException( |
60
|
|
|
'@import directives must contain an array of configuration file paths.' |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
foreach($config['@import'] as $importPath) { |
65
|
|
|
$importPath = dirname($basePath) . '/' . $importPath; |
66
|
|
|
|
67
|
|
|
if (!file_exists($importPath)) { |
68
|
|
|
throw ConfigurationException::missingImportedFile( |
69
|
|
|
$importPath, |
70
|
|
|
$basePath |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->load($importPath); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Do not keep these in the configuration hive |
78
|
|
|
unset($config['@import']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $config; |
82
|
|
|
} |
83
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.