Skip to content

Commit 60f0fca

Browse files
[Config][DependencyInjection] Optimize dumped resources for tracking
1 parent 2115c12 commit 60f0fca

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/Symfony/Component/Config/Resource/ReflectionClassResource.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private function loadFiles(\ReflectionClass $class): void
8181
$file = $class->getFileName();
8282
if (false !== $file && is_file($file)) {
8383
foreach ($this->excludedVendors as $vendor) {
84-
if (str_starts_with($file, $vendor) && false !== strpbrk(substr($file, \strlen($vendor), 1), '/'.\DIRECTORY_SEPARATOR)) {
84+
if (\in_array($file[\strlen($vendor)] ?? '', ['/', \DIRECTORY_SEPARATOR], true) && str_starts_with($file, $vendor)) {
8585
$file = false;
8686
break;
8787
}
@@ -154,6 +154,13 @@ private function generateSignature(\ReflectionClass $class): iterable
154154
}
155155

156156
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $m) {
157+
foreach ($this->excludedVendors as $vendor) {
158+
$file = $m->getFileName();
159+
if (\in_array($file[\strlen($vendor)] ?? '', ['/', \DIRECTORY_SEPARATOR], true) && str_starts_with($file, $vendor)) {
160+
continue 2;
161+
}
162+
}
163+
157164
foreach ($m->getAttributes() as $a) {
158165
$attributes[] = [$a->getName(), (string) $a];
159166
}

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection;
1313

14+
use Composer\Autoload\ClassLoader;
1415
use Composer\InstalledVersions;
1516
use Symfony\Component\Config\Resource\ClassExistenceResource;
1617
use Symfony\Component\Config\Resource\ComposerResource;
@@ -46,6 +47,7 @@
4647
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
4748
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
4849
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
50+
use Symfony\Component\ErrorHandler\DebugClassLoader;
4951
use Symfony\Component\ExpressionLanguage\Expression;
5052
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
5153

@@ -117,7 +119,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
117119
private array $vendors;
118120

119121
/**
120-
* @var array<string, bool> the cache for paths being in vendor directories
122+
* @var array<string, bool> whether a path is in a vendor directory
121123
*/
122124
private array $pathsInVendor = [];
123125

@@ -262,6 +264,53 @@ public function addResource(ResourceInterface $resource): static
262264
if ($resource instanceof GlobResource && $this->inVendors($resource->getPrefix())) {
263265
return $this;
264266
}
267+
if ($resource instanceof FileExistenceResource && $this->inVendors($resource->getResource())) {
268+
return $this;
269+
}
270+
if ($resource instanceof FileResource && $this->inVendors($resource->getResource())) {
271+
return $this;
272+
}
273+
if ($resource instanceof DirectoryResource && $this->inVendors($resource->getResource())) {
274+
return $this;
275+
}
276+
if ($resource instanceof ClassExistenceResource) {
277+
$class = $resource->getResource();
278+
279+
$inVendor = false;
280+
foreach (spl_autoload_functions() as $autoloader) {
281+
if (!\is_array($autoloader)) {
282+
continue;
283+
}
284+
285+
if ($autoloader[0] instanceof DebugClassLoader) {
286+
$autoloader = $autoloader[0]->getClassLoader();
287+
}
288+
289+
if (!\is_array($autoloader) || !$autoloader[0] instanceof ClassLoader || !$autoloader[0]->findFile(__CLASS__)) {
290+
continue;
291+
}
292+
293+
foreach ($autoloader[0]->getPrefixesPsr4() as $prefix => $dirs) {
294+
if ('' === $prefix || !str_starts_with($class, $prefix)) {
295+
continue;
296+
}
297+
298+
foreach ($dirs as $dir) {
299+
if (!$dir = realpath($dir)) {
300+
continue;
301+
}
302+
303+
if (!$inVendor = $this->inVendors($dir)) {
304+
break 3;
305+
}
306+
}
307+
}
308+
}
309+
310+
if ($inVendor) {
311+
return $this;
312+
}
313+
}
265314

266315
$this->resources[(string) $resource] = $resource;
267316

@@ -1693,8 +1742,10 @@ private function inVendors(string $path): bool
16931742
}
16941743

16951744
foreach ($this->vendors as $vendor) {
1696-
if (str_starts_with($path, $vendor) && false !== strpbrk(substr($path, \strlen($vendor), 1), '/'.\DIRECTORY_SEPARATOR)) {
1745+
if (\in_array($path[\strlen($vendor)] ?? '', ['/', \DIRECTORY_SEPARATOR], true) && str_starts_with($path, $vendor)) {
1746+
$this->pathsInVendor[$vendor.'/composer'] = false;
16971747
$this->addResource(new FileResource($vendor.'/composer/installed.json'));
1748+
$this->pathsInVendor[$vendor.'/composer'] = true;
16981749

16991750
return $this->pathsInVendor[$path] = true;
17001751
}

0 commit comments

Comments
 (0)