Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6d8387c

Browse filesBrowse files
[DependencyInjection] Optimize autowiring logic by telling it about about excluded symbols
1 parent c0dbb90 commit 6d8387c
Copy full SHA for 6d8387c

File tree

6 files changed

+44
-8
lines changed
Filter options

6 files changed

+44
-8
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class UnusedTagsPass implements CompilerPassInterface
3333
'console.command',
3434
'container.env_var_loader',
3535
'container.env_var_processor',
36+
'container.excluded',
3637
'container.hot_path',
3738
'container.no_preload',
3839
'container.preload',

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/services.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/services.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigBuilderCacheWarmer;
1515
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
16+
use Symfony\Component\Config\Loader\LoaderInterface;
1617
use Symfony\Component\Config\Resource\SelfCheckingResourceChecker;
1718
use Symfony\Component\Config\ResourceCheckerConfigCacheFactory;
1819
use Symfony\Component\Console\ConsoleEvents;
@@ -26,7 +27,10 @@
2627
use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcherInterfaceComponentAlias;
2728
use Symfony\Component\Filesystem\Filesystem;
2829
use Symfony\Component\Form\FormEvents;
30+
use Symfony\Component\HttpFoundation\Request;
2931
use Symfony\Component\HttpFoundation\RequestStack;
32+
use Symfony\Component\HttpFoundation\Response;
33+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
3034
use Symfony\Component\HttpFoundation\UrlHelper;
3135
use Symfony\Component\HttpKernel\CacheClearer\ChainCacheClearer;
3236
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate;
@@ -218,5 +222,11 @@ class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
218222
->set('config_builder.warmer', ConfigBuilderCacheWarmer::class)
219223
->args([service(KernelInterface::class), service('logger')->nullOnInvalid()])
220224
->tag('kernel.cache_warmer')
225+
226+
// register as abstract and excluded, aka not-autowirable types
227+
->set(LoaderInterface::class)->abstract()->tag('container.excluded')
228+
->set(Request::class)->abstract()->tag('container.excluded')
229+
->set(Response::class)->abstract()->tag('container.excluded')
230+
->set(SessionInterface::class)->abstract()->tag('container.excluded')
221231
;
222232
};

‎src/Symfony/Component/DependencyInjection/Compiler/AbstractRecursivePass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AbstractRecursivePass.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ protected function processValue(mixed $value, bool $isRoot = false)
7575
if (\is_array($value)) {
7676
foreach ($value as $k => $v) {
7777
if ($isRoot) {
78+
if ($v->hasTag('container.excluded')) {
79+
continue;
80+
}
7881
$this->currentId = $k;
7982
}
8083
if ($v !== $processedValue = $this->processValue($v, $isRoot)) {

‎src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,22 @@ private function createTypeNotFoundMessageCallback(TypedReference $reference, st
503503

504504
private function createTypeNotFoundMessage(TypedReference $reference, string $label, string $currentId): string
505505
{
506-
if (!$r = $this->container->getReflectionClass($type = $reference->getType(), false)) {
506+
$type = $reference->getType();
507+
508+
if ($this->container->hasDefinition($type) && $this->container->getDefinition($type)->hasTag('container.excluded')) {
509+
return sprintf('Cannot autowire service "%s": %s has type "%s" but this symbol has been excluded.', $currentId, $label, $type);
510+
}
511+
512+
$namespace = $type;
513+
while (false !== $i = strrpos($namespace, '\\')) {
514+
$namespace = substr($namespace, 0, $i);
515+
516+
if ($this->container->hasDefinition($namespace) && $this->container->getDefinition($namespace)->hasTag('container.excluded')) {
517+
return sprintf('Cannot autowire service "%s": %s has type "%s" but the "%s" namespace has been excluded.', $currentId, $label, $type, $namespace);
518+
}
519+
}
520+
521+
if (!$r = $this->container->getReflectionClass($type, false)) {
507522
// either $type does not exist or a parent class does not exist
508523
try {
509524
$resource = new ClassExistenceResource($type, false);

‎src/Symfony/Component/DependencyInjection/Loader/FileLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/FileLoader.php
+13-3Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ private function findClasses(string $namespace, string $pattern, array $excludeP
189189

190190
$pattern = $parameterBag->unescapeValue($parameterBag->resolveValue($pattern));
191191
$classes = [];
192-
$extRegexp = '/\\.php$/';
193192
$prefixLen = null;
194193
foreach ($this->glob($pattern, true, $resource, false, false, $excludePaths) as $path => $info) {
195194
if (null === $prefixLen) {
@@ -204,10 +203,10 @@ private function findClasses(string $namespace, string $pattern, array $excludeP
204203
continue;
205204
}
206205

207-
if (!preg_match($extRegexp, $path, $m) || !$info->isReadable()) {
206+
if (!str_ends_with($path, '.php') || !$info->isReadable()) {
208207
continue;
209208
}
210-
$class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, -\strlen($m[0]))), '\\');
209+
$class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, -4)), '\\');
211210

212211
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $class)) {
213212
continue;
@@ -242,6 +241,17 @@ private function findClasses(string $namespace, string $pattern, array $excludeP
242241
}
243242
}
244243

244+
if (null !== $prefixLen) {
245+
foreach ($excludePaths as $path => $_) {
246+
$class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, str_ends_with($path, '.php') ? -4 : null)), '\\');
247+
if (!$this->container->has($class)) {
248+
$this->container->register($class)
249+
->setAbstract(true)
250+
->addTag('container.excluded');
251+
}
252+
}
253+
}
254+
245255
return $classes;
246256
}
247257
}

‎src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php
+1-4Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111

1212
namespace Symfony\Component\HttpKernel\DependencyInjection;
1313

14-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1514
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1615
use Symfony\Component\DependencyInjection\Attribute\Target;
1716
use Symfony\Component\DependencyInjection\ChildDefinition;
1817
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1918
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
20-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
2119
use Symfony\Component\DependencyInjection\ContainerBuilder;
2220
use Symfony\Component\DependencyInjection\ContainerInterface;
2321
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
@@ -70,13 +68,12 @@ public function process(ContainerBuilder $container)
7068
if (!$r = $container->getReflectionClass($class)) {
7169
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
7270
}
73-
$isContainerAware = $r->implementsInterface(ContainerAwareInterface::class) || is_subclass_of($class, AbstractController::class);
7471

7572
// get regular public methods
7673
$methods = [];
7774
$arguments = [];
7875
foreach ($r->getMethods(\ReflectionMethod::IS_PUBLIC) as $r) {
79-
if ('setContainer' === $r->name && $isContainerAware) {
76+
if ('setContainer' === $r->name) {
8077
continue;
8178
}
8279
if (!$r->isConstructor() && !$r->isDestructor() && !$r->isAbstract()) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.