1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher; |
4
|
|
|
|
5
|
|
|
use Brendt\Stitcher\Plugin\PluginConfiguration; |
6
|
|
|
use Symfony\Component\Config\FileLocator; |
7
|
|
|
use Symfony\Component\Console\Application; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
11
|
|
|
|
12
|
|
|
class App |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ContainerBuilder |
16
|
|
|
*/ |
17
|
|
|
protected static $container; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected static $configDefaults = [ |
23
|
|
|
'environment' => 'development', |
24
|
|
|
'directories.src' => './src', |
25
|
|
|
'directories.public' => './public', |
26
|
|
|
'directories.cache' => './.cache', |
27
|
|
|
'directories.htaccess' => './public/.htaccess', |
28
|
|
|
'meta' => [], |
29
|
|
|
'minify' => false, |
30
|
|
|
'engines.template' => 'smarty', |
31
|
|
|
'engines.image' => 'gd', |
32
|
|
|
'engines.optimizer' => true, |
33
|
|
|
'engines.async' => true, |
34
|
|
|
'cdn' => [], |
35
|
|
|
'caches.image' => true, |
36
|
|
|
'caches.cdn' => true, |
37
|
|
|
'optimizer.options' => [], |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $configPath |
42
|
|
|
* @param array $defaultConfig |
43
|
|
|
* |
44
|
|
|
* @return App |
45
|
|
|
*/ |
46
|
|
|
public static function init(string $configPath = './config.yml', array $defaultConfig = []) : App { |
47
|
|
|
self::$container = new ContainerBuilder(); |
48
|
|
|
|
49
|
|
|
$configFile = Config::getConfigFile($configPath); |
50
|
|
|
$parsedDefaultConfig = Yaml::parse($configFile->getContents()); |
51
|
|
|
$parsedImportConfig = Config::parseImports($parsedDefaultConfig); |
52
|
|
|
|
53
|
|
|
$config = array_merge( |
54
|
|
|
self::$configDefaults, |
55
|
|
|
$parsedImportConfig, |
56
|
|
|
Config::flatten($parsedImportConfig), |
57
|
|
|
$defaultConfig |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$config['directories.template'] = $config['directories.template'] ?? $config['directories.src']; |
61
|
|
|
|
62
|
|
|
foreach ($config as $key => $value) { |
63
|
|
|
self::$container->setParameter($key, $value); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$serviceLoader = new YamlFileLoader(self::$container, new FileLocator(__DIR__)); |
67
|
|
|
$serviceLoader->load(__DIR__ . '/../../services.yml'); |
68
|
|
|
$pluginConfigurationCollection = self::loadPlugins($config); |
69
|
|
|
self::loadPluginServices($serviceLoader, $pluginConfigurationCollection); |
70
|
|
|
self::loadPluginConfig($pluginConfigurationCollection); |
71
|
|
|
|
72
|
|
|
return new self(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $config |
77
|
|
|
* |
78
|
|
|
* @return PluginConfiguration[] |
79
|
|
|
*/ |
80
|
|
|
public static function loadPlugins(array $config) : array { |
81
|
|
|
if (!isset($config['plugins'])) { |
82
|
|
|
return []; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$pluginConfigurationCollection = []; |
86
|
|
|
|
87
|
|
|
foreach ($config['plugins'] as $pluginPath) { |
88
|
|
|
$pluginConfiguration = new PluginConfiguration($pluginPath); |
89
|
|
|
|
90
|
|
|
$pluginConfigurationCollection[] = $pluginConfiguration; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $pluginConfigurationCollection; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param PluginConfiguration[] $pluginConfigurationCollection |
98
|
|
|
*/ |
99
|
|
|
public static function loadPluginConfig(array $pluginConfigurationCollection) { |
100
|
|
|
foreach ($pluginConfigurationCollection as $pluginConfig) { |
101
|
|
|
$flatPluginConfig = Config::flatten($pluginConfig->getConfig()); |
102
|
|
|
|
103
|
|
|
foreach ($flatPluginConfig as $key => $value) { |
104
|
|
|
if (!self::$container->hasParameter($key)) { |
105
|
|
|
self::$container->setParameter($key, $value); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param YamlFileLoader $serviceLoader |
113
|
|
|
* @param array $pluginConfigurationCollection |
114
|
|
|
*/ |
115
|
|
|
public static function loadPluginServices(YamlFileLoader $serviceLoader, array $pluginConfigurationCollection) { |
116
|
|
|
foreach ($pluginConfigurationCollection as $pluginConfiguration) { |
117
|
|
|
$servicePath = $pluginConfiguration->getServicePath(); |
118
|
|
|
|
119
|
|
|
if (!$servicePath) { |
120
|
|
|
continue; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$serviceLoader->load($servicePath); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $id |
129
|
|
|
* |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public static function get(string $id) { |
133
|
|
|
return self::$container->get($id); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $id |
138
|
|
|
* @param Application $application |
139
|
|
|
*/ |
140
|
|
|
public static function setApplication(string $id, Application $application) { |
141
|
|
|
self::$container->set($id, $application); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $key |
146
|
|
|
* |
147
|
|
|
* @return mixed |
148
|
|
|
*/ |
149
|
|
|
public static function getParameter(string $key) { |
150
|
|
|
return self::$container->getParameter($key); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|