Skip to content

Commit 75d79e9

Browse files
javiereguiluzfabpot
authored andcommitted
[DependencyInjection] Improve the error message when there is no extension to load some configuration
1 parent af02616 commit 75d79e9

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

Loader/Configurator/ContainerConfigurator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2222
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
2323
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
24+
use Symfony\Component\DependencyInjection\Loader\UndefinedExtensionHandler;
2425
use Symfony\Component\ExpressionLanguage\Expression;
2526

2627
/**
@@ -58,7 +59,7 @@ final public function extension(string $namespace, array $config, bool $prepend
5859

5960
if (!$this->container->hasExtension($namespace)) {
6061
$extensions = array_filter(array_map(fn (ExtensionInterface $ext) => $ext->getAlias(), $this->container->getExtensions()));
61-
throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in "%s"). Looked for namespace "%s", found "%s".', $namespace, $this->file, $namespace, $extensions ? implode('", "', $extensions) : 'none'));
62+
throw new InvalidArgumentException(UndefinedExtensionHandler::getErrorMessage($namespace, $this->file, $namespace, $extensions));
6263
}
6364

6465
$this->container->loadFromExtension($namespace, static::processValue($config));

Loader/PhpFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ private function configBuilder(string $namespace): ConfigBuilderInterface
183183

184184
if (!$this->container->hasExtension($alias)) {
185185
$extensions = array_filter(array_map(fn (ExtensionInterface $ext) => $ext->getAlias(), $this->container->getExtensions()));
186-
throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s". Looked for namespace "%s", found "%s".', $namespace, $alias, $extensions ? implode('", "', $extensions) : 'none'));
186+
throw new InvalidArgumentException(UndefinedExtensionHandler::getErrorMessage($namespace, null, $alias, $extensions));
187187
}
188188

189189
$extension = $this->container->getExtension($alias);

Loader/UndefinedExtensionHandler.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Loader;
13+
14+
class UndefinedExtensionHandler
15+
{
16+
private const BUNDLE_EXTENSIONS = [
17+
'debug' => 'DebugBundle',
18+
'doctrine' => 'DoctrineBundle',
19+
'doctrine_migrations' => 'DoctrineMigrationsBundle',
20+
'framework' => 'FrameworkBundle',
21+
'maker' => 'MakerBundle',
22+
'monolog' => 'MonologBundle',
23+
'security' => 'SecurityBundle',
24+
'twig' => 'TwigBundle',
25+
'twig_component' => 'TwigComponentBundle',
26+
'ux_icons' => 'UXIconsBundle',
27+
'web_profiler' => 'WebProfilerBundle',
28+
];
29+
30+
public static function getErrorMessage(string $extensionName, ?string $loadingFilePath, string $namespaceOrAlias, array $foundExtensionNamespaces): string
31+
{
32+
$message = '';
33+
if (isset(self::BUNDLE_EXTENSIONS[$extensionName])) {
34+
$message .= sprintf('Did you forget to install or enable the %s? ', self::BUNDLE_EXTENSIONS[$extensionName]);
35+
}
36+
37+
$message .= match (true) {
38+
\is_string($loadingFilePath) => sprintf('There is no extension able to load the configuration for "%s" (in "%s"). ', $extensionName, $loadingFilePath),
39+
default => sprintf('There is no extension able to load the configuration for "%s". ', $extensionName),
40+
};
41+
42+
$message .= sprintf('Looked for namespace "%s", found "%s".', $namespaceOrAlias, $foundExtensionNamespaces ? implode('", "', $foundExtensionNamespaces) : 'none');
43+
44+
return $message;
45+
}
46+
}

Loader/XmlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ private function validateExtensions(\DOMDocument $dom, string $file): void
810810
// can it be handled by an extension?
811811
if (!$this->container->hasExtension($node->namespaceURI)) {
812812
$extensionNamespaces = array_filter(array_map(fn (ExtensionInterface $ext) => $ext->getNamespace(), $this->container->getExtensions()));
813-
throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in "%s"). Looked for namespace "%s", found "%s".', $node->tagName, $file, $node->namespaceURI, $extensionNamespaces ? implode('", "', $extensionNamespaces) : 'none'));
813+
throw new InvalidArgumentException(UndefinedExtensionHandler::getErrorMessage($node->tagName, $file, $node->namespaceURI, $extensionNamespaces));
814814
}
815815
}
816816
}

Loader/YamlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ private function validate(mixed $content, string $file): ?array
804804

805805
if (!$this->container->hasExtension($namespace)) {
806806
$extensionNamespaces = array_filter(array_map(fn (ExtensionInterface $ext) => $ext->getAlias(), $this->container->getExtensions()));
807-
throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in "%s"). Looked for namespace "%s", found "%s".', $namespace, $file, $namespace, $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'));
807+
throw new InvalidArgumentException(UndefinedExtensionHandler::getErrorMessage($namespace, $file, $namespace, $extensionNamespaces));
808808
}
809809
}
810810

0 commit comments

Comments
 (0)