function PhpFileLoader::executeCallback
Resolve the parameters to the $callback and execute it.
1 call to PhpFileLoader::executeCallback()
- PhpFileLoader::load in vendor/
symfony/ dependency-injection/ Loader/ PhpFileLoader.php
File
-
vendor/
symfony/ dependency-injection/ Loader/ PhpFileLoader.php, line 95
Class
- PhpFileLoader
- PhpFileLoader loads service definitions from a PHP file.
Namespace
Symfony\Component\DependencyInjection\LoaderCode
private function executeCallback(callable $callback, ContainerConfigurator $containerConfigurator, string $path) : void {
$callback = $callback(...);
$arguments = [];
$configBuilders = [];
$r = new \ReflectionFunction($callback);
$excluded = true;
$whenAttributes = $r->getAttributes(When::class, \ReflectionAttribute::IS_INSTANCEOF);
$notWhenAttributes = $r->getAttributes(WhenNot::class, \ReflectionAttribute::IS_INSTANCEOF);
if ($whenAttributes && $notWhenAttributes) {
throw new LogicException('Using both #[When] and #[WhenNot] attributes on the same target is not allowed.');
}
if (!$whenAttributes && !$notWhenAttributes) {
$excluded = false;
}
foreach ($whenAttributes as $attribute) {
if ($this->env === $attribute->newInstance()->env) {
$excluded = false;
break;
}
}
foreach ($notWhenAttributes as $attribute) {
if ($excluded = $this->env === $attribute->newInstance()->env) {
break;
}
}
if ($excluded) {
return;
}
foreach ($r->getParameters() as $parameter) {
$reflectionType = $parameter->getType();
if (!$reflectionType instanceof \ReflectionNamedType) {
throw new \InvalidArgumentException(\sprintf('Could not resolve argument "$%s" for "%s". You must typehint it (for example with "%s" or "%s").', $parameter->getName(), $path, ContainerConfigurator::class, ContainerBuilder::class));
}
$type = $reflectionType->getName();
switch ($type) {
case ContainerConfigurator::class:
$arguments[] = $containerConfigurator;
break;
case ContainerBuilder::class:
$arguments[] = $this->container;
break;
case FileLoader::class:
case self::class:
$arguments[] = $this;
break;
case 'string':
if (null !== $this->env && 'env' === $parameter->getName()) {
$arguments[] = $this->env;
break;
}
// no break
default:
try {
$configBuilder = $this->configBuilder($type);
} catch (InvalidArgumentException|\LogicException $e) {
throw new \InvalidArgumentException(\sprintf('Could not resolve argument "%s" for "%s".', $type . ' $' . $parameter->getName(), $path), 0, $e);
}
$configBuilders[] = $configBuilder;
$arguments[] = $configBuilder;
}
}
// Force load ContainerConfigurator to make env(), param() etc available.
class_exists(ContainerConfigurator::class);
++$this->importing;
try {
$callback(...$arguments);
} finally {
--$this->importing;
}
foreach ($configBuilders as $configBuilder) {
$this->loadExtensionConfig($configBuilder->getExtensionAlias(), ContainerConfigurator::processValue($configBuilder->toArray()));
}
$this->loadExtensionConfigs();
}