Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
86.21% |
50 / 58 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
InjectionAwareService | |
86.21% |
50 / 58 |
|
85.71% |
6 / 7 |
24.39 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__toPhpCode | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
getSerializedDependencies | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
iterateParameters | |
61.90% |
13 / 21 |
|
0.00% |
0 / 1 |
7.99 | |||
getDependencies | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
hasConfig | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isFactoryNeeded | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
6 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2020 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\tao\model\service; |
24 | |
25 | use common_Utils; |
26 | use oat\oatbox\service\ConfigurableService; |
27 | use ReflectionClass; |
28 | use ReflectionException; |
29 | use RuntimeException; |
30 | |
31 | /** |
32 | * @deprecated Use the new Dependency Injection Container |
33 | */ |
34 | abstract class InjectionAwareService extends ConfigurableService |
35 | { |
36 | /** @var bool */ |
37 | private $isChildItem = false; |
38 | |
39 | // to skip checking parent's constructor parameters inherited from Configurable::class |
40 | public function __construct() |
41 | { |
42 | parent::__construct([]); |
43 | } |
44 | |
45 | /** |
46 | * @noinspection MagicMethodsValidityInspection |
47 | * |
48 | * @throws ReflectionException |
49 | */ |
50 | public function __toPhpCode(): string |
51 | { |
52 | $content = 'new %s(%s)'; |
53 | |
54 | if (!$this->isChildItem && $this->isFactoryNeeded($this)) { |
55 | $content = <<<'FACTORY' |
56 | new class implements \oat\oatbox\service\ServiceFactoryInterface { |
57 | public function __invoke(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator) |
58 | { |
59 | return new %s(%s); |
60 | } |
61 | } |
62 | FACTORY; |
63 | } |
64 | |
65 | return sprintf( |
66 | $content, |
67 | static::class, |
68 | implode(",\n", $this->getSerializedDependencies()) |
69 | ); |
70 | } |
71 | |
72 | /** |
73 | * @return array |
74 | * |
75 | * @throws ReflectionException |
76 | */ |
77 | private function getSerializedDependencies(): array |
78 | { |
79 | return array_map( |
80 | [common_Utils::class, 'toPHPVariableString'], |
81 | $this->getDependencies() |
82 | ); |
83 | } |
84 | |
85 | /** |
86 | * @param InjectionAwareService $service |
87 | * |
88 | * @return iterable |
89 | * @throws ReflectionException |
90 | */ |
91 | protected function iterateParameters(InjectionAwareService $service): iterable |
92 | { |
93 | $class = new ReflectionClass($service); |
94 | $constructor = $class->getMethod('__construct'); |
95 | |
96 | $parameters = $constructor->getParameters(); |
97 | |
98 | foreach ($parameters as $parameter) { |
99 | $parameterName = $parameter->getName(); |
100 | |
101 | if (!$class->hasProperty($parameterName)) { |
102 | $message = sprintf( |
103 | 'Cannot find property "%s" in class %s. Please name properties exactly like constructor ' |
104 | . 'parameters, or overload %s', |
105 | $parameterName, |
106 | static::class, |
107 | __METHOD__ |
108 | ); |
109 | throw new RuntimeException($message); |
110 | } |
111 | |
112 | $classProperty = $class->getProperty($parameterName); |
113 | |
114 | if ($classProperty->isPrivate() || $classProperty->isProtected()) { |
115 | $classProperty->setAccessible(true); |
116 | } |
117 | |
118 | $value = $classProperty->getValue($service); |
119 | |
120 | $parameter->isVariadic() |
121 | ? yield from $value |
122 | : yield $value; |
123 | } |
124 | } |
125 | |
126 | /** |
127 | * @return array A list of dependencies to be injected in their order. |
128 | * @throws ReflectionException |
129 | */ |
130 | protected function getDependencies(): array |
131 | { |
132 | $dependencies = []; |
133 | |
134 | foreach ($this->iterateParameters($this) as $propertyValue) { |
135 | if ($propertyValue instanceof ConfigurableService) { |
136 | if ($this->hasConfig($propertyValue)) { |
137 | $propertyValue = new PhpCode( |
138 | sprintf('$serviceLocator->get(%s::class)', get_class($propertyValue)) |
139 | ); |
140 | } |
141 | if ($propertyValue instanceof self) { |
142 | $propertyValue->isChildItem = true; |
143 | } |
144 | } |
145 | |
146 | $dependencies[] = $propertyValue; |
147 | } |
148 | |
149 | return $dependencies; |
150 | } |
151 | |
152 | private function hasConfig(ConfigurableService $service): bool |
153 | { |
154 | $className = get_class($service); |
155 | |
156 | return defined("$className::SERVICE_ID"); |
157 | } |
158 | |
159 | /** |
160 | * @param InjectionAwareService $service |
161 | * |
162 | * @return bool |
163 | * @throws ReflectionException |
164 | */ |
165 | protected function isFactoryNeeded(InjectionAwareService $service): bool |
166 | { |
167 | foreach ($this->iterateParameters($service) as $propertyValue) { |
168 | if (!$propertyValue instanceof ConfigurableService) { |
169 | continue; |
170 | } |
171 | |
172 | if ( |
173 | !$propertyValue instanceof self |
174 | || $this->hasConfig($propertyValue) |
175 | ) { |
176 | return true; |
177 | } |
178 | |
179 | $result = $this->isFactoryNeeded($propertyValue); |
180 | if ($result) { |
181 | return true; |
182 | } |
183 | } |
184 | |
185 | return false; |
186 | } |
187 | } |