@@ -81,59 +81,59 @@ |
||
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | /** |
| 84 | - * Resolves missing argument using autowiring. |
|
| 84 | + * Resolves missing argument using autowiring. |
|
| 85 | 85 | * @param ReflectionParameter $parameter |
| 86 | - * @param (callable(string $type, bool $single): object|object[]|null) $getter |
|
| 86 | + * @param (callable(string $type, bool $single): object|object[]|null) $getter |
|
| 87 | 87 | * |
| 88 | - * @throws ServiceCreationException |
|
| 88 | + * @throws ServiceCreationException |
|
| 89 | 89 | * |
| 90 | - * @return mixed |
|
| 91 | - */ |
|
| 92 | - private static function autowireArgument(ReflectionParameter $parameter, callable $getter) |
|
| 93 | - { |
|
| 94 | - $type = Reflection::getParameterType($parameter); |
|
| 95 | - $method = $parameter->getDeclaringFunction(); |
|
| 96 | - $desc = Reflection::toString($parameter); |
|
| 90 | + * @return mixed |
|
| 91 | + */ |
|
| 92 | + private static function autowireArgument(ReflectionParameter $parameter, callable $getter) |
|
| 93 | + { |
|
| 94 | + $type = Reflection::getParameterType($parameter); |
|
| 95 | + $method = $parameter->getDeclaringFunction(); |
|
| 96 | + $desc = Reflection::toString($parameter); |
|
| 97 | 97 | |
| 98 | - if ($type && !Reflection::isBuiltinType($type)) { |
|
| 99 | - try { |
|
| 100 | - $res = $getter($type, true); |
|
| 101 | - } catch (MissingServiceException $e) { |
|
| 102 | - $res = null; |
|
| 103 | - } catch (ServiceCreationException $e) { |
|
| 104 | - throw new ServiceCreationException("{$e->getMessage()} (needed by $desc)", 0, $e); |
|
| 105 | - } |
|
| 98 | + if ($type && !Reflection::isBuiltinType($type)) { |
|
| 99 | + try { |
|
| 100 | + $res = $getter($type, true); |
|
| 101 | + } catch (MissingServiceException $e) { |
|
| 102 | + $res = null; |
|
| 103 | + } catch (ServiceCreationException $e) { |
|
| 104 | + throw new ServiceCreationException("{$e->getMessage()} (needed by $desc)", 0, $e); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - if ($res !== null || $parameter->allowsNull()) { |
|
| 108 | - return $res; |
|
| 109 | - } elseif (class_exists($type) || interface_exists($type)) { |
|
| 110 | - throw new ServiceCreationException("Service of type $type needed by $desc not found. Did you add it to configuration file?"); |
|
| 111 | - } else { |
|
| 112 | - throw new ServiceCreationException("Class $type needed by $desc not found. Check type hint and 'use' statements."); |
|
| 113 | - } |
|
| 107 | + if ($res !== null || $parameter->allowsNull()) { |
|
| 108 | + return $res; |
|
| 109 | + } elseif (class_exists($type) || interface_exists($type)) { |
|
| 110 | + throw new ServiceCreationException("Service of type $type needed by $desc not found. Did you add it to configuration file?"); |
|
| 111 | + } else { |
|
| 112 | + throw new ServiceCreationException("Class $type needed by $desc not found. Check type hint and 'use' statements."); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - } elseif ( |
|
| 116 | - $method instanceof \ReflectionMethod |
|
| 117 | - && $type === 'array' |
|
| 118 | - && preg_match('#@param[ \t]+([\w\\\\]+)\[\][ \t]+\$' . $parameter->name . '#', (string) $method->getDocComment(), $m) |
|
| 119 | - && ($itemType = Reflection::expandClassName($m[1], $method->getDeclaringClass())) |
|
| 120 | - && (class_exists($itemType) || interface_exists($itemType)) |
|
| 121 | - ) { |
|
| 122 | - return $getter($itemType, false); |
|
| 115 | + } elseif ( |
|
| 116 | + $method instanceof \ReflectionMethod |
|
| 117 | + && $type === 'array' |
|
| 118 | + && preg_match('#@param[ \t]+([\w\\\\]+)\[\][ \t]+\$' . $parameter->name . '#', (string) $method->getDocComment(), $m) |
|
| 119 | + && ($itemType = Reflection::expandClassName($m[1], $method->getDeclaringClass())) |
|
| 120 | + && (class_exists($itemType) || interface_exists($itemType)) |
|
| 121 | + ) { |
|
| 122 | + return $getter($itemType, false); |
|
| 123 | 123 | |
| 124 | - } elseif ( |
|
| 125 | - ($type && $parameter->allowsNull()) |
|
| 126 | - || $parameter->isOptional() |
|
| 127 | - || $parameter->isDefaultValueAvailable() |
|
| 128 | - ) { |
|
| 129 | - // !optional + defaultAvailable = func($a = null, $b) since 5.4.7 |
|
| 130 | - // optional + !defaultAvailable = i.e. Exception::__construct, mysqli::mysqli, ... |
|
| 131 | - return $parameter->isDefaultValueAvailable() |
|
| 132 | - ? Reflection::getParameterDefaultValue($parameter) |
|
| 133 | - : null; |
|
| 124 | + } elseif ( |
|
| 125 | + ($type && $parameter->allowsNull()) |
|
| 126 | + || $parameter->isOptional() |
|
| 127 | + || $parameter->isDefaultValueAvailable() |
|
| 128 | + ) { |
|
| 129 | + // !optional + defaultAvailable = func($a = null, $b) since 5.4.7 |
|
| 130 | + // optional + !defaultAvailable = i.e. Exception::__construct, mysqli::mysqli, ... |
|
| 131 | + return $parameter->isDefaultValueAvailable() |
|
| 132 | + ? Reflection::getParameterDefaultValue($parameter) |
|
| 133 | + : null; |
|
| 134 | 134 | |
| 135 | - } else { |
|
| 136 | - throw new ServiceCreationException("Parameter $desc has no class type hint or default value, so its value must be specified."); |
|
| 137 | - } |
|
| 138 | - } |
|
| 135 | + } else { |
|
| 136 | + throw new ServiceCreationException("Parameter $desc has no class type hint or default value, so its value must be specified."); |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | 139 | } |
@@ -58,27 +58,3 @@ |
||
| 58 | 58 | { |
| 59 | 59 | $class->setExtends(Container::class); |
| 60 | 60 | $class->setComment(<<<'COMMENT' |
| 61 | - |
|
| 62 | -Main DependencyInjection Container. This class has been auto-generated |
|
| 63 | -by the Nette Dependency Injection Component. |
|
| 64 | - |
|
| 65 | -Automatically detects if "container" property are presented in class or uses |
|
| 66 | -global container as fallback. |
|
| 67 | -COMMENT); |
|
| 68 | - |
|
| 69 | - return parent::toString($class); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - public function generateMethod(Definitions\Definition $def): Nette\PhpGenerator\Method |
|
| 73 | - { |
|
| 74 | - $method = parent::generateMethod($def); |
|
| 75 | - $name = $def->getName(); |
|
| 76 | - $comment = 'This service can be accessed by it\'s name in lower case,'; |
|
| 77 | - $comment2 = "thus `%s`, using container get or make methods.\n\n@return %s"; |
|
| 78 | - |
|
| 79 | - $method->setProtected(); |
|
| 80 | - $method->setComment(\sprintf($comment . "\n" . $comment2, $name, $def->getType())); |
|
| 81 | - |
|
| 82 | - return $method; |
|
| 83 | - } |
|
| 84 | -} |
|
@@ -58,27 +58,3 @@ |
||
| 58 | 58 | { |
| 59 | 59 | $class->setExtends(Container::class); |
| 60 | 60 | $class->setComment(<<<'COMMENT' |
| 61 | - |
|
| 62 | -Main DependencyInjection Container. This class has been auto-generated |
|
| 63 | -by the Nette Dependency Injection Component. |
|
| 64 | - |
|
| 65 | -Automatically detects if "container" property are presented in class or uses |
|
| 66 | -global container as fallback. |
|
| 67 | -COMMENT); |
|
| 68 | - |
|
| 69 | - return parent::toString($class); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - public function generateMethod(Definitions\Definition $def): Nette\PhpGenerator\Method |
|
| 73 | - { |
|
| 74 | - $method = parent::generateMethod($def); |
|
| 75 | - $name = $def->getName(); |
|
| 76 | - $comment = 'This service can be accessed by it\'s name in lower case,'; |
|
| 77 | - $comment2 = "thus `%s`, using container get or make methods.\n\n@return %s"; |
|
| 78 | - |
|
| 79 | - $method->setProtected(); |
|
| 80 | - $method->setComment(\sprintf($comment . "\n" . $comment2, $name, $def->getType())); |
|
| 81 | - |
|
| 82 | - return $method; |
|
| 83 | - } |
|
| 84 | -} |
|