1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stitcher; |
4
|
|
|
|
5
|
|
|
use Pageon\Config; |
6
|
|
|
use Stitcher\Application\DevelopmentServer; |
7
|
|
|
use Stitcher\Application\ProductionServer; |
8
|
|
|
use Stitcher\Exception\InvalidConfiguration; |
9
|
|
|
use Symfony\Component\Config\FileLocator; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
11
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
12
|
|
|
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; |
13
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
14
|
|
|
|
15
|
|
|
class App |
16
|
|
|
{ |
17
|
|
|
/** @var ContainerBuilder */ |
18
|
|
|
protected static $container; |
19
|
|
|
|
20
|
1 |
|
public static function init(string $basePath) |
21
|
|
|
{ |
22
|
1 |
|
Config::init($basePath); |
23
|
|
|
|
24
|
1 |
|
self::$container = new ContainerBuilder(); |
25
|
|
|
|
26
|
1 |
|
self::loadConfig(); |
27
|
1 |
|
self::loadServices(); |
28
|
1 |
|
} |
29
|
|
|
|
30
|
1 |
|
public static function get(string $id) |
31
|
|
|
{ |
32
|
1 |
|
return self::$container->get($id); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
View Code Duplication |
public static function developmentServer(): DevelopmentServer |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
try { |
38
|
|
|
/** @var DevelopmentServer $server */ |
39
|
|
|
$server = self::get(DevelopmentServer::class); |
40
|
|
|
|
41
|
|
|
return $server; |
42
|
|
|
} catch (ParameterNotFoundException $e) { |
43
|
|
|
throw InvalidConfiguration::missingParameter($e->getKey()); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public static function productionServer(): ProductionServer |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
try { |
50
|
|
|
/** @var ProductionServer $server */ |
51
|
|
|
$server = self::get(ProductionServer::class); |
52
|
|
|
|
53
|
|
|
return $server; |
54
|
|
|
} catch (ParameterNotFoundException $e) { |
55
|
|
|
throw InvalidConfiguration::missingParameter($e->getKey()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
protected static function loadConfig() |
60
|
|
|
{ |
61
|
1 |
|
foreach (Config::all() as $key => $value) { |
62
|
1 |
|
self::$container->setParameter($key, $value); |
63
|
|
|
} |
64
|
1 |
|
} |
65
|
|
|
|
66
|
1 |
|
protected static function loadServices() |
67
|
|
|
{ |
68
|
1 |
|
$loader = new YamlFileLoader(self::$container, new FileLocator(__DIR__)); |
69
|
1 |
|
$loader->load('services.yaml'); |
70
|
|
|
|
71
|
|
|
/** @var Definition $definition */ |
72
|
1 |
|
foreach (self::$container->getDefinitions() as $id => $definition) { |
73
|
1 |
|
self::$container->setAlias($definition->getClass(), $id); |
74
|
|
|
} |
75
|
1 |
|
} |
76
|
|
|
} |
77
|
|
|
|
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.