function PhpDumper::addService
1 call to PhpDumper::addService()
- PhpDumper::addServices in vendor/
symfony/ dependency-injection/ Dumper/ PhpDumper.php
File
-
vendor/
symfony/ dependency-injection/ Dumper/ PhpDumper.php, line 822
Class
- PhpDumper
- PhpDumper dumps a service container as a PHP class.
Namespace
Symfony\Component\DependencyInjection\DumperCode
private function addService(string $id, Definition $definition) : array {
$this->definitionVariables = new \SplObjectStorage();
$this->referenceVariables = [];
$this->variableCount = 0;
$this->referenceVariables[$id] = new Variable('instance');
$return = [];
if ($class = $definition->getClass()) {
$class = $class instanceof Parameter ? '%' . $class . '%' : $this->container
->resolveEnvPlaceholders($class);
$return[] = \sprintf(str_starts_with($class, '%') ? '@return object A %1$s instance' : '@return \\%s', ltrim($class, '\\'));
}
elseif ($definition->getFactory()) {
$factory = $definition->getFactory();
if (\is_string($factory) && !str_starts_with($factory, '@=')) {
$return[] = \sprintf('@return object An instance returned by %s()', $factory);
}
elseif (\is_array($factory) && (\is_string($factory[0]) || $factory[0] instanceof Definition || $factory[0] instanceof Reference)) {
$class = $factory[0] instanceof Definition ? $factory[0]->getClass() : (string) $factory[0];
$class = $class instanceof Parameter ? '%' . $class . '%' : $this->container
->resolveEnvPlaceholders($class);
$return[] = \sprintf('@return object An instance returned by %s::%s()', $class, $factory[1]);
}
}
if ($definition->isDeprecated()) {
if ($return && str_starts_with($return[\count($return) - 1], '@return')) {
$return[] = '';
}
$deprecation = $definition->getDeprecation($id);
$return[] = \sprintf('@deprecated %s', ($deprecation['package'] || $deprecation['version'] ? "Since {$deprecation['package']} {$deprecation['version']}: " : '') . $deprecation['message']);
}
$return = str_replace("\n * \n", "\n *\n", implode("\n * ", $return));
$return = $this->container
->resolveEnvPlaceholders($return);
$shared = $definition->isShared() ? ' shared' : '';
$public = $definition->isPublic() ? 'public' : 'private';
$autowired = $definition->isAutowired() ? ' autowired' : '';
$asFile = $this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition);
$methodName = $this->generateMethodName($id);
if ($asFile || $definition->isLazy()) {
$lazyInitialization = ', $lazyLoad = true';
}
else {
$lazyInitialization = '';
}
$code = <<<EOF
/*{<span class="php-variable">$this</span>-><span class="php-function-or-constant property member-of-self">docStar</span>}
* Gets the {<span class="php-variable">$public</span>} '{<span class="php-variable">$id</span>}'{<span class="php-variable">$shared</span>}{<span class="php-variable">$autowired</span>} service.
*
* {<span class="php-variable">$return</span>}
EOF;
$code = str_replace('*/', ' ', $code) . <<<EOF
*/
protected static function {<span class="php-variable">$methodName</span>}(\$container{<span class="php-variable">$lazyInitialization</span>})
{
EOF;
if ($asFile) {
$file = $methodName . '.php';
$code = str_replace("protected static function {$methodName}(", 'public static function do(', $code);
}
else {
$file = null;
}
if ($definition->hasErrors() && ($e = $definition->getErrors())) {
$code .= \sprintf(" throw new RuntimeException(%s);\n", $this->export(reset($e)));
}
else {
$this->serviceCalls = [];
$this->inlinedDefinitions = $this->getDefinitionsFromArguments([
$definition,
], null, $this->serviceCalls);
if ($definition->isDeprecated()) {
$deprecation = $definition->getDeprecation($id);
$code .= \sprintf(" trigger_deprecation(%s, %s, %s);\n\n", $this->export($deprecation['package']), $this->export($deprecation['version']), $this->export($deprecation['message']));
}
elseif ($definition->hasTag($this->hotPathTag) || !$definition->hasTag($this->preloadTags[1])) {
foreach ($this->inlinedDefinitions as $def) {
foreach ($this->getClasses($def, $id) as $class) {
$this->preload[$class] = $class;
}
}
}
if (!$definition->isShared()) {
$factory = \sprintf('$container->factories%s[%s]', $definition->isPublic() ? '' : "['service_container']", $this->doExport($id));
}
$asGhostObject = false;
if ($isProxyCandidate = $this->isProxyCandidate($definition, $asGhostObject, $id)) {
$definition = $isProxyCandidate;
if (!$definition->isShared()) {
$code .= \sprintf(' %s ??= ', $factory);
if ($definition->isPublic()) {
$code .= \sprintf("fn () => self::%s(\$container);\n\n", $asFile ? 'do' : $methodName);
}
else {
$code .= \sprintf("self::%s(...);\n\n", $asFile ? 'do' : $methodName);
}
}
$lazyLoad = $asGhostObject ? '$proxy' : 'false';
$factoryCode = $asFile ? \sprintf('self::do($container, %s)', $lazyLoad) : \sprintf('self::%s($container, %s)', $methodName, $lazyLoad);
$code .= $this->getProxyDumper()
->getProxyFactoryCode($definition, $id, $factoryCode);
}
$c = $this->addServiceInclude($id, $definition, null !== $isProxyCandidate);
if ('' !== $c && $isProxyCandidate && !$definition->isShared()) {
$c = implode("\n", array_map(fn($line) => $line ? ' ' . $line : $line, explode("\n", $c)));
$code .= " static \$include = true;\n\n";
$code .= " if (\$include) {\n";
$code .= $c;
$code .= " \$include = false;\n";
$code .= " }\n\n";
}
else {
$code .= $c;
}
$c = $this->addInlineService($id, $definition);
if (!$isProxyCandidate && !$definition->isShared()) {
$c = implode("\n", array_map(fn($line) => $line ? ' ' . $line : $line, explode("\n", $c)));
$lazyloadInitialization = $definition->isLazy() ? ', $lazyLoad = true' : '';
$c = \sprintf(" %s = function (\$container%s) {\n%s };\n\n return %1\$s(\$container);\n", $factory, $lazyloadInitialization, $c);
}
$code .= $c;
}
$code .= " }\n";
$this->definitionVariables = $this->inlinedDefinitions = null;
$this->referenceVariables = $this->serviceCalls = null;
return [
$file,
$code,
];
}