Skip to content

Commit d7f3d6b

Browse files
[DependencyInjection] Fix optimizing ClassExistenceResource
1 parent a4c9b4f commit d7f3d6b

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

ContainerBuilder.php

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -273,46 +273,58 @@ public function addResource(ResourceInterface $resource): static
273273
if ($resource instanceof DirectoryResource && $this->inVendors($resource->getResource())) {
274274
return $this;
275275
}
276-
if ($resource instanceof ClassExistenceResource) {
277-
$class = $resource->getResource();
276+
if (!$resource instanceof ClassExistenceResource) {
277+
$this->resources[(string) $resource] = $resource;
278278

279-
$inVendor = false;
280-
foreach (spl_autoload_functions() as $autoloader) {
281-
if (!\is_array($autoloader)) {
282-
continue;
283-
}
279+
return $this;
280+
}
284281

285-
if ($autoloader[0] instanceof DebugClassLoader) {
286-
$autoloader = $autoloader[0]->getClassLoader();
287-
}
282+
$class = $resource->getResource();
283+
284+
if (!(new ClassExistenceResource($class, false))->isFresh(1)) {
285+
if (!$this->inVendors((new \ReflectionClass($class))->getFileName())) {
286+
$this->resources[$class] = $resource;
287+
}
288+
289+
return $this;
290+
}
291+
292+
$inVendor = true;
293+
foreach (spl_autoload_functions() as $autoloader) {
294+
if (!\is_array($autoloader)) {
295+
$inVendor = false;
296+
break;
297+
}
298+
299+
if ($autoloader[0] instanceof DebugClassLoader) {
300+
$autoloader = $autoloader[0]->getClassLoader();
301+
}
288302

289-
if (!\is_array($autoloader) || !$autoloader[0] instanceof ClassLoader || !$autoloader[0]->findFile(__CLASS__)) {
303+
if (!\is_array($autoloader) || !$autoloader[0] instanceof ClassLoader) {
304+
$inVendor = false;
305+
break;
306+
}
307+
308+
foreach ($autoloader[0]->getPrefixesPsr4() as $prefix => $dirs) {
309+
if (!str_starts_with($class, $prefix)) {
290310
continue;
291311
}
292312

293-
foreach ($autoloader[0]->getPrefixesPsr4() as $prefix => $dirs) {
294-
if ('' === $prefix || !str_starts_with($class, $prefix)) {
313+
foreach ($dirs as $dir) {
314+
if (!$dir = realpath($dir)) {
295315
continue;
296316
}
297317

298-
foreach ($dirs as $dir) {
299-
if (!$dir = realpath($dir)) {
300-
continue;
301-
}
302-
303-
if (!$inVendor = $this->inVendors($dir)) {
304-
break 3;
305-
}
318+
if (!$inVendor = $this->inVendors($dir)) {
319+
break 3;
306320
}
307321
}
308322
}
309-
310-
if ($inVendor) {
311-
return $this;
312-
}
313323
}
314324

315-
$this->resources[(string) $resource] = $resource;
325+
if (!$inVendor) {
326+
$this->resources[$class] = $resource;
327+
}
316328

317329
return $this;
318330
}

Tests/ContainerBuilderTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,10 @@ public function testGetReflectionClass()
12301230
$r2 = $container->getReflectionClass('BarClass');
12311231
$r3 = $container->getReflectionClass('BarClass');
12321232

1233-
$this->assertNull($container->getReflectionClass('BarMissingClass'));
1233+
$this->assertNull($container->getReflectionClass(BarMissingClass::class));
1234+
1235+
// No resource should be added for this class because no autoloader would be able to load it
1236+
$this->assertNull($container->getReflectionClass(\BarMissingClass::class));
12341237

12351238
$this->assertEquals($r1, $r2);
12361239
$this->assertSame($r2, $r3);
@@ -1240,7 +1243,7 @@ public function testGetReflectionClass()
12401243
$this->assertCount(2, $resources);
12411244

12421245
$this->assertSame('reflection.BarClass', (string) $resources[0]);
1243-
$this->assertSame('BarMissingClass', (string) end($resources));
1246+
$this->assertSame(BarMissingClass::class, (string) end($resources));
12441247
}
12451248

12461249
public function testGetReflectionClassOnInternalTypes()

0 commit comments

Comments
 (0)