|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stitcher; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Pageon\Config; |
|
7
|
|
|
use Stitcher\Application\DevelopmentServer; |
|
8
|
|
|
use Stitcher\Application\ProductionServer; |
|
9
|
|
|
use Stitcher\Application\Router; |
|
10
|
|
|
use Stitcher\Exception\InvalidConfiguration; |
|
11
|
|
|
use Stitcher\Exception\InvalidPlugin; |
|
12
|
|
|
use Symfony\Component\Config\FileLocator; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
17
|
|
|
|
|
18
|
|
|
class App |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var ContainerBuilder */ |
|
21
|
|
|
protected static $container; |
|
22
|
|
|
|
|
23
|
9 |
|
public static function init(): void |
|
24
|
|
|
{ |
|
25
|
9 |
|
Config::init(); |
|
26
|
|
|
|
|
27
|
9 |
|
self::$container = new ContainerBuilder(); |
|
28
|
|
|
|
|
29
|
9 |
|
self::loadConfig(Config::all()); |
|
30
|
|
|
|
|
31
|
9 |
|
self::loadServices('services.yaml'); |
|
32
|
|
|
|
|
33
|
9 |
|
self::loadPlugins(); |
|
34
|
|
|
|
|
35
|
9 |
|
self::loadRoutes(); |
|
36
|
9 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $id |
|
40
|
|
|
* |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
* @throws \Exception |
|
43
|
|
|
*/ |
|
44
|
9 |
|
public static function get(string $id) |
|
45
|
|
|
{ |
|
46
|
9 |
|
return self::$container->get($id); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function developmentServer(): DevelopmentServer |
|
50
|
|
|
{ |
|
51
|
|
|
try { |
|
52
|
|
|
return self::get(DevelopmentServer::class); |
|
53
|
|
|
} catch (ParameterNotFoundException $e) { |
|
54
|
|
|
throw InvalidConfiguration::missingParameter($e->getKey()); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function productionServer(): ProductionServer |
|
59
|
|
|
{ |
|
60
|
|
|
try { |
|
61
|
|
|
return self::get(ProductionServer::class); |
|
62
|
|
|
} catch (ParameterNotFoundException $e) { |
|
63
|
|
|
throw InvalidConfiguration::missingParameter($e->getKey()); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
public static function router(): Router |
|
68
|
|
|
{ |
|
69
|
1 |
|
return self::get(Router::class); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
9 |
|
protected static function loadConfig(array $config): void |
|
73
|
|
|
{ |
|
74
|
9 |
|
foreach ($config as $key => $value) { |
|
75
|
9 |
|
self::$container->setParameter($key, $value); |
|
76
|
|
|
} |
|
77
|
9 |
|
} |
|
78
|
|
|
|
|
79
|
9 |
|
protected static function loadServices(string $servicesPath): void |
|
80
|
|
|
{ |
|
81
|
9 |
|
$loader = new YamlFileLoader(self::$container, new FileLocator(__DIR__)); |
|
82
|
|
|
|
|
83
|
9 |
|
$loader->load($servicesPath); |
|
84
|
|
|
|
|
85
|
|
|
/** @var Definition $definition */ |
|
86
|
9 |
|
foreach (self::$container->getDefinitions() as $id => $definition) { |
|
87
|
9 |
|
self::$container->setAlias($definition->getClass(), $id); |
|
88
|
|
|
} |
|
89
|
9 |
|
} |
|
90
|
|
|
|
|
91
|
9 |
|
protected static function loadPlugins(): void |
|
92
|
|
|
{ |
|
93
|
9 |
|
foreach (Config::plugins() as $pluginClass) { |
|
94
|
9 |
|
if (!class_implements($pluginClass, Plugin::class)) { |
|
95
|
|
|
throw InvalidPlugin::doesntImplementPluginInterface($pluginClass); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
9 |
|
self::loadPluginConfiguration($pluginClass); |
|
99
|
|
|
|
|
100
|
9 |
|
self::loadPluginServices($pluginClass); |
|
101
|
|
|
|
|
102
|
9 |
|
self::registerPluginDefinition($pluginClass); |
|
103
|
|
|
} |
|
104
|
9 |
|
} |
|
105
|
|
|
|
|
106
|
9 |
|
protected static function loadPluginConfiguration(string $pluginClass): void |
|
107
|
|
|
{ |
|
108
|
9 |
|
$configurationPath = forward_static_call([$pluginClass, 'getConfigurationPath']); |
|
109
|
|
|
|
|
110
|
9 |
|
if (!$configurationPath) { |
|
111
|
|
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
9 |
|
if (!file_exists($configurationPath)) { |
|
115
|
|
|
throw InvalidPlugin::configurationFileNotFound($pluginClass, $configurationPath); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
9 |
|
$pluginConfiguration = require $configurationPath; |
|
119
|
|
|
|
|
120
|
9 |
|
if (! \is_array($pluginConfiguration)) { |
|
121
|
|
|
throw InvalidPlugin::configurationMustBeArray($pluginClass, $configurationPath); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
9 |
|
self::loadConfig(Arr::dot($pluginConfiguration)); |
|
125
|
9 |
|
} |
|
126
|
|
|
|
|
127
|
9 |
|
protected static function loadPluginServices(string $pluginClass): void |
|
128
|
|
|
{ |
|
129
|
9 |
|
$servicesPath = forward_static_call([$pluginClass, 'getServicesPath']); |
|
130
|
|
|
|
|
131
|
9 |
|
if (!$servicesPath) { |
|
132
|
|
|
return; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
9 |
|
if (!file_exists($servicesPath)) { |
|
136
|
|
|
throw InvalidPlugin::serviceFileNotFound($pluginClass, $servicesPath); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
9 |
|
self::loadServices($servicesPath); |
|
140
|
9 |
|
} |
|
141
|
|
|
|
|
142
|
9 |
|
protected static function registerPluginDefinition(string $pluginClass): void |
|
143
|
|
|
{ |
|
144
|
9 |
|
$definition = new Definition($pluginClass); |
|
145
|
|
|
|
|
146
|
9 |
|
$definition->setAutowired(true); |
|
147
|
|
|
|
|
148
|
9 |
|
self::$container->setDefinition($pluginClass, $definition); |
|
149
|
|
|
|
|
150
|
9 |
|
forward_static_call([$pluginClass, 'boot']); |
|
151
|
9 |
|
} |
|
152
|
|
|
|
|
153
|
9 |
|
protected static function loadRoutes(): void |
|
154
|
|
|
{ |
|
155
|
9 |
|
$routeFile = File::path('src/routes.php'); |
|
156
|
|
|
|
|
157
|
9 |
|
if (! file_exists($routeFile)) { |
|
158
|
|
|
return; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
9 |
|
require_once $routeFile; |
|
162
|
9 |
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|