Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection] Always preload services classes tagged with container.preload #36742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ private function addServiceInclude(string $cId, Definition $definition): string
$lineage = [];
foreach ($this->inlinedDefinitions as $def) {
if (!$def->isDeprecated()) {
foreach ($this->getClasses($def, $cId) as $class) {
foreach ($this->getClasses($def) as $class) {
$this->collectLineage($class, $lineage);
}
}
Expand All @@ -596,7 +596,7 @@ private function addServiceInclude(string $cId, Definition $definition): string
&& $this->container->has($id)
&& $this->isTrivialInstance($def = $this->container->findDefinition($id))
) {
foreach ($this->getClasses($def, $cId) as $class) {
foreach ($this->getClasses($def) as $class) {
$this->collectLineage($class, $lineage);
}
}
Expand Down Expand Up @@ -851,9 +851,9 @@ protected function {$methodName}($lazyInitialization)
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->preloadTags[1])) {
} elseif ($definition->hasTag($this->preloadTags[0]) || !$definition->hasTag($this->preloadTags[1])) {
foreach ($this->inlinedDefinitions as $def) {
foreach ($this->getClasses($def, $id) as $class) {
foreach ($this->getClasses($def) as $class) {
$this->preload[$class] = $class;
}
}
Expand Down Expand Up @@ -1017,10 +1017,10 @@ private function addServices(array &$services = null): string
foreach ($definitions as $id => $definition) {
if (!$definition->isSynthetic()) {
$services[$id] = $this->addService($id, $definition);
} elseif (!$definition->hasTag($this->preloadTags[1])) {
} elseif ($definition->hasTag($this->preloadTags[0]) || !$definition->hasTag($this->preloadTags[1])) {
$services[$id] = null;

foreach ($this->getClasses($definition, $id) as $class) {
foreach ($this->getClasses($definition) as $class) {
$this->preload[$class] = $class;
}
}
Expand All @@ -1046,7 +1046,7 @@ private function generateServiceFiles(array $services): iterable
ksort($definitions);
foreach ($definitions as $id => $definition) {
if ((list($file, $code) = $services[$id]) && null !== $file && ($definition->isPublic() || !$this->isTrivialInstance($definition) || isset($this->locatedIds[$id]))) {
yield $file => [$code, !$definition->hasTag($this->preloadTags[1]) && !$definition->isDeprecated() && !$definition->hasErrors()];
yield $file => [$code, ($definition->hasTag($this->preloadTags[0]) || !$definition->hasTag($this->preloadTags[1])) && !$definition->isDeprecated() && !$definition->hasErrors()];
}
}
}
Expand Down Expand Up @@ -1399,7 +1399,7 @@ private function addInlineRequires(): string
$inlinedDefinitions = $this->getDefinitionsFromArguments([$definition]);

foreach ($inlinedDefinitions as $def) {
foreach ($this->getClasses($def, $id) as $class) {
foreach ($this->getClasses($def) as $class) {
$this->collectLineage($class, $lineage);
}
}
Expand Down Expand Up @@ -2130,17 +2130,15 @@ private function getAutoloadFile(): ?string
return null;
}

private function getClasses(Definition $definition, string $id): array
private function getClasses(Definition $definition): array
{
$classes = [];

while ($definition instanceof Definition) {
foreach ($definition->getTag($this->preloadTags[0]) as $tag) {
if (!isset($tag['class'])) {
throw new InvalidArgumentException(sprintf('Missing attribute "class" on tag "%s" for service "%s".', $this->preloadTags[0], $id));
if (isset($tag['class'])) {
$classes[] = trim($tag['class'], '\\');
}

$classes[] = trim($tag['class'], '\\');
}

$classes[] = trim($definition->getClass(), '\\');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,11 @@ public function testDumpAsFiles()
->addError('No-no-no-no');
$container->compile();
$dumper = new PhpDumper($container);
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot', 'inline_factories_parameter' => false, 'inline_class_loader_parameter' => false]), true);
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot', 'inline_factories_parameter' => false, 'inline_class_loader_parameter' => false, 'build_time' => 1588866825]), true);
if ('\\' === \DIRECTORY_SEPARATOR) {
$dump = str_replace('\\\\Fixtures\\\\includes\\\\foo.php', '/Fixtures/includes/foo.php', $dump);
}
$this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services9_as_files.txt', $dump);
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services9_as_files.txt', $dump);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use a regex anymore because the file to compare to have become too big (preg_match(): Compilation failed: regular expression is too large at offset 35752). I guess this strategy is useful when merging from lower branches? Do someone have a better idea than strict comparison or creating a new file? 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are failing: I need to recheck the strategy.

}

public function testDumpAsFilesWithFactoriesInlined()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@
->tag('container.preload', ['class' => 'Some\Sidekick2'])
->public();

$s->set('no_preload', 'TestNoPreload')
->tag('container.no_preload')
->public();

$s->set('preload_no_preload', 'TestPreloadNoPreload')
->tag('container.preload')
->tag('container.no_preload')
->public();

$s->alias('alias_for_foo', 'foo')->private()->public();
$s->alias('alias_for_alias', ref('alias_for_foo'));
};
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,13 @@
->addTag('container.preload', ['class' => 'Some\Sidekick1'])
->addTag('container.preload', ['class' => 'Some\Sidekick2']);

$container->register('no_preload', 'TestNoPreload')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also added a test with container.no_preload as I think it was not tested.

->addTag('container.no_preload')
->setPublic(true);

$container->register('preload_no_preload', 'TestPreloadNoPreload')
->addTag('container.preload')
->addTag('container.no_preload')
->setPublic(true);

return $container;
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ digraph sc {
node_runtime_error [label="runtime_error\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_errored_definition [label="errored_definition\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_preload_sidekick [label="preload_sidekick\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_no_preload [label="no_preload\nTestNoPreload\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_preload_no_preload [label="preload_no_preload\nTestPreloadNoPreload\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_foo2 [label="foo2\n\n", shape=record, fillcolor="#ff9999", style="filled"];
node_foo3 [label="foo3\n\n", shape=record, fillcolor="#ff9999", style="filled"];
node_foobaz [label="foobaz\n\n", shape=record, fillcolor="#ff9999", style="filled"];
Expand Down
Loading