|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Bldr.io |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Aaron Scherer <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Bldr; |
|
13
|
|
|
|
|
14
|
|
|
use Bldr\DependencyInjection\ContainerBuilder; |
|
15
|
|
|
use Bldr\DependencyInjection\Loader; |
|
16
|
|
|
use Bldr\Exception\ConfigurationFileNotFoundException; |
|
17
|
|
|
use Symfony\Component\Config\FileLocator; |
|
18
|
|
|
use Symfony\Component\Config\Loader\DelegatingLoader; |
|
19
|
|
|
use Symfony\Component\Config\Loader\LoaderResolver; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Aaron Scherer <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class Config |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string $NAME |
|
29
|
|
|
*/ |
|
30
|
|
|
public static $NAME = '.bldr'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array $TYPES |
|
34
|
|
|
*/ |
|
35
|
|
|
public static $TYPES = ['yml', 'xml', 'php', 'ini', 'json', 'toml']; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string $DEFAULT_TYPE |
|
39
|
|
|
*/ |
|
40
|
|
|
public static $DEFAULT_TYPE = 'yml'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param ContainerBuilder $container |
|
44
|
|
|
* |
|
45
|
|
|
* @throws Exception\ConfigurationFileNotFoundException |
|
46
|
|
|
* @throws \Exception |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function read(ContainerBuilder $container) |
|
49
|
|
|
{ |
|
50
|
|
|
/** @var InputInterface $input */ |
|
51
|
|
|
$input = $container->get('input'); |
|
52
|
|
|
|
|
53
|
|
|
$locations = [getcwd(), getcwd().'/.bldr/']; |
|
54
|
|
|
if ($input->hasParameterOption('--global')) { |
|
55
|
|
|
$locations = array_merge($locations, [getenv('HOME'), getenv('HOME').'/.bldr/']); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$locator = new FileLocator($locations); |
|
59
|
|
|
$resolver = new LoaderResolver( |
|
60
|
|
|
[ |
|
61
|
|
|
new Loader\YamlFileLoader($container, $locator), |
|
62
|
|
|
new Loader\XmlFileLoader($container, $locator), |
|
63
|
|
|
new Loader\PhpFileLoader($container, $locator), |
|
64
|
|
|
new Loader\IniFileLoader($container, $locator), |
|
65
|
|
|
new Loader\JsonFileLoader($container, $locator), |
|
66
|
|
|
new Loader\TomlFileLoader($container, $locator) |
|
67
|
|
|
] |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
$loader = new DelegatingLoader($resolver); |
|
71
|
|
|
$files = static::findFiles($input); |
|
72
|
|
|
$foundConfig = false; |
|
73
|
|
|
foreach ($files as $file) { |
|
74
|
|
|
try { |
|
75
|
|
|
$loader->load($file); |
|
76
|
|
|
$foundConfig = true; |
|
77
|
|
|
} catch (\Exception $e) { |
|
78
|
|
|
if (get_class($e) !== 'InvalidArgumentException') { |
|
79
|
|
|
throw $e; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (!$foundConfig) { |
|
85
|
|
|
throw new ConfigurationFileNotFoundException( |
|
86
|
|
|
sprintf( |
|
87
|
|
|
"Either couldn't find the configuration file, or couldn't read it. ". |
|
88
|
|
|
"Make sure the extension is valid (%s). Tried: %s", |
|
89
|
|
|
implode(', ', static::$TYPES), |
|
90
|
|
|
implode(', ', $files) |
|
91
|
|
|
) |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param InputInterface $input |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
* @throws \Exception |
|
101
|
|
|
*/ |
|
102
|
|
|
public static function findFiles(InputInterface $input) |
|
103
|
|
|
{ |
|
104
|
|
|
if ($input->hasParameterOption('--config-file')) { |
|
105
|
|
|
$file = $input->getParameterOption('--config-file'); |
|
106
|
|
|
if (file_exists($file)) { |
|
107
|
|
|
return [$file]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
throw new ConfigurationFileNotFoundException( |
|
111
|
|
|
sprintf("Couldn't find the configuration file: %s", $file) |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$format = $input->hasParameterOption('--config-format') |
|
116
|
|
|
? $input->getParameterOption('--config-format') |
|
117
|
|
|
: static::$DEFAULT_TYPE |
|
118
|
|
|
; |
|
119
|
|
|
|
|
120
|
|
|
return [ |
|
121
|
|
|
sprintf("%s.%s", static::$NAME, $format), |
|
122
|
|
|
sprintf("%s.%s.dist", static::$NAME, $format) |
|
123
|
|
|
]; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|