@@ -10,10 +10,10 @@ |
||
10 | 10 | interface ServiceInstanceInterface |
11 | 11 | { |
12 | 12 | |
13 | - /** @return string */ |
|
14 | - public function getServiceName(); |
|
13 | + /** @return string */ |
|
14 | + public function getServiceName(); |
|
15 | 15 | |
16 | - /** @return mixed */ |
|
17 | - public function getInstance(); |
|
16 | + /** @return mixed */ |
|
17 | + public function getInstance(); |
|
18 | 18 | |
19 | 19 | } |
@@ -10,16 +10,16 @@ |
||
10 | 10 | interface ServiceDefinitionInterface |
11 | 11 | { |
12 | 12 | |
13 | - /** @return string */ |
|
14 | - public function getServiceName(); |
|
13 | + /** @return string */ |
|
14 | + public function getServiceName(); |
|
15 | 15 | |
16 | - /** @return string */ |
|
17 | - public function getClass(); |
|
16 | + /** @return string */ |
|
17 | + public function getClass(); |
|
18 | 18 | |
19 | - /** @return array */ |
|
20 | - public function getConstructArgs(); |
|
19 | + /** @return array */ |
|
20 | + public function getConstructArgs(); |
|
21 | 21 | |
22 | - /** @return array */ |
|
23 | - public function getCalls(); |
|
22 | + /** @return array */ |
|
23 | + public function getCalls(); |
|
24 | 24 | |
25 | 25 | } |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | declare( strict_types = 1 ); |
3 | 3 | |
4 | 4 | if ( PHP_SAPI !== 'cli' ) { |
5 | - die( 'Not an entry point' ); |
|
5 | + die( 'Not an entry point' ); |
|
6 | 6 | } |
7 | 7 | |
8 | 8 | error_reporting( -1 ); |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | ini_set( 'display_errors', '1' ); |
11 | 11 | |
12 | 12 | if ( !is_readable( __DIR__ . '/../vendor/autoload.php' ) ) { |
13 | - die( 'You need to install this package with Composer before you can run the tests' ); |
|
13 | + die( 'You need to install this package with Composer before you can run the tests' ); |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | require_once __DIR__ . '/../vendor/autoload.php'; |
@@ -1,16 +1,16 @@ |
||
1 | 1 | <?php |
2 | -declare( strict_types = 1 ); |
|
2 | +declare(strict_types=1); |
|
3 | 3 | |
4 | -if ( PHP_SAPI !== 'cli' ) { |
|
5 | - die( 'Not an entry point' ); |
|
4 | +if (PHP_SAPI !== 'cli') { |
|
5 | + die('Not an entry point'); |
|
6 | 6 | } |
7 | 7 | |
8 | 8 | error_reporting( -1 ); |
9 | 9 | |
10 | -ini_set( 'display_errors', '1' ); |
|
10 | +ini_set('display_errors', '1'); |
|
11 | 11 | |
12 | -if ( !is_readable( __DIR__ . '/../vendor/autoload.php' ) ) { |
|
13 | - die( 'You need to install this package with Composer before you can run the tests' ); |
|
12 | +if (!is_readable(__DIR__ . '/../vendor/autoload.php')) { |
|
13 | + die('You need to install this package with Composer before you can run the tests'); |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | require_once __DIR__ . '/../vendor/autoload.php'; |
@@ -9,62 +9,62 @@ |
||
9 | 9 | */ |
10 | 10 | class ServiceDefinition implements ServiceDefinitionInterface |
11 | 11 | { |
12 | - /** @var string */ |
|
13 | - private $serviceName; |
|
12 | + /** @var string */ |
|
13 | + private $serviceName; |
|
14 | 14 | |
15 | - /** @var string */ |
|
16 | - private $class; |
|
15 | + /** @var string */ |
|
16 | + private $class; |
|
17 | 17 | |
18 | - /** @var array */ |
|
19 | - private $constructArgs; |
|
18 | + /** @var array */ |
|
19 | + private $constructArgs; |
|
20 | 20 | |
21 | - /** @var array */ |
|
22 | - private $calls; |
|
21 | + /** @var array */ |
|
22 | + private $calls; |
|
23 | 23 | |
24 | - /** |
|
25 | - * RepositoryFactory constructor. |
|
26 | - * |
|
27 | - * @param string $serviceName |
|
28 | - * @param string $class |
|
29 | - * @param array $args |
|
30 | - * @param array $calls |
|
31 | - */ |
|
32 | - public function __construct(string $serviceName, string $class, array $args = [], array $calls = []) |
|
33 | - { |
|
34 | - $this->serviceName = $serviceName; |
|
35 | - $this->class = $class; |
|
36 | - $this->constructArgs = $args; |
|
37 | - $this->calls = $calls; |
|
38 | - } |
|
24 | + /** |
|
25 | + * RepositoryFactory constructor. |
|
26 | + * |
|
27 | + * @param string $serviceName |
|
28 | + * @param string $class |
|
29 | + * @param array $args |
|
30 | + * @param array $calls |
|
31 | + */ |
|
32 | + public function __construct(string $serviceName, string $class, array $args = [], array $calls = []) |
|
33 | + { |
|
34 | + $this->serviceName = $serviceName; |
|
35 | + $this->class = $class; |
|
36 | + $this->constructArgs = $args; |
|
37 | + $this->calls = $calls; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @return string |
|
42 | - */ |
|
43 | - public function getClass() |
|
44 | - { |
|
45 | - return $this->class; |
|
46 | - } |
|
40 | + /** |
|
41 | + * @return string |
|
42 | + */ |
|
43 | + public function getClass() |
|
44 | + { |
|
45 | + return $this->class; |
|
46 | + } |
|
47 | 47 | |
48 | - /** @return string */ |
|
49 | - public function getServiceName() |
|
50 | - { |
|
51 | - return $this->serviceName; |
|
52 | - } |
|
48 | + /** @return string */ |
|
49 | + public function getServiceName() |
|
50 | + { |
|
51 | + return $this->serviceName; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * @return array |
|
56 | - */ |
|
57 | - public function getConstructArgs() |
|
58 | - { |
|
59 | - return $this->constructArgs; |
|
60 | - } |
|
54 | + /** |
|
55 | + * @return array |
|
56 | + */ |
|
57 | + public function getConstructArgs() |
|
58 | + { |
|
59 | + return $this->constructArgs; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @return array |
|
64 | - */ |
|
65 | - public function getCalls() |
|
66 | - { |
|
67 | - return $this->calls; |
|
68 | - } |
|
62 | + /** |
|
63 | + * @return array |
|
64 | + */ |
|
65 | + public function getCalls() |
|
66 | + { |
|
67 | + return $this->calls; |
|
68 | + } |
|
69 | 69 | |
70 | 70 | } |
@@ -9,8 +9,8 @@ |
||
9 | 9 | */ |
10 | 10 | final class Fields |
11 | 11 | { |
12 | - const ROOT_CONFIG_NAME = 'services'; |
|
13 | - const CLASS_CONFIG_NAME = 'class'; |
|
14 | - const CONSTRUCTOR_PARAMS_CONFIG_NAME = 'arguments'; |
|
15 | - const METHOD_CALLS_CONFIG_NAME = 'calls'; |
|
12 | + const ROOT_CONFIG_NAME = 'services'; |
|
13 | + const CLASS_CONFIG_NAME = 'class'; |
|
14 | + const CONSTRUCTOR_PARAMS_CONFIG_NAME = 'arguments'; |
|
15 | + const METHOD_CALLS_CONFIG_NAME = 'calls'; |
|
16 | 16 | } |
@@ -10,16 +10,16 @@ |
||
10 | 10 | interface ParserInterface |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * @param string $filePath |
|
15 | - * @return array |
|
16 | - */ |
|
17 | - public function loadFile(string $filePath): array; |
|
13 | + /** |
|
14 | + * @param string $filePath |
|
15 | + * @return array |
|
16 | + */ |
|
17 | + public function loadFile(string $filePath): array; |
|
18 | 18 | |
19 | - /** |
|
20 | - * @param string $directorPath |
|
21 | - * @return array |
|
22 | - */ |
|
23 | - public function loadDirectory(string $directorPath): array; |
|
19 | + /** |
|
20 | + * @param string $directorPath |
|
21 | + * @return array |
|
22 | + */ |
|
23 | + public function loadDirectory(string $directorPath): array; |
|
24 | 24 | |
25 | 25 | } |
@@ -15,122 +15,122 @@ |
||
15 | 15 | */ |
16 | 16 | class ServiceLoader |
17 | 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 | - public function __construct(ParserInterface $parser) |
|
29 | - { |
|
30 | - $this->parser = $parser; |
|
31 | - $this->validator = new ConfigurationValidator(); |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * @param string $filePath |
|
36 | - * @return ServiceDefinition[] |
|
37 | - * @throws InvalidConfigFormatException |
|
38 | - */ |
|
39 | - public function loadFile(string $filePath) |
|
40 | - { |
|
41 | - $definitionList = $this->parser->loadFile($filePath); |
|
42 | - |
|
43 | - return $this->loadDefinitions($definitionList); |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * @param string $directoryPath |
|
48 | - * @return ServiceDefinition[] |
|
49 | - * @throws InvalidConfigFormatException |
|
50 | - */ |
|
51 | - public function loadDirectory(string $directoryPath) |
|
52 | - { |
|
53 | - $definitionList = $this->parser->loadDirectory($directoryPath); |
|
54 | - |
|
55 | - $list = []; |
|
56 | - foreach ($definitionList as $item) { |
|
57 | - $list = array_merge($list, $this->loadDefinitions($item)); |
|
58 | - } |
|
59 | - |
|
60 | - return $list; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @param array $definitionList |
|
65 | - * @return ServiceDefinition[] |
|
66 | - * @throws InvalidConfigFormatException |
|
67 | - */ |
|
68 | - private function loadDefinitions(array $definitionList) |
|
69 | - { |
|
70 | - $this->validator->validateDefinitions($definitionList); |
|
71 | - |
|
72 | - if (null === $definitionList[Fields::ROOT_CONFIG_NAME]) { |
|
73 | - return []; |
|
74 | - } |
|
75 | - |
|
76 | - $list = []; |
|
77 | - foreach ($definitionList[Fields::ROOT_CONFIG_NAME] as $serviceName => $serviceConfig) { |
|
78 | - $list[] = $this->initServiceDefinition($serviceName, $serviceConfig); |
|
79 | - } |
|
80 | - |
|
81 | - return $list; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * @param string $serviceName |
|
86 | - * @param array $serviceConfig |
|
87 | - * @return ServiceDefinition |
|
88 | - */ |
|
89 | - private function initServiceDefinition(string $serviceName, array $serviceConfig) |
|
90 | - { |
|
91 | - $definition = new ServiceDefinition( |
|
92 | - $serviceName, |
|
93 | - $this->getClass($serviceConfig), |
|
94 | - $this->getConstructorParams($serviceConfig), |
|
95 | - $this->getMethodCalls($serviceConfig) |
|
96 | - ); |
|
97 | - |
|
98 | - return $definition; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @param $serviceConfig |
|
103 | - * @return string |
|
104 | - */ |
|
105 | - private function getClass($serviceConfig) |
|
106 | - { |
|
107 | - return $serviceConfig[Fields::CLASS_CONFIG_NAME]; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @param $serviceConfig |
|
112 | - * @return array |
|
113 | - */ |
|
114 | - private function getConstructorParams($serviceConfig) |
|
115 | - { |
|
116 | - if (false === array_key_exists(Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME, $serviceConfig)) { |
|
117 | - return []; |
|
118 | - } |
|
119 | - |
|
120 | - return $serviceConfig[Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME]; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * @param $serviceConfig |
|
125 | - * @return array |
|
126 | - */ |
|
127 | - private function getMethodCalls($serviceConfig) |
|
128 | - { |
|
129 | - if (false === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $serviceConfig)) { |
|
130 | - return []; |
|
131 | - } |
|
132 | - |
|
133 | - return $serviceConfig[Fields::METHOD_CALLS_CONFIG_NAME]; |
|
134 | - } |
|
18 | + /** @var ParserInterface */ |
|
19 | + private $parser; |
|
20 | + |
|
21 | + /** @var ConfigurationValidator */ |
|
22 | + private $validator; |
|
23 | + |
|
24 | + /** |
|
25 | + * ServiceLoader constructor. |
|
26 | + * @param ParserInterface $parser |
|
27 | + */ |
|
28 | + public function __construct(ParserInterface $parser) |
|
29 | + { |
|
30 | + $this->parser = $parser; |
|
31 | + $this->validator = new ConfigurationValidator(); |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * @param string $filePath |
|
36 | + * @return ServiceDefinition[] |
|
37 | + * @throws InvalidConfigFormatException |
|
38 | + */ |
|
39 | + public function loadFile(string $filePath) |
|
40 | + { |
|
41 | + $definitionList = $this->parser->loadFile($filePath); |
|
42 | + |
|
43 | + return $this->loadDefinitions($definitionList); |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * @param string $directoryPath |
|
48 | + * @return ServiceDefinition[] |
|
49 | + * @throws InvalidConfigFormatException |
|
50 | + */ |
|
51 | + public function loadDirectory(string $directoryPath) |
|
52 | + { |
|
53 | + $definitionList = $this->parser->loadDirectory($directoryPath); |
|
54 | + |
|
55 | + $list = []; |
|
56 | + foreach ($definitionList as $item) { |
|
57 | + $list = array_merge($list, $this->loadDefinitions($item)); |
|
58 | + } |
|
59 | + |
|
60 | + return $list; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @param array $definitionList |
|
65 | + * @return ServiceDefinition[] |
|
66 | + * @throws InvalidConfigFormatException |
|
67 | + */ |
|
68 | + private function loadDefinitions(array $definitionList) |
|
69 | + { |
|
70 | + $this->validator->validateDefinitions($definitionList); |
|
71 | + |
|
72 | + if (null === $definitionList[Fields::ROOT_CONFIG_NAME]) { |
|
73 | + return []; |
|
74 | + } |
|
75 | + |
|
76 | + $list = []; |
|
77 | + foreach ($definitionList[Fields::ROOT_CONFIG_NAME] as $serviceName => $serviceConfig) { |
|
78 | + $list[] = $this->initServiceDefinition($serviceName, $serviceConfig); |
|
79 | + } |
|
80 | + |
|
81 | + return $list; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * @param string $serviceName |
|
86 | + * @param array $serviceConfig |
|
87 | + * @return ServiceDefinition |
|
88 | + */ |
|
89 | + private function initServiceDefinition(string $serviceName, array $serviceConfig) |
|
90 | + { |
|
91 | + $definition = new ServiceDefinition( |
|
92 | + $serviceName, |
|
93 | + $this->getClass($serviceConfig), |
|
94 | + $this->getConstructorParams($serviceConfig), |
|
95 | + $this->getMethodCalls($serviceConfig) |
|
96 | + ); |
|
97 | + |
|
98 | + return $definition; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @param $serviceConfig |
|
103 | + * @return string |
|
104 | + */ |
|
105 | + private function getClass($serviceConfig) |
|
106 | + { |
|
107 | + return $serviceConfig[Fields::CLASS_CONFIG_NAME]; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @param $serviceConfig |
|
112 | + * @return array |
|
113 | + */ |
|
114 | + private function getConstructorParams($serviceConfig) |
|
115 | + { |
|
116 | + if (false === array_key_exists(Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME, $serviceConfig)) { |
|
117 | + return []; |
|
118 | + } |
|
119 | + |
|
120 | + return $serviceConfig[Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME]; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * @param $serviceConfig |
|
125 | + * @return array |
|
126 | + */ |
|
127 | + private function getMethodCalls($serviceConfig) |
|
128 | + { |
|
129 | + if (false === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $serviceConfig)) { |
|
130 | + return []; |
|
131 | + } |
|
132 | + |
|
133 | + return $serviceConfig[Fields::METHOD_CALLS_CONFIG_NAME]; |
|
134 | + } |
|
135 | 135 | |
136 | 136 | } |
@@ -14,212 +14,212 @@ |
||
14 | 14 | */ |
15 | 15 | class ServiceContainer implements ContainerInterface |
16 | 16 | { |
17 | - private const SERVICE_PREFIX = '@'; |
|
18 | - |
|
19 | - /** @var ServiceDefinitionInterface[] */ |
|
20 | - private $services; |
|
21 | - |
|
22 | - /** @var array */ |
|
23 | - private $serviceInstances; |
|
24 | - |
|
25 | - /** |
|
26 | - * ServiceContainer constructor. |
|
27 | - */ |
|
28 | - public function __construct() |
|
29 | - { |
|
30 | - $this->services = []; |
|
31 | - $this->serviceInstances = []; |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * @param ServiceDefinitionInterface $serviceDefinition |
|
36 | - * @return ServiceContainer |
|
37 | - * @throws DuplicatedServiceException |
|
38 | - */ |
|
39 | - public function addService(ServiceDefinitionInterface $serviceDefinition) |
|
40 | - { |
|
41 | - if (true === $this->has($serviceDefinition->getServiceName())) { |
|
42 | - throw new DuplicatedServiceException( |
|
43 | - 'The service "' . $serviceDefinition->getServiceName() . '" is already registered in the container' |
|
44 | - ); |
|
45 | - } |
|
46 | - |
|
47 | - $this->services[$serviceDefinition->getServiceName()] = $serviceDefinition; |
|
48 | - |
|
49 | - return $this; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @param ServiceDefinitionInterface[] $definitionList |
|
54 | - * @return ServiceContainer |
|
55 | - * @throws DuplicatedServiceException |
|
56 | - */ |
|
57 | - public function loadServices(array $definitionList) |
|
58 | - { |
|
59 | - foreach ($definitionList as $definition) { |
|
60 | - $this->addService($definition); |
|
61 | - } |
|
62 | - |
|
63 | - return $this; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * @param InstanceDefinitionInterface $definition |
|
68 | - * @return ServiceContainer |
|
69 | - * @throws DuplicatedServiceException |
|
70 | - */ |
|
71 | - public function addServiceInstance(InstanceDefinitionInterface $definition) |
|
72 | - { |
|
73 | - if (true === $this->has($definition->getServiceName())) { |
|
74 | - throw new DuplicatedServiceException( |
|
75 | - 'The service "' . $definition->getServiceName() . '" is already registered in the container' |
|
76 | - ); |
|
77 | - } |
|
78 | - |
|
79 | - $this->serviceInstances[$definition->getServiceName()] = $definition->getInstance(); |
|
80 | - |
|
81 | - return $this; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * @param string $index |
|
86 | - * @param bool $new |
|
87 | - * @return mixed|null |
|
88 | - */ |
|
89 | - public function get($index, $new = false) |
|
90 | - { |
|
91 | - $index = $this->cleanServiceName($index); |
|
92 | - |
|
93 | - // if service is shared true and already exists we return it |
|
94 | - if (isset($this->serviceInstances[$index]) && false === $new) { |
|
95 | - return $this->serviceInstances[$index]; |
|
96 | - } |
|
97 | - |
|
98 | - // is service not defined return null |
|
99 | - if (!isset($this->services[$index])) { |
|
100 | - return null; |
|
101 | - } |
|
102 | - |
|
103 | - // we create a new instance |
|
104 | - $instance = $this->create($this->services[$index]); |
|
105 | - |
|
106 | - // if shared true we set it in the current instances |
|
107 | - if (false === $new) { |
|
108 | - $this->serviceInstances[$index] = $instance; |
|
109 | - } |
|
110 | - |
|
111 | - return $instance; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Returns true if the container can return an entry for the given identifier. |
|
116 | - * Returns false otherwise. |
|
117 | - * |
|
118 | - * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
|
119 | - * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
|
120 | - * |
|
121 | - * @param string $index Identifier of the entry to look for. |
|
122 | - * |
|
123 | - * @return bool |
|
124 | - */ |
|
125 | - public function has($index) |
|
126 | - { |
|
127 | - $index = $this->cleanServiceName($index); |
|
128 | - |
|
129 | - // if service is shared true and already exists we return it |
|
130 | - if (isset($this->serviceInstances[$index])) { |
|
131 | - return true; |
|
132 | - } |
|
133 | - |
|
134 | - // is service not defined return null |
|
135 | - if (isset($this->services[$index])) { |
|
136 | - return true; |
|
137 | - } |
|
138 | - |
|
139 | - return false; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @param ServiceDefinitionInterface $serviceDefinition |
|
144 | - * @return mixed |
|
145 | - */ |
|
146 | - private function create(ServiceDefinitionInterface $serviceDefinition) |
|
147 | - { |
|
148 | - // we get the class name in a var for the new later |
|
149 | - $newClass = $serviceDefinition->getClass(); |
|
150 | - |
|
151 | - // we get the construct args |
|
152 | - $args = $this->checkArgsServices($serviceDefinition->getConstructArgs()); |
|
153 | - $instance = new $newClass(...$args); |
|
154 | - |
|
155 | - // we call the injection methods after we instance the object |
|
156 | - $calls = $this->checkCallsServices($serviceDefinition->getCalls()); |
|
157 | - foreach ($calls as $call => $params) { |
|
158 | - call_user_func_array([$instance, $call], $params); |
|
159 | - } |
|
160 | - |
|
161 | - // we create the new instance |
|
162 | - return $instance; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * @param array $args |
|
167 | - * @return array |
|
168 | - */ |
|
169 | - private function checkArgsServices(array $args) |
|
170 | - { |
|
171 | - // we check if any of the construct args are services themself and we create them |
|
172 | - foreach ($args as $k => $arg) { |
|
173 | - if (false === $this->isServiceName($arg)) { |
|
174 | - continue; |
|
175 | - } |
|
176 | - |
|
177 | - if (true === $this->has($arg)) { |
|
178 | - $args[$k] = $this->get($arg); |
|
179 | - } |
|
180 | - } |
|
181 | - |
|
182 | - return $args; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * @param array $calls |
|
187 | - * @return array |
|
188 | - */ |
|
189 | - private function checkCallsServices(array $calls) |
|
190 | - { |
|
191 | - // we check if any of the calls args are services themself and we create them |
|
192 | - foreach ($calls as $i => $callArgs) { |
|
193 | - foreach ($callArgs as $k => $arg) { |
|
194 | - if (false === $this->isServiceName($arg)) { |
|
195 | - continue; |
|
196 | - } |
|
197 | - |
|
198 | - if (true === $this->has($arg)) { |
|
199 | - $calls[$i][$k] = $this->get($arg); |
|
200 | - } |
|
201 | - } |
|
202 | - } |
|
203 | - |
|
204 | - return $calls; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * @param string $str |
|
209 | - * @return bool |
|
210 | - */ |
|
211 | - private function isServiceName(string $str) |
|
212 | - { |
|
213 | - return self::SERVICE_PREFIX === substr($str, 0, 1); |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * @param string $str |
|
218 | - * @return string |
|
219 | - */ |
|
220 | - private function cleanServiceName(string $str) |
|
221 | - { |
|
222 | - return trim($str, self::SERVICE_PREFIX); |
|
223 | - } |
|
17 | + private const SERVICE_PREFIX = '@'; |
|
18 | + |
|
19 | + /** @var ServiceDefinitionInterface[] */ |
|
20 | + private $services; |
|
21 | + |
|
22 | + /** @var array */ |
|
23 | + private $serviceInstances; |
|
24 | + |
|
25 | + /** |
|
26 | + * ServiceContainer constructor. |
|
27 | + */ |
|
28 | + public function __construct() |
|
29 | + { |
|
30 | + $this->services = []; |
|
31 | + $this->serviceInstances = []; |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * @param ServiceDefinitionInterface $serviceDefinition |
|
36 | + * @return ServiceContainer |
|
37 | + * @throws DuplicatedServiceException |
|
38 | + */ |
|
39 | + public function addService(ServiceDefinitionInterface $serviceDefinition) |
|
40 | + { |
|
41 | + if (true === $this->has($serviceDefinition->getServiceName())) { |
|
42 | + throw new DuplicatedServiceException( |
|
43 | + 'The service "' . $serviceDefinition->getServiceName() . '" is already registered in the container' |
|
44 | + ); |
|
45 | + } |
|
46 | + |
|
47 | + $this->services[$serviceDefinition->getServiceName()] = $serviceDefinition; |
|
48 | + |
|
49 | + return $this; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @param ServiceDefinitionInterface[] $definitionList |
|
54 | + * @return ServiceContainer |
|
55 | + * @throws DuplicatedServiceException |
|
56 | + */ |
|
57 | + public function loadServices(array $definitionList) |
|
58 | + { |
|
59 | + foreach ($definitionList as $definition) { |
|
60 | + $this->addService($definition); |
|
61 | + } |
|
62 | + |
|
63 | + return $this; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * @param InstanceDefinitionInterface $definition |
|
68 | + * @return ServiceContainer |
|
69 | + * @throws DuplicatedServiceException |
|
70 | + */ |
|
71 | + public function addServiceInstance(InstanceDefinitionInterface $definition) |
|
72 | + { |
|
73 | + if (true === $this->has($definition->getServiceName())) { |
|
74 | + throw new DuplicatedServiceException( |
|
75 | + 'The service "' . $definition->getServiceName() . '" is already registered in the container' |
|
76 | + ); |
|
77 | + } |
|
78 | + |
|
79 | + $this->serviceInstances[$definition->getServiceName()] = $definition->getInstance(); |
|
80 | + |
|
81 | + return $this; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * @param string $index |
|
86 | + * @param bool $new |
|
87 | + * @return mixed|null |
|
88 | + */ |
|
89 | + public function get($index, $new = false) |
|
90 | + { |
|
91 | + $index = $this->cleanServiceName($index); |
|
92 | + |
|
93 | + // if service is shared true and already exists we return it |
|
94 | + if (isset($this->serviceInstances[$index]) && false === $new) { |
|
95 | + return $this->serviceInstances[$index]; |
|
96 | + } |
|
97 | + |
|
98 | + // is service not defined return null |
|
99 | + if (!isset($this->services[$index])) { |
|
100 | + return null; |
|
101 | + } |
|
102 | + |
|
103 | + // we create a new instance |
|
104 | + $instance = $this->create($this->services[$index]); |
|
105 | + |
|
106 | + // if shared true we set it in the current instances |
|
107 | + if (false === $new) { |
|
108 | + $this->serviceInstances[$index] = $instance; |
|
109 | + } |
|
110 | + |
|
111 | + return $instance; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Returns true if the container can return an entry for the given identifier. |
|
116 | + * Returns false otherwise. |
|
117 | + * |
|
118 | + * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
|
119 | + * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
|
120 | + * |
|
121 | + * @param string $index Identifier of the entry to look for. |
|
122 | + * |
|
123 | + * @return bool |
|
124 | + */ |
|
125 | + public function has($index) |
|
126 | + { |
|
127 | + $index = $this->cleanServiceName($index); |
|
128 | + |
|
129 | + // if service is shared true and already exists we return it |
|
130 | + if (isset($this->serviceInstances[$index])) { |
|
131 | + return true; |
|
132 | + } |
|
133 | + |
|
134 | + // is service not defined return null |
|
135 | + if (isset($this->services[$index])) { |
|
136 | + return true; |
|
137 | + } |
|
138 | + |
|
139 | + return false; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @param ServiceDefinitionInterface $serviceDefinition |
|
144 | + * @return mixed |
|
145 | + */ |
|
146 | + private function create(ServiceDefinitionInterface $serviceDefinition) |
|
147 | + { |
|
148 | + // we get the class name in a var for the new later |
|
149 | + $newClass = $serviceDefinition->getClass(); |
|
150 | + |
|
151 | + // we get the construct args |
|
152 | + $args = $this->checkArgsServices($serviceDefinition->getConstructArgs()); |
|
153 | + $instance = new $newClass(...$args); |
|
154 | + |
|
155 | + // we call the injection methods after we instance the object |
|
156 | + $calls = $this->checkCallsServices($serviceDefinition->getCalls()); |
|
157 | + foreach ($calls as $call => $params) { |
|
158 | + call_user_func_array([$instance, $call], $params); |
|
159 | + } |
|
160 | + |
|
161 | + // we create the new instance |
|
162 | + return $instance; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * @param array $args |
|
167 | + * @return array |
|
168 | + */ |
|
169 | + private function checkArgsServices(array $args) |
|
170 | + { |
|
171 | + // we check if any of the construct args are services themself and we create them |
|
172 | + foreach ($args as $k => $arg) { |
|
173 | + if (false === $this->isServiceName($arg)) { |
|
174 | + continue; |
|
175 | + } |
|
176 | + |
|
177 | + if (true === $this->has($arg)) { |
|
178 | + $args[$k] = $this->get($arg); |
|
179 | + } |
|
180 | + } |
|
181 | + |
|
182 | + return $args; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * @param array $calls |
|
187 | + * @return array |
|
188 | + */ |
|
189 | + private function checkCallsServices(array $calls) |
|
190 | + { |
|
191 | + // we check if any of the calls args are services themself and we create them |
|
192 | + foreach ($calls as $i => $callArgs) { |
|
193 | + foreach ($callArgs as $k => $arg) { |
|
194 | + if (false === $this->isServiceName($arg)) { |
|
195 | + continue; |
|
196 | + } |
|
197 | + |
|
198 | + if (true === $this->has($arg)) { |
|
199 | + $calls[$i][$k] = $this->get($arg); |
|
200 | + } |
|
201 | + } |
|
202 | + } |
|
203 | + |
|
204 | + return $calls; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * @param string $str |
|
209 | + * @return bool |
|
210 | + */ |
|
211 | + private function isServiceName(string $str) |
|
212 | + { |
|
213 | + return self::SERVICE_PREFIX === substr($str, 0, 1); |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * @param string $str |
|
218 | + * @return string |
|
219 | + */ |
|
220 | + private function cleanServiceName(string $str) |
|
221 | + { |
|
222 | + return trim($str, self::SERVICE_PREFIX); |
|
223 | + } |
|
224 | 224 | |
225 | 225 | } |
@@ -9,28 +9,28 @@ |
||
9 | 9 | */ |
10 | 10 | class InstanceDefinition implements InstanceDefinitionInterface |
11 | 11 | { |
12 | - /** @var string */ |
|
13 | - private $serviceName; |
|
12 | + /** @var string */ |
|
13 | + private $serviceName; |
|
14 | 14 | |
15 | - /** @var mixed */ |
|
16 | - private $instance; |
|
15 | + /** @var mixed */ |
|
16 | + private $instance; |
|
17 | 17 | |
18 | - public function __construct(string $serviceName, $instance) |
|
19 | - { |
|
20 | - $this->serviceName = $serviceName; |
|
21 | - $this->instance = $instance; |
|
22 | - } |
|
18 | + public function __construct(string $serviceName, $instance) |
|
19 | + { |
|
20 | + $this->serviceName = $serviceName; |
|
21 | + $this->instance = $instance; |
|
22 | + } |
|
23 | 23 | |
24 | - /** @return string */ |
|
25 | - public function getServiceName() |
|
26 | - { |
|
27 | - return $this->serviceName; |
|
28 | - } |
|
24 | + /** @return string */ |
|
25 | + public function getServiceName() |
|
26 | + { |
|
27 | + return $this->serviceName; |
|
28 | + } |
|
29 | 29 | |
30 | - /** @return mixed */ |
|
31 | - public function getInstance() |
|
32 | - { |
|
33 | - return $this->instance; |
|
34 | - } |
|
30 | + /** @return mixed */ |
|
31 | + public function getInstance() |
|
32 | + { |
|
33 | + return $this->instance; |
|
34 | + } |
|
35 | 35 | |
36 | 36 | } |