function AbstractRecursivePass::getConstructor
Throws
6 calls to AbstractRecursivePass::getConstructor()
- AbstractRecursivePass::getReflectionMethod in vendor/
symfony/ dependency-injection/ Compiler/ AbstractRecursivePass.php - AttributeAutoconfigurationPass::processValue in vendor/
symfony/ dependency-injection/ Compiler/ AttributeAutoconfigurationPass.php - Processes a value found in a definition tree.
- AutowirePass::doProcessValue in vendor/
symfony/ dependency-injection/ Compiler/ AutowirePass.php - CheckTypeDeclarationsPass::processValue in vendor/
symfony/ dependency-injection/ Compiler/ CheckTypeDeclarationsPass.php - Processes a value found in a definition tree.
- ResolveAutowireInlineAttributesPass::processValue in vendor/
symfony/ dependency-injection/ Compiler/ ResolveAutowireInlineAttributesPass.php - Processes a value found in a definition tree.
File
-
vendor/
symfony/ dependency-injection/ Compiler/ AbstractRecursivePass.php, line 121
Class
- AbstractRecursivePass
- @author Nicolas Grekas <p@tchwork.com>
Namespace
Symfony\Component\DependencyInjection\CompilerCode
protected function getConstructor(Definition $definition, bool $required) : ?\ReflectionFunctionAbstract {
if ($definition->isSynthetic()) {
return null;
}
if (\is_string($factory = $definition->getFactory())) {
if (str_starts_with($factory, '@=')) {
return new \ReflectionFunction(static function (...$args) {
});
}
if (!\function_exists($factory)) {
throw new RuntimeException(\sprintf('Invalid service "%s": function "%s" does not exist.', $this->currentId, $factory));
}
$r = new \ReflectionFunction($factory);
if (false !== $r->getFileName() && file_exists($r->getFileName())) {
$this->container
->fileExists($r->getFileName());
}
return $r;
}
if ($factory) {
[
$class,
$method,
] = $factory;
if ('__construct' === $method) {
throw new RuntimeException(\sprintf('Invalid service "%s": "__construct()" cannot be used as a factory method.', $this->currentId));
}
if ($class instanceof Reference) {
$factoryDefinition = $this->container
->findDefinition((string) $class);
while (null === ($class = $factoryDefinition->getClass()) && $factoryDefinition instanceof ChildDefinition) {
$factoryDefinition = $this->container
->findDefinition($factoryDefinition->getParent());
}
}
elseif ($class instanceof Definition) {
$class = $class->getClass();
}
else {
$class ??= $definition->getClass();
}
return $this->getReflectionMethod(new Definition($class), $method);
}
while (null === ($class = $definition->getClass()) && $definition instanceof ChildDefinition) {
$definition = $this->container
->findDefinition($definition->getParent());
}
try {
if (!($r = $this->container
->getReflectionClass($class))) {
if (null === $class) {
throw new RuntimeException(\sprintf('Invalid service "%s": the class is not set.', $this->currentId));
}
throw new RuntimeException(\sprintf('Invalid service "%s": class "%s" does not exist.', $this->currentId, $class));
}
} catch (\ReflectionException $e) {
throw new RuntimeException(\sprintf('Invalid service "%s": ', $this->currentId) . lcfirst($e->getMessage()));
}
if (!($r = $r->getConstructor())) {
if ($required) {
throw new RuntimeException(\sprintf('Invalid service "%s": class%s has no constructor.', $this->currentId, \sprintf($class !== $this->currentId ? ' "%s"' : '', $class)));
}
}
elseif (!$r->isPublic()) {
throw new RuntimeException(\sprintf('Invalid service "%s": ', $this->currentId) . \sprintf($class !== $this->currentId ? 'constructor of class "%s"' : 'its constructor', $class) . ' must be public.');
}
return $r;
}