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\Exception\InvalidConfiguration; |
10
|
|
|
use Stitcher\Exception\InvalidPlugin; |
11
|
|
|
use Symfony\Component\Config\FileLocator; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
13
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
14
|
|
|
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; |
15
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
16
|
|
|
|
17
|
|
|
class App |
18
|
|
|
{ |
19
|
|
|
/** @var ContainerBuilder */ |
20
|
|
|
protected static $container; |
21
|
|
|
|
22
|
5 |
|
public static function init() |
23
|
|
|
{ |
24
|
5 |
|
Config::init(); |
25
|
|
|
|
26
|
5 |
|
self::$container = new ContainerBuilder(); |
27
|
|
|
|
28
|
5 |
|
self::loadConfig(Config::all()); |
29
|
|
|
|
30
|
5 |
|
self::loadServices('services.yaml'); |
31
|
|
|
|
32
|
5 |
|
self::loadPlugins(); |
33
|
5 |
|
} |
34
|
|
|
|
35
|
4 |
|
public static function get(string $id) |
36
|
|
|
{ |
37
|
4 |
|
return self::$container->get($id); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
public static function developmentServer(): DevelopmentServer |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
try { |
43
|
|
|
/** @var DevelopmentServer $server */ |
44
|
|
|
$server = self::get(DevelopmentServer::class); |
45
|
|
|
|
46
|
|
|
return $server; |
47
|
|
|
} catch (ParameterNotFoundException $e) { |
48
|
|
|
throw InvalidConfiguration::missingParameter($e->getKey()); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
public static function productionServer(): ProductionServer |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
try { |
55
|
|
|
/** @var ProductionServer $server */ |
56
|
|
|
$server = self::get(ProductionServer::class); |
57
|
|
|
|
58
|
|
|
return $server; |
59
|
|
|
} catch (ParameterNotFoundException $e) { |
60
|
|
|
throw InvalidConfiguration::missingParameter($e->getKey()); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
5 |
|
protected static function loadConfig(array $config): void |
65
|
|
|
{ |
66
|
5 |
|
foreach ($config as $key => $value) { |
67
|
5 |
|
self::$container->setParameter($key, $value); |
68
|
|
|
} |
69
|
5 |
|
} |
70
|
|
|
|
71
|
5 |
|
protected static function loadServices(string $servicesPath): void |
72
|
|
|
{ |
73
|
5 |
|
$loader = new YamlFileLoader(self::$container, new FileLocator(__DIR__)); |
74
|
|
|
|
75
|
5 |
|
$loader->load($servicesPath); |
76
|
|
|
|
77
|
|
|
/** @var Definition $definition */ |
78
|
5 |
|
foreach (self::$container->getDefinitions() as $id => $definition) { |
79
|
5 |
|
self::$container->setAlias($definition->getClass(), $id); |
80
|
|
|
} |
81
|
5 |
|
} |
82
|
|
|
|
83
|
5 |
|
protected static function loadPlugins(): void |
84
|
|
|
{ |
85
|
5 |
|
foreach (Config::plugins() as $pluginClass) { |
86
|
5 |
|
if (!class_implements($pluginClass, Plugin::class)) { |
87
|
|
|
throw InvalidPlugin::doesntImplementPluginInterface($pluginClass); |
88
|
|
|
} |
89
|
|
|
|
90
|
5 |
|
self::loadPluginConfiguration($pluginClass); |
91
|
|
|
|
92
|
5 |
|
self::loadPluginServices($pluginClass); |
93
|
|
|
|
94
|
5 |
|
self::registerPluginDefinition($pluginClass); |
95
|
|
|
} |
96
|
5 |
|
} |
97
|
|
|
|
98
|
5 |
|
protected static function loadPluginConfiguration(string $pluginClass): void |
99
|
|
|
{ |
100
|
5 |
|
$configurationPath = forward_static_call([$pluginClass, 'getConfigurationPath']); |
101
|
|
|
|
102
|
5 |
|
if (!$configurationPath) { |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
5 |
|
if (!file_exists($configurationPath)) { |
107
|
|
|
throw InvalidPlugin::configurationFileNotFound($pluginClass, $configurationPath); |
108
|
|
|
} |
109
|
|
|
|
110
|
5 |
|
$pluginConfiguration = require $configurationPath; |
111
|
|
|
|
112
|
5 |
|
if (!is_array($pluginConfiguration)) { |
113
|
|
|
throw InvalidPlugin::configurationMustBeArray($pluginClass, $configurationPath); |
114
|
|
|
} |
115
|
|
|
|
116
|
5 |
|
self::loadConfig(Arr::dot($pluginConfiguration)); |
117
|
5 |
|
} |
118
|
|
|
|
119
|
5 |
|
protected static function loadPluginServices(string $pluginClass): void |
120
|
|
|
{ |
121
|
5 |
|
$servicesPath = forward_static_call([$pluginClass, 'getServicesPath']); |
122
|
|
|
|
123
|
5 |
|
if (!$servicesPath) { |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
5 |
|
if (!file_exists($servicesPath)) { |
128
|
|
|
throw InvalidPlugin::serviceFileNotFound($pluginClass, $servicesPath); |
129
|
|
|
} |
130
|
|
|
|
131
|
5 |
|
self::loadServices($servicesPath); |
132
|
5 |
|
} |
133
|
|
|
|
134
|
5 |
|
protected static function registerPluginDefinition(string $pluginClass): void |
135
|
|
|
{ |
136
|
5 |
|
$definition = new Definition($pluginClass); |
137
|
|
|
|
138
|
5 |
|
$definition->setAutowired(true); |
139
|
|
|
|
140
|
5 |
|
self::$container->setDefinition($pluginClass, $definition); |
141
|
5 |
|
} |
142
|
|
|
} |
143
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.