@@ -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 | } |