|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wonderland\Container\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
use Wonderland\Container\Configuration\Config\Fields; |
|
6
|
|
|
use Wonderland\Container\Configuration\Parser\ParserInterface; |
|
7
|
|
|
use Wonderland\Container\Configuration\Validator\ConfigurationValidator; |
|
8
|
|
|
use Wonderland\Container\Exception\InvalidConfigFormatException; |
|
9
|
|
|
use Wonderland\Container\Service\ServiceDefinition; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ServiceLoader |
|
13
|
|
|
* @package Wonderland\Container\Configuration |
|
14
|
|
|
* @author Alice Praud <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class ServiceLoader |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var ParserInterface */ |
|
19
|
|
|
private $parser; |
|
20
|
|
|
|
|
21
|
|
|
/** @var ConfigurationValidator */ |
|
22
|
|
|
private $validator; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* ServiceLoader constructor. |
|
26
|
|
|
* @param ParserInterface $parser |
|
27
|
|
|
*/ |
|
28
|
12 |
|
public function __construct(ParserInterface $parser) |
|
29
|
|
|
{ |
|
30
|
12 |
|
$this->parser = $parser; |
|
31
|
12 |
|
$this->validator = new ConfigurationValidator(); |
|
32
|
12 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $filePath |
|
36
|
|
|
* @return ServiceDefinition[] |
|
37
|
|
|
* @throws InvalidConfigFormatException |
|
38
|
|
|
*/ |
|
39
|
11 |
|
public function loadFile(string $filePath) |
|
40
|
|
|
{ |
|
41
|
11 |
|
$definitionList = $this->parser->loadFile($filePath); |
|
42
|
|
|
|
|
43
|
11 |
|
return $this->loadDefinitions($definitionList); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $directoryPath |
|
48
|
|
|
* @return ServiceDefinition[] |
|
49
|
|
|
* @throws InvalidConfigFormatException |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function loadDirectory(string $directoryPath) |
|
52
|
|
|
{ |
|
53
|
1 |
|
$definitionList = $this->parser->loadDirectory($directoryPath); |
|
54
|
|
|
|
|
55
|
1 |
|
$list = []; |
|
56
|
1 |
|
foreach ($definitionList as $item) { |
|
57
|
1 |
|
$list = array_merge($list, $this->loadDefinitions($item)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
return $list; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $definitionList |
|
65
|
|
|
* @return ServiceDefinition[] |
|
66
|
|
|
* @throws InvalidConfigFormatException |
|
67
|
|
|
*/ |
|
68
|
12 |
|
private function loadDefinitions(array $definitionList) |
|
69
|
|
|
{ |
|
70
|
12 |
|
$this->validator->validateDefinitions($definitionList); |
|
71
|
|
|
|
|
72
|
3 |
|
if (null === $definitionList[Fields::ROOT_CONFIG_NAME]) { |
|
73
|
2 |
|
return []; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
$list = []; |
|
77
|
2 |
|
foreach ($definitionList[Fields::ROOT_CONFIG_NAME] as $serviceName => $serviceConfig) { |
|
78
|
2 |
|
$list[] = $this->initServiceDefinition($serviceName, $serviceConfig); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
return $list; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $serviceName |
|
86
|
|
|
* @param array $serviceConfig |
|
87
|
|
|
* @return ServiceDefinition |
|
88
|
|
|
*/ |
|
89
|
2 |
|
private function initServiceDefinition(string $serviceName, array $serviceConfig) |
|
90
|
|
|
{ |
|
91
|
2 |
|
$definition = new ServiceDefinition( |
|
92
|
2 |
|
$serviceName, |
|
93
|
2 |
|
$this->getClass($serviceConfig), |
|
94
|
2 |
|
$this->getConstructorParams($serviceConfig), |
|
95
|
2 |
|
$this->getMethodCalls($serviceConfig) |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
2 |
|
return $definition; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param $serviceConfig |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
2 |
|
private function getClass($serviceConfig) |
|
106
|
|
|
{ |
|
107
|
2 |
|
return $serviceConfig[Fields::CLASS_CONFIG_NAME]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param $serviceConfig |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
2 |
|
private function getConstructorParams($serviceConfig) |
|
115
|
|
|
{ |
|
116
|
2 |
|
if (false === array_key_exists(Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME, $serviceConfig)) { |
|
117
|
2 |
|
return []; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
2 |
|
return $serviceConfig[Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param $serviceConfig |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
2 |
|
private function getMethodCalls($serviceConfig) |
|
128
|
|
|
{ |
|
129
|
2 |
|
if (false === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $serviceConfig)) { |
|
130
|
2 |
|
return []; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
return $serviceConfig[Fields::METHOD_CALLS_CONFIG_NAME]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|