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 82448bc

Browse filesBrowse files
[DependencyInjection] Add ContainerBuilder::isAvailable() to help with conditional configuration
1 parent 386555b commit 82448bc
Copy full SHA for 82448bc

File tree

13 files changed

+126
-88
lines changed
Filter options

13 files changed

+126
-88
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterUidTypePass.php
+1-1Lines 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/Bridge/Doctrine/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/composer.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"symfony/stopwatch": "^4.4|^5.0",
3131
"symfony/cache": "^5.1",
3232
"symfony/config": "^4.4|^5.0",
33-
"symfony/dependency-injection": "^4.4|^5.0",
33+
"symfony/dependency-injection": "^5.3",
3434
"symfony/form": "^5.1.3",
3535
"symfony/http-kernel": "^5.0",
3636
"symfony/messenger": "^4.4|^5.0",
@@ -54,7 +54,7 @@
5454
"conflict": {
5555
"doctrine/dbal": "<2.10",
5656
"phpunit/phpunit": "<5.4.3",
57-
"symfony/dependency-injection": "<4.4",
57+
"symfony/dependency-injection": "<5.3",
5858
"symfony/form": "<5.1",
5959
"symfony/http-kernel": "<5",
6060
"symfony/messenger": "<4.4",

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+75-65Lines changed: 75 additions & 65 deletions
Large diffs are not rendered by default.

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php
+5-5Lines changed: 5 additions & 5 deletions
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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/composer.json
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php
+5-5Lines changed: 5 additions & 5 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

@@ -111,7 +111,7 @@ public function process(ContainerBuilder $container)
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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php
+4-4Lines 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

‎src/Symfony/Bundle/TwigBundle/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/composer.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"require-dev": {
2828
"symfony/asset": "^4.4|^5.0",
2929
"symfony/stopwatch": "^4.4|^5.0",
30-
"symfony/dependency-injection": "^5.2",
30+
"symfony/dependency-injection": "^5.3",
3131
"symfony/expression-language": "^4.4|^5.0",
3232
"symfony/finder": "^4.4|^5.0",
3333
"symfony/form": "^4.4|^5.0",
@@ -40,7 +40,7 @@
4040
"doctrine/cache": "~1.0"
4141
},
4242
"conflict": {
43-
"symfony/dependency-injection": "<5.2",
43+
"symfony/dependency-injection": "<5.3",
4444
"symfony/framework-bundle": "<5.0",
4545
"symfony/translation": "<5.0"
4646
},

‎src/Symfony/Component/DependencyInjection/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add `ServicesConfigurator::remove()` in the PHP-DSL
8+
* Add `ContainerBuilder::isAvailable()` to help with conditional configuration
89

910
5.2.0
1011
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AbstractRecursivePass.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ protected function getReflectionMethod(Definition $definition, string $method)
197197
private function getExpressionLanguage(): ExpressionLanguage
198198
{
199199
if (null === $this->expressionLanguage) {
200-
if (!class_exists(ExpressionLanguage::class)) {
201-
throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
200+
if (!$this->container->isAvailable('symfony/expression-language', ExpressionLanguage::class)) {
201+
throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed. Try running "composer require symfony/expression-language".');
202202
}
203203

204204
$providers = $this->container->getExpressionLanguageProviders();

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function process(ContainerBuilder $container)
3838
$definitions = $container->getDefinitions();
3939
$aliases = $container->getAliases();
4040
$exprLangProviders = $container->getExpressionLanguageProviders();
41-
$configAvailable = class_exists(BaseNode::class);
41+
$configAvailable = $container->isAvailable('symfony/config', BaseNode::class);
4242

4343
foreach ($container->getExtensions() as $extension) {
4444
if ($extension instanceof PrependExtensionInterface) {

‎src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/ContainerBuilder.php
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection;
1313

14+
use Composer\InstalledVersions;
1415
use Psr\Container\ContainerInterface as PsrContainerInterface;
1516
use Symfony\Component\Config\Resource\ClassExistenceResource;
1617
use Symfony\Component\Config\Resource\ComposerResource;
@@ -407,6 +408,27 @@ public function fileExists(string $path, $trackContents = true): bool
407408
return $exists;
408409
}
409410

411+
final public function isAvailable(string $package, string $class, string $method = null): bool
412+
{
413+
if (!$class = $this->getReflectionClass($class, false)) {
414+
return false;
415+
}
416+
417+
if ($method && !$class->hasMethod($method)) {
418+
return false;
419+
}
420+
421+
if ($class->isInternal() || !class_exists(InstalledVersions::class)) {
422+
return true;
423+
}
424+
425+
if (InstalledVersions::isInstalled($package)) {
426+
return InstalledVersions::isInstalled($package, true);
427+
}
428+
429+
return true;
430+
}
431+
410432
/**
411433
* Loads the configuration for an extension.
412434
*

‎src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ final public function services(): ServicesConfigurator
7171
return new ServicesConfigurator($this->container, $this->loader, $this->instanceof, $this->path, $this->anonymousCount);
7272
}
7373

74+
final public function isAvailable(string $package, string $class, string $method = null): bool
75+
{
76+
return $this->container->isAvailable($package, $class, $method);
77+
}
78+
7479
/**
7580
* @return static
7681
*/

0 commit comments

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