|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher; |
|
4
|
|
|
|
|
5
|
|
|
use Brendt\Image\Config\DefaultConfigurator; |
|
6
|
|
|
use Brendt\Image\ResponsiveFactory; |
|
7
|
|
|
use Brendt\Stitcher\Factory\AdapterFactory; |
|
8
|
|
|
use Brendt\Stitcher\Factory\ParserFactory; |
|
9
|
|
|
use Brendt\Stitcher\Factory\TemplateEngineFactory; |
|
10
|
|
|
use Brendt\Stitcher\Template\smarty\SmartyEngine; |
|
11
|
|
|
use Brendt\Stitcher\Template\TemplatePlugin; |
|
12
|
|
|
use CSSmin; |
|
13
|
|
|
use Leafo\ScssPhp\Compiler; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
15
|
|
|
use Symfony\Component\Finder\Finder; |
|
16
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
17
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @todo Change parsing to work the other way around |
|
21
|
|
|
*/ |
|
22
|
|
|
class Config |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected static $config; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ContainerBuilder |
|
32
|
|
|
*/ |
|
33
|
|
|
protected static $container; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $root |
|
37
|
|
|
* @param string $name |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function load($root = './', $name = 'config.yml') { |
|
40
|
|
|
/** @var SplFileInfo[] $configFiles */ |
|
41
|
|
|
$configFiles = Finder::create()->files()->in($root)->name($name); |
|
42
|
|
|
$config = []; |
|
43
|
|
|
|
|
44
|
|
|
foreach ($configFiles as $configFile) { |
|
45
|
|
|
$config += Yaml::parse($configFile->getContents()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
foreach ($config as $key => $value) { |
|
49
|
|
|
self::$config[$key] = $value; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (!self::get('directories.template')) { |
|
53
|
|
|
self::$config['directories']['template'] = self::get('directories.src') . '/template'; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
self::$container = new ContainerBuilder(); |
|
57
|
|
|
self::$container->register('factory.parser', ParserFactory::class); |
|
58
|
|
|
self::$container->register('factory.adapter', AdapterFactory::class); |
|
59
|
|
|
self::$container->register('factory.template.engine', TemplateEngineFactory::class); |
|
60
|
|
|
|
|
61
|
|
|
$imageConfig = new DefaultConfigurator([ |
|
62
|
|
|
'driver' => Config::get('engines.image'), |
|
63
|
|
|
'publicPath' => Config::get('directories.public'), |
|
64
|
|
|
'sourcePath' => Config::get('directories.src'), |
|
65
|
|
|
'enableCache' => Config::get('caches.image'), |
|
66
|
|
|
'optimize' => Config::get('engines.optimizer', false), |
|
|
|
|
|
|
67
|
|
|
'async' => true, |
|
68
|
|
|
]); |
|
69
|
|
|
|
|
70
|
|
|
self::$container->register('factory.image', ResponsiveFactory::class) |
|
71
|
|
|
->addArgument($imageConfig); |
|
72
|
|
|
|
|
73
|
|
|
self::$container->register('engine.smarty', SmartyEngine::class); |
|
74
|
|
|
self::$container->register('engine.plugin', TemplatePlugin::class); |
|
75
|
|
|
self::$container->register('engine.minify.css', CSSmin::class); |
|
76
|
|
|
self::$container->register('engine.sass', Compiler::class) |
|
77
|
|
|
->addMethodCall('addImportPath', ['path' => Config::get('directories.src')]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param $id |
|
82
|
|
|
* |
|
83
|
|
|
* @return object |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function getDependency($id) { |
|
86
|
|
|
return self::$container->get($id); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string $key |
|
91
|
|
|
* @param null|string $default |
|
92
|
|
|
* |
|
93
|
|
|
* @return mixed|null |
|
94
|
|
|
* @todo Refactor to work with dependencies |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function get(string $key, string $default = null) { |
|
97
|
|
|
$keys = explode('.', $key); |
|
98
|
|
|
$config = self::$config; |
|
99
|
|
|
|
|
100
|
|
|
reset($keys); |
|
101
|
|
|
|
|
102
|
|
|
while (($key = current($keys)) && isset($config[$key])) { |
|
103
|
|
|
$hasNext = next($keys); |
|
104
|
|
|
|
|
105
|
|
|
if (!$hasNext) { |
|
106
|
|
|
return $config[$key]; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$config = $config[$key]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $default; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param $key |
|
117
|
|
|
* @param $value |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function set($key, $value) { |
|
120
|
|
|
$keys = explode('.', $key); |
|
121
|
|
|
$configEntry = self::createConfigEntry($keys, $value); |
|
122
|
|
|
|
|
123
|
|
|
self::$config = array_merge(self::$config, $configEntry); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Reset the config |
|
128
|
|
|
*/ |
|
129
|
|
|
public static function reset() { |
|
130
|
|
|
self::$config = []; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
|
|
public static function getConfig() : array { |
|
137
|
|
|
return self::$config; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param $config |
|
142
|
|
|
* @param string $prefix |
|
143
|
|
|
* |
|
144
|
|
|
* @return array |
|
145
|
|
|
*/ |
|
146
|
|
|
public static function flatten(array $config, string $prefix = '') : array { |
|
147
|
|
|
$result = []; |
|
148
|
|
|
|
|
149
|
|
|
foreach ($config as $key => $value) { |
|
150
|
|
|
$new_key = $prefix . (empty($prefix) ? '' : '.') . $key; |
|
151
|
|
|
|
|
152
|
|
|
if (is_array($value)) { |
|
153
|
|
|
$result = array_merge($result, self::flatten($value, $new_key)); |
|
154
|
|
|
} else { |
|
155
|
|
|
$result[$new_key] = $value; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $result; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param $keys |
|
164
|
|
|
* @param $value |
|
165
|
|
|
* |
|
166
|
|
|
* @return array |
|
167
|
|
|
*/ |
|
168
|
|
|
private static function createConfigEntry($keys, $value) : array { |
|
169
|
|
|
$configEntry = []; |
|
170
|
|
|
$key = array_shift($keys); |
|
171
|
|
|
|
|
172
|
|
|
if (!count($keys)) { |
|
173
|
|
|
return [$key => $value]; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$configEntry[$key] = self::createConfigEntry($keys, $value); |
|
177
|
|
|
|
|
178
|
|
|
return $configEntry; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: