|
15 | 15 | use Nette\DI\ServiceCreationException; |
16 | 16 | use Nette\PhpGenerator as Php; |
17 | 17 | use Nette\Utils\Callback; |
| 18 | +use Nette\Utils\Validators; |
18 | 19 |
|
19 | 20 |
|
20 | 21 | /** |
@@ -71,7 +72,7 @@ public function getEntity(): string|array|Definition|Reference|null |
71 | 72 |
|
72 | 73 | public function resolveType(Resolver $resolver): ?string |
73 | 74 | { |
74 | | - $entity = $resolver->normalizeEntity($this); |
| 75 | + $entity = $this->normalizeEntity($resolver); |
75 | 76 |
|
76 | 77 | if ($this->arguments === Resolver::getFirstClassCallable()) { |
77 | 78 | return \Closure::class; |
@@ -137,6 +138,202 @@ interface_exists($entity) |
137 | 138 | } |
138 | 139 |
|
139 | 140 |
|
| 141 | + public function complete(Resolver $resolver): void |
| 142 | + { |
| 143 | + $entity = $this->normalizeEntity($resolver); |
| 144 | + $this->convertReferences($resolver); |
| 145 | + $arguments = $this->arguments; |
| 146 | + |
| 147 | + switch (true) { |
| 148 | + case $this->arguments === Resolver::getFirstClassCallable(): |
| 149 | + if (!is_array($entity) || !Php\Helpers::isIdentifier($entity[1])) { |
| 150 | + throw new ServiceCreationException(sprintf('Cannot create closure for %s(...)', $entity)); |
| 151 | + } |
| 152 | + if ($entity[0] instanceof self) { |
| 153 | + $entity[0]->complete($resolver); |
| 154 | + } |
| 155 | + break; |
| 156 | + |
| 157 | + case is_string($entity) && str_contains($entity, '?'): // PHP literal |
| 158 | + break; |
| 159 | + |
| 160 | + case $entity === 'not': |
| 161 | + if (count($arguments) !== 1) { |
| 162 | + throw new ServiceCreationException(sprintf('Function %s() expects 1 parameter, %s given.', $entity, count($arguments))); |
| 163 | + } |
| 164 | + |
| 165 | + $this->entity = ['', '!']; |
| 166 | + break; |
| 167 | + |
| 168 | + case $entity === 'bool': |
| 169 | + case $entity === 'int': |
| 170 | + case $entity === 'float': |
| 171 | + case $entity === 'string': |
| 172 | + if (count($arguments) !== 1) { |
| 173 | + throw new ServiceCreationException(sprintf('Function %s() expects 1 parameter, %s given.', $entity, count($arguments))); |
| 174 | + } |
| 175 | + |
| 176 | + $arguments = [$arguments[0], $entity]; |
| 177 | + $this->entity = [DI\Helpers::class, 'convertType']; |
| 178 | + break; |
| 179 | + |
| 180 | + case is_string($entity): // create class |
| 181 | + if (!class_exists($entity)) { |
| 182 | + throw new ServiceCreationException(sprintf("Class '%s' not found.", $entity)); |
| 183 | + } elseif ((new \ReflectionClass($entity))->isAbstract()) { |
| 184 | + throw new ServiceCreationException(sprintf('Class %s is abstract.', $entity)); |
| 185 | + } elseif (($rm = (new \ReflectionClass($entity))->getConstructor()) !== null && !$rm->isPublic()) { |
| 186 | + throw new ServiceCreationException(sprintf('Class %s has %s constructor.', $entity, $rm->isProtected() ? 'protected' : 'private')); |
| 187 | + } elseif ($constructor = (new \ReflectionClass($entity))->getConstructor()) { |
| 188 | + $arguments = $resolver->autowireServices($constructor, $arguments); |
| 189 | + $resolver->addDependency($constructor); |
| 190 | + } elseif ($arguments) { |
| 191 | + throw new ServiceCreationException(sprintf( |
| 192 | + 'Unable to pass arguments, class %s has no constructor.', |
| 193 | + $entity, |
| 194 | + )); |
| 195 | + } |
| 196 | + |
| 197 | + break; |
| 198 | + |
| 199 | + case $entity instanceof Reference: |
| 200 | + if ($arguments) { |
| 201 | + $e = $resolver->completeException(new ServiceCreationException(sprintf('Parameters were passed to reference @%s, although references cannot have any parameters.', $entity->getValue())), $resolver->getCurrentService()); |
| 202 | + trigger_error($e->getMessage(), E_USER_DEPRECATED); |
| 203 | + } |
| 204 | + $this->entity = [new Reference(DI\ContainerBuilder::ThisContainer), DI\Container::getMethodName($entity->getValue())]; |
| 205 | + break; |
| 206 | + |
| 207 | + case is_array($entity): |
| 208 | + if (!preg_match('#^\$?(\\\?' . Php\Helpers::ReIdentifier . ')+(\[\])?$#D', $entity[1])) { |
| 209 | + throw new ServiceCreationException(sprintf( |
| 210 | + "Expected function, method or property name, '%s' given.", |
| 211 | + $entity[1], |
| 212 | + )); |
| 213 | + } |
| 214 | + |
| 215 | + switch (true) { |
| 216 | + case $entity[0] === '': // function call |
| 217 | + if (!function_exists($entity[1])) { |
| 218 | + throw new ServiceCreationException(sprintf("Function %s doesn't exist.", $entity[1])); |
| 219 | + } |
| 220 | + |
| 221 | + $rf = new \ReflectionFunction($entity[1]); |
| 222 | + $arguments = $resolver->autowireServices($rf, $arguments); |
| 223 | + $resolver->addDependency($rf); |
| 224 | + break; |
| 225 | + |
| 226 | + case $entity[0] instanceof self: |
| 227 | + $entity[0]->complete($resolver); |
| 228 | + // break omitted |
| 229 | + |
| 230 | + case is_string($entity[0]): // static method call |
| 231 | + case $entity[0] instanceof Reference: |
| 232 | + if ($entity[1][0] === '$') { // property getter, setter or appender |
| 233 | + Validators::assert($arguments, 'list:0..1', "setup arguments for '" . Callback::toString($entity) . "'"); |
| 234 | + if (!$arguments && str_ends_with($entity[1], '[]')) { |
| 235 | + throw new ServiceCreationException(sprintf('Missing argument for %s.', $entity[1])); |
| 236 | + } |
| 237 | + } elseif ( |
| 238 | + $type = ($entity[0] instanceof Expression ? $entity[0] : new self($entity[0]))->resolveType($resolver) |
| 239 | + ) { |
| 240 | + $rc = new \ReflectionClass($type); |
| 241 | + if ($rc->hasMethod($entity[1])) { |
| 242 | + $rm = $rc->getMethod($entity[1]); |
| 243 | + if (!$rm->isPublic()) { |
| 244 | + throw new ServiceCreationException(sprintf('%s::%s() is not callable.', $type, $entity[1])); |
| 245 | + } |
| 246 | + |
| 247 | + $arguments = $resolver->autowireServices($rm, $arguments); |
| 248 | + $resolver->addDependency($rm); |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + try { |
| 255 | + $this->arguments = $this->completeArguments($resolver, $arguments); |
| 256 | + } catch (ServiceCreationException $e) { |
| 257 | + if (!str_contains($e->getMessage(), ' (used in')) { |
| 258 | + $e->setMessage($e->getMessage() . " (used in {$resolver->entityToString($entity)})"); |
| 259 | + } |
| 260 | + |
| 261 | + throw $e; |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + |
| 266 | + public function completeArguments(Resolver $resolver, array $arguments): array |
| 267 | + { |
| 268 | + array_walk_recursive($arguments, function (&$val) use ($resolver): void { |
| 269 | + if ($val instanceof self) { |
| 270 | + if ($val->entity === 'typed' || $val->entity === 'tagged') { |
| 271 | + $services = []; |
| 272 | + $current = $resolver->getCurrentService()?->getName(); |
| 273 | + foreach ($val->arguments as $argument) { |
| 274 | + foreach ($val->entity === 'tagged' ? $resolver->getContainerBuilder()->findByTag($argument) : $resolver->getContainerBuilder()->findAutowired($argument) as $name => $foo) { |
| 275 | + if ($name !== $current) { |
| 276 | + $services[] = new Reference($name); |
| 277 | + } |
| 278 | + } |
| 279 | + } |
| 280 | + |
| 281 | + $val = $this->completeArguments($resolver, $services); |
| 282 | + } else { |
| 283 | + $val->complete($resolver); |
| 284 | + } |
| 285 | + } elseif ($val instanceof Definition || $val instanceof Reference) { |
| 286 | + $val = (new self($val))->normalizeEntity($resolver); |
| 287 | + } |
| 288 | + }); |
| 289 | + return $arguments; |
| 290 | + } |
| 291 | + |
| 292 | + |
| 293 | + /** Returns literal, Class, Reference, [Class, member], [, globalFunc], [Reference, member], [Statement, member] */ |
| 294 | + private function normalizeEntity(Resolver $resolver): string|array|Reference|null |
| 295 | + { |
| 296 | + if (is_array($this->entity)) { |
| 297 | + $item = &$this->entity[0]; |
| 298 | + } else { |
| 299 | + $item = &$this->entity; |
| 300 | + } |
| 301 | + |
| 302 | + if ($item instanceof Definition) { |
| 303 | + if ($resolver->getContainerBuilder()->getDefinition($item->getName()) !== $item) { |
| 304 | + throw new ServiceCreationException(sprintf("Service '%s' does not match the expected service.", $item->getName())); |
| 305 | + |
| 306 | + } |
| 307 | + $item = new Reference($item->getName()); |
| 308 | + } |
| 309 | + |
| 310 | + if ($item instanceof Reference) { |
| 311 | + $item->complete($resolver); |
| 312 | + } |
| 313 | + |
| 314 | + return $this->entity; |
| 315 | + } |
| 316 | + |
| 317 | + |
| 318 | + private function convertReferences(Resolver $resolver): void |
| 319 | + { |
| 320 | + array_walk_recursive($this->arguments, function (&$val) use ($resolver): void { |
| 321 | + if (is_string($val) && strlen($val) > 1 && $val[0] === '@' && $val[1] !== '@') { |
| 322 | + $pair = explode('::', substr($val, 1), 2); |
| 323 | + if (!isset($pair[1])) { // @service |
| 324 | + $val = new Reference($pair[0]); |
| 325 | + } elseif (preg_match('#^[A-Z][a-zA-Z0-9_]*$#D', $pair[1])) { // @service::CONSTANT |
| 326 | + $val = DI\ContainerBuilder::literal((new Reference($pair[0]))->resolveType($resolver) . '::' . $pair[1]); |
| 327 | + } else { // @service::property |
| 328 | + $val = new self([new Reference($pair[0]), '$' . $pair[1]]); |
| 329 | + } |
| 330 | + } elseif (is_string($val) && str_starts_with($val, '@@')) { // escaped text @@ |
| 331 | + $val = substr($val, 1); |
| 332 | + } |
| 333 | + }); |
| 334 | + } |
| 335 | + |
| 336 | + |
140 | 337 | /** |
141 | 338 | * Formats PHP code for class instantiating, function calling or property setting in PHP. |
142 | 339 | */ |
|
0 commit comments