8000 [DependencyInjection] Add ContainerBuilder::isAvailable() to help wit… · symfony/symfony@4d42dfe · GitHub
[go: up one dir, main page]

Skip to content
< 10000 script crossorigin="anonymous" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_gsap_index_js-028cb2a18f5a.js" defer="defer">

Commit 4d42dfe

Browse files
[DependencyInjection] Add ContainerBuilder::isAvailable() to help with conditional configuration
1 parent 386555b commit 4d42dfe

File tree

18 files changed

+143
-113
lines changed

18 files changed

+143
-113
lines changed

src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterUidTypePass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class RegisterUidTypePass implements CompilerPassInterface
2424
*/
2525
public function process(ContainerBuilder $container)
2626
{
27-
if (!class_exists(AbstractUid::class)) {
27+
if (!$container->isAvailable('symfony/uid', AbstractUid::class)) {
2828
return;
2929
}
3030

src/Symfony/Bundle/DebugBundle/DependencyInjection/Configuration.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1515
use Symfony\Component\Config\Definition\ConfigurationInterface;
16-
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
1716

1817
/**
1918
* DebugExtension configuration structure.
@@ -51,22 +50,15 @@ public function getConfigTreeBuilder()
5150
->example('php://stderr, or tcp://%env(VAR_DUMPER_SERVER)% when using the "server:dump" command')
5251
->defaultNull()
5352
->end()
53+
->enumNode('theme')
54+
->info('Changes the color of the dump() output when rendered directly on the templating. "dark" (default) or "light"')
55+
->example('dark')
56+
->values(['dark', 'light'])
57+
->defaultValue('dark')
58+
->end()
5459
->end()
5560
;
5661

57-
if (method_exists(HtmlDumper::class, 'setTheme')) {
58-
$rootNode
59-
->children()
60-
->enumNode('theme')
61-
->info('Changes the color of the dump() output when rendered directly on the templating. "dark" (default) or "light"')
62-
->example('dark')
63-
->values(['dark', 'light'])
64-
->defaultValue('dark')
65-
->end()
66-
->end()
67-
;
68-
}
69-
7062
return $treeBuilder;
7163
}
7264
}

src/Symfony/Bundle/DebugBundle/DependencyInjection/DebugExtension.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public function load(array $configs, ContainerBuilder $container)
4545
->addMethodCall('setMinDepth', [$config['min_depth']])
4646
->addMethodCall('setMaxString', [$config['max_string_length']]);
4747

48-
if (method_exists(ReflectionCaster::class, 'unsetClosureFileInfo')) {
48+
if ($container->isAvailable('symfony/var-dumper', ReflectionCaster::class, 'unsetClosureFileInfo')) {
4949
$container->getDefinition('var_dumper.cloner')
5050
->addMethodCall('addCasters', [ReflectionCaster::UNSET_CLOSURE_FILE_INFO]);
5151
}
5252

53-
if (method_exists(HtmlDumper::class, 'setTheme') && 'dark' !== $config['theme']) {
53+
if ($container->isAvailable('symfony/var-dumper', HtmlDumper::class, 'setTheme') && 'dark' !== $config['theme']) {
5454
$container->getDefinition('var_dumper.html_dumper')
5555
->addMethodCall('setTheme', [$config['theme']]);
5656
}
@@ -84,15 +84,15 @@ public function load(array $configs, ContainerBuilder $container)
8484
;
8585
}
8686

87-
if (method_exists(CliDumper::class, 'setDisplayOptions')) {
87+
if ($container->isAvailable('symfony/var-dumper', CliDumper::class, 'setDisplayOptions')) {
8888
$container->getDefinition('var_dumper.cli_dumper')
8989
->addMethodCall('setDisplayOptions', [[
9090
'fileLinkFormat' => new Reference('debug.file_link_formatter', ContainerBuilder::IGNORE_ON_INVALID_REFERENCE),
9191
]])
9292
;
9393
}
9494

95-
if (!class_exists(ServerLogCommand::class)) {
95+
if (!$container->isAvailable('symfony/monolog-bridge', ServerLogCommand::class)) {
9696
$container->removeDefinition('monolog.command.server_log');
9797
}
9898
}

src/Symfony/Bundle/DebugBundle/Resources/config/services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
->set('monolog.command.server_log', ServerLogCommand::class)
135135
;
136136

137-
if (class_exists(ConsoleFormatter::class) && interface_exists(FormatterInterface::class)) {
137+
if ($container->isAvailable('symfony/monolog-bridge', ConsoleFormatter::class) && $container->isAvailable('monolog/monolog', FormatterInterface::class)) {
138138
$container->services()->get('monolog.command.server_log')->tag('console.command');
139139
}
140140
};

src/Symfony/Bundle/DebugBundle/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
},
2525
"require-dev": {
2626
"symfony/config": "^4.4|^5.0",
27-
"symfony/dependency-injection": "^4.4|^5.0",
27+
"symfony/dependency-injection": "^5.3",
2828
"symfony/web-profiler-bundle": "^4.4|^5.0"
2929
},
3030
"conflict": {
3131
"symfony/config": "<4.4",
32-
"symfony/dependency-injection": "<5.2"
32+
"symfony/dependency-injection": "<5.3"
3333
},
3434
"suggest": {
3535
"symfony/config": "For service container configuration",

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 78 additions & 68 deletions
Large diffs are not rendered by default.

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 5 additions & 5 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function load(array $configs, ContainerBuilder $container)
125125
$loader->load('security_legacy.php');
126126
}
127127

128-
if (class_exists(AbstractExtension::class)) {
128+
if ($container->isAvailable('twig/twig', AbstractExtension::class)) {
129129
$loader->load('templating_twig.php');
130130
}
131131

@@ -136,7 +136,7 @@ public function load(array $configs, ContainerBuilder $container)
136136
$loader->load('security_debug.php');
137137
}
138138

139-
if (!class_exists(\Symfony\Component\ExpressionLanguage\ExpressionLanguage::class)) {
139+
if (!$container->isAvailable('symfony/expression-language', \Symfony\Component\ExpressionLanguage\ExpressionLanguage::class)) {
140140
$container->removeDefinition('security.expression_language');
141141
$container->removeDefinition('security.access.expression_voter');
142142
}
@@ -170,7 +170,7 @@ public function load(array $configs, ContainerBuilder $container)
170170
$this->createEncoders($config['encoders'], $container);
171171
}
172172

173-
if (class_exists(Application::class)) {
173+
if ($container->isAvailable('symfony/console', Application::class)) {
174174
$loader->load('console.php');
175175
$container->getDefinition('security.command.user_password_encoder')->replaceArgument(1, array_keys($config['encoders']));
176176
}
@@ -844,8 +844,8 @@ private function createExpression(ContainerBuilder $container, string $expressio
844844
return $this->expressions[$id];
845845
}
846846

847-
if (!class_exists(\Symfony\Component\ExpressionLanguage\ExpressionLanguage::class)) {
848-
throw new \RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
847+
if (!$container->isAvailable('symfony/expression-language', \Symfony\Component\ExpressionLanguage\ExpressionLanguage::class)) {
848+
throw new \RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed. Try running "composer require symfony/expression-language".');
849849
}
850850

851851
$container

src/Symfony/Bundle/SecurityBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": ">=7.2.5",
2020
"ext-xml": "*",
2121
"symfony/config": "^4.4|^5.0",
22-
"symfony/dependency-injection": "^5.2",
22+
"symfony/dependency-injection": "^5.3",
2323
"symfony/deprecation-contracts": "^2.1",
2424
"symfony/event-dispatcher": "^5.1",
2525
"symfony/http-kernel": "^5.0",

src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ class ExtensionPass implements CompilerPassInterface
2323
{
2424
public function process(ContainerBuilder $container)
2525
{
26-
if (!class_exists(\Symfony\Component\Asset\Packages::class)) {
26+
if (!$container->isAvailable('symfony/asset', \Symfony\Component\Asset\Packages::class)) {
2727
$container->removeDefinition('twig.extension.assets');
2828
}
2929

30-
if (!class_exists(\Symfony\Component\ExpressionLanguage\Expression::class)) {
30+
if (!$container->isAvailable('symfony/expression-language', \Symfony\Component\ExpressionLanguage\Expression::class)) {
3131
$container->removeDefinition('twig.extension.expression');
3232
}
3333

34-
if (!interface_exists(\Symfony\Component\Routing\Generator\UrlGeneratorInterface::class)) {
34+
if (!$container->isAvailable('symfony/routing', \Symfony\Component\Routing\Generator\UrlGeneratorInterface::class)) {
3535
$container->removeDefinition('twig.extension.routing');
3636
}
3737

38-
if (!class_exists(\Symfony\Component\Yaml\Yaml::class)) {
38+
if (!$container->isAvailable('symfony/yaml', \Symfony\Component\Yaml\Yaml::class)) {
3939
$container->removeDefinition('twig.extension.yaml');
4040
}
4141

@@ -103,15 +103,15 @@ public function process(ContainerBuilder $container)
103103
$container->getDefinition('twig.extension.yaml')->addTag('twig.extension');
104104
}
105105

106-
if (class_exists(\Symfony\Component\Stopwatch\Stopwatch::class)) {
106+
if ($container->isAvailable('symfony/stopwatch', \Symfony\Component\Stopwatch\Stopwatch::class)) {
107107
$container->getDefinition('twig.extension.debug.stopwatch')->addTag('twig.extension');
108108
}
109109

110110
if ($container->hasDefinition('twig.extension.expression')) {
111111
$container->getDefinition('twig.extension.expression')->addTag('twig.extension');
112112
}
113113

114-
if (!class_exists(Workflow::class) || !$container->has('workflow.registry')) {
114+
if (!$container->isAvailable('symfony/workflow', Workflow::class) || !$container->has('workflow.registry')) {
115115
$container->removeDefinition('workflow.twig_extension');
116116
} else {
117117
$container->getDefinition('workflow.twig_extension')->addTag('twig.extension');

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ public function load(array $configs, ContainerBuilder $container)
3737
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3838
$loader->load('twig.php');
3939

40-
if (class_exists(\Symfony\Component\Form\Form::class)) {
40+
if ($container->isAvailable('symfony/form', \Symfony\Component\Form\Form::class)) {
4141
$loader->load('form.php');
4242
}
4343

44-
if (class_exists(Application::class)) {
44+
if ($container->isAvailable('symfony/console', Application::class)) {
4545
$loader->load('console.php');
4646
}
4747

48-
if (class_exists(Mailer::class)) {
48+
if ($container->isAvailable('symfony/mailer', Mailer::class)) {
4949
$loader->load('mailer.php');
5050
}
5151

52-
if (!class_exists(Translator::class)) {
52+
if (!$container->isAvailable('symfony/translation', Translator::class)) {
5353
$container->removeDefinition('twig.translation.extractor');
5454
}
5555

0 commit comments

Comments
 (0)
0