8000 [DI][Config] Add ContainerBuilder::getClassExists() for checking and … · symfony/symfony@506bba8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 506bba8

Browse files
[DI][Config] Add ContainerBuilder::getClassExists() for checking and tracking class, interface or trait existence
1 parent b1098d9 commit 506bba8

File tree

15 files changed

+141
-65
lines changed

15 files changed

+141
-65
lines changed

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

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;
1313

14+
use Doctrine\Common\Annotations\Annotation;
1415
use Doctrine\Common\Annotations\Reader;
16+
use phpDocumentor\Reflection\DocBlockFactoryInterface;
1517
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
18+
use Symfony\Component\Asset\Package;
1619
use Symfony\Component\Cache\Adapter\AdapterInterface;
1720
use Symfony\Component\Config\Loader\LoaderInterface;
1821
use Symfony\Component\DependencyInjection\Alias;
@@ -26,16 +29,21 @@
2629
use Symfony\Component\Config\Resource\FileResource;
2730
use Symfony\Component\Config\Resource\DirectoryResource;
2831
use Symfony\Component\Finder\Finder;
32+
use Symfony\Component\Form\Form;
33+
use Symfony\Component\Form\FormInterface;
2934
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
3035
use Symfony\Component\Config\FileLocator;
31-
use Symfony\Component\Config\Resource\ClassExistenceResource;
3236
use Symfony\Component\PropertyAccess\PropertyAccessor;
37+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
3338
use Symfony\Component\Serializer\Encoder\YamlEncoder;
3439
use Symfony\Component\Serializer\Encoder\CsvEncoder;
3540
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
3641
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
3742
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
3843
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
44+
use Symfony\Component\Templating\PhpEngine;
45+
use Symfony\Component\Translation\Translator;
46+
use Symfony\Component\Validator\Validation;
3947
use Symfony\Component\Workflow;
4048
use Symfony\Component\Workflow\SupportStrategy\ClassInstanceSupportStrategy;
4149
use Symfony\Component\Yaml\Yaml;
@@ -85,8 +93,7 @@ public function load(array $configs, ContainerBuilder $container)
8593

8694
$loader->load('fragment_renderer.xml');
8795

88-
$container->addResource(new ClassExistenceResource(Application::class));
89-
if (class_exists(Application::class)) {
96+
if ($container->classExists(Application::class)) {
9097
$loader->load('console.xml');
9198
}
9299

@@ -105,16 +112,18 @@ public function load(array $configs, ContainerBuilder $container)
105112
// default in the Form and Validator component). If disabled, an identity
106113
// translator will be used and everything will still work as expected.
107114
if ($this->isConfigEnabled($container, $config['translator']) || $this->isConfigEnabled($container, $config['form']) || $this->isConfigEnabled($container, $config['validation'])) {
108-
if (!class_exists('Symfony\Component\Transl 10000 ation\Translator') && $this->isConfigEnabled($container, $config['translator'])) {
109-
throw new LogicException('Translation support cannot be enabled as the Translation component is not installed.');
110-
}
115+
if (!$container->classExists(Translator::class)) {
116+
if ($this->isConfigEnabled($container, $config['translator'])) {
117+
throw new LogicException('Translation support cannot be enabled as the Translation component is not installed.');
118+
}
111119

112-
if (!class_exists('Symfony\Component\Translation\Translator') && $this->isConfigEnabled($container, $config['form'])) {
113-
throw new LogicException('Form support cannot be enabled as the Translation component is not installed.');
114-
}
120+
if ($this->isConfigEnabled($container, $config['form'])) {
121+
throw new LogicException('Form support cannot be enabled as the Translation component is not installed.');
122+
}
115123

116-
if (!class_exists('Symfony\Component\Translation\Translator') && $this->isConfigEnabled($container, $config['validation'])) {
117-
throw new LogicException('Validation support cannot be enabled as the Translation component is not installed.');
124+
if ($this->isConfigEnabled($container, $config['validation'])) {
125+
throw new LogicException('Validation support cannot be enabled as the Translation component is not installed.');
126+
}
118127
}
119128

120129
$loader->load('identity_translator.xml');
@@ -163,23 +172,23 @@ public function load(array $configs, ContainerBuilder $container)
163172
$this->registerFormConfiguration($config, $container, $loader);
164173
$config['validation']['enabled'] = true;
165174

166-
if (!class_exists('Symfony\Component\Validator\Validation')) {
175+
if (!$container->classExists(Validation::class)) {
167176
throw new LogicException('The Validator component is required to use the Form component.');
168177
}
169178
}
170179

171180
$this->registerSecurityCsrfConfiguration($config['csrf_protection'], $container, $loader);
172181

173182
if ($this->isConfigEnabled($container, $config['assets'])) {
174-
if (!class_exists('Symfony\Component\Asset\Package')) {
183+
if (!$container->classExists(Package::class)) {
175184
throw new LogicException('Asset support cannot be enabled as the Asset component is not installed.');
176185
}
177186

178187
$this->registerAssetsConfiguration($config['assets'], $container, $loader);
179188
}
180189

181190
if ($this->isConfigEnabled($container, $config['templating'])) {
182-
if (!class_exists('Symfony\Component\Templating\PhpEngine')) {
191+
if (!$container->classExists(PhpEngine::class)) {
183192
throw new LogicException('Templating support cannot be enabled as the Templating component is not installed.');
184193
}
185194

@@ -525,7 +534,7 @@ private function registerDebugConfiguration(array $config, ContainerBuilder $con
525534
$definition->replaceArgument(4, $debug);
526535
$definition->replaceArgument(6, $debug);
527536

528-
if ($debug && class_exists(DebugProcessor::class)) {
537+
if ($debug && $container->classExists(DebugProcessor::class)) {
529538
$definition = new Definition(DebugProcessor::class);
530539
$definition->setPublic(false);
531540
$container->setDefinition('debug.log_processor', $definition);
@@ -860,18 +869,18 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
860869

861870
// Discover translation directories
862871
$dirs = array();
863-
if (class_exists('Symfony\Component\Validator\Validation')) {
864-
$r = new \ReflectionClass('Symfony\Component\Validator\Validation');
872+
if ($container->classExists(Validation::class)) {
873+
$r = new \ReflectionClass(Validation::class);
865874

866875
$dirs[] = dirname($r->getFileName()).'/Resources/translations';
867876
}
868-
if (class_exists('Symfony\Component\Form\Form')) {
869-
$r = new \ReflectionClass('Symfony\Component\Form\Form');
877+
if ($container->classExists(Form::class)) {
878+
$r = new \ReflectionClass(Form::class);
870879

871880
$dirs[] = dirname($r->getFileName()).'/Resources/translations';
872881
}
873-
if (class_exists('Symfony\Component\Security\Core\Exception\AuthenticationException')) {
874-
$r = new \ReflectionClass('Symfony\Component\Security\Core\Exception\AuthenticationException');
882+
if ($container->classExists(AuthenticationException::class)) {
883+
$r = new \ReflectionClass(AuthenticationException::class);
875884

876885
$dirs[] = dirname(dirname($r->getFileName())).'/Resources/translations';
877886
}
@@ -944,7 +953,7 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
944953
return;
945954
}
946955

947-
if (!class_exists('Symfony\Component\Validator\Validation')) {
956+
if (!$container->classExists(Validation::class)) {
948957
throw new LogicException('Validation support cannot be enabled as the Validator component is not installed.');
949958
}
950959

@@ -999,8 +1008,8 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
9991008

10001009
private function getValidatorMappingFiles(ContainerBuilder $container, array &$files)
10011010
{
1002-
if (interface_exists('Symfony\Component\Form\FormInterface')) {
1003-
$reflClass = new \ReflectionClass('Symfony\Component\Form\FormInterface');
1011+
if ($container->classExists(FormInterface::class)) {
1012+
$reflClass = new \ReflectionClass(FormInterface::class);
10041013
$files['xml'][] = $file = dirname($reflClass->getFileName()).'/Resources/config/validation.xml';
10051014
$container->addResource(new FileResource($file));
10061015
}
@@ -1057,7 +1066,7 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
10571066
return;
10581067
}
10591068

1060-
if (!class_exists('Doctrine\Common\Annotations\Annotation')) {
1069+
if (!$container->classExists(Annotation::class)) {
10611070
throw new LogicException('Annotations cannot be enabled as the Doctrine Annotation library is not installed.');
10621071
}
10631072

@@ -1129,7 +1138,7 @@ private function registerSecurityCsrfConfiguration(array $config, ContainerBuild
11291138
return;
11301139
}
11311140

1132-
if (!class_exists('Symfony\Component\Security\Csrf\CsrfToken')) {
1141+
if (!$container->classExists('Symfony\Component\Security\Csrf\CsrfToken')) {
11331142
throw new LogicException('CSRF support cannot be enabled as the Security CSRF component is not installed.');
11341143
}
11351144

@@ -1150,34 +1159,34 @@ private function registerSecurityCsrfConfiguration(array $config, ContainerBuild
11501159
*/
11511160
private function registerSerializerConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
11521161
{
1153-
if (class_exists('Symfony\Component\Serializer\Normalizer\DataUriNormalizer')) {
1162+
if ($container->classExists(DataUriNormalizer::class)) {
11541163
// Run after serializer.normalizer.object
11551164
$definition = $container->register('serializer.normalizer.data_uri', DataUriNormalizer::class);
11561165
$definition->setPublic(false);
11571166
$definition->addTag('serializer.normalizer', array('priority' => -920));
11581167
}
11591168

1160-
if (class_exists('Symfony\Component\Serializer\Normalizer\DateTimeNormalizer')) {
1169+
if ($container->classExists(DateTimeNormalizer::class)) {
11611170
// Run before serializer.normalizer.object
11621171
$definition = $container->register('serializer.normalizer.datetime', DateTimeNormalizer::class);
11631172
$definition->setPublic(false);
11641173
$definition->addTag('serializer.normalizer', array('priority' => -910));
11651174
}
11661175

1167-
if (class_exists('Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer')) {
1176+
if ($container->classExists(JsonSerializableNormalizer::class)) {
11681177
// Run before serializer.normalizer.object
11691178
$definition = $container->register('serializer.normalizer.json_serializable', JsonSerializableNormalizer::class);
11701179
$definition->setPublic(false);
11711180
$definition->addTag('serializer.normalizer', array('priority' => -900));
11721181
}
11731182

1174-
if (class_exists(YamlEncoder::class) && defined('Symfony\Component\Yaml\Yaml::DUMP_OBJECT')) {
1183+
if ($container->classExists(YamlEncoder::class) && defined('Symfony\Component\Yaml\Yaml::DUMP_OBJECT')) {
11751184
$definition = $container->register('serializer.encoder.yaml', YamlEncoder::class);
11761185
$definition->setPublic(false);
11771186
$definition->addTag('serializer.encoder');
11781187
}
11791188

1180-
if (class_exists(CsvEncoder::class)) {
1189+
if ($container->classExists(CsvEncoder::class)) {
11811190
$definition = $container->register('serializer.encoder.csv', CsvEncoder::class);
11821191
$definition->setPublic(false);
11831192
$definition->addTag('serializer.encoder');
@@ -1252,7 +1261,7 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
12521261
$container->getDefinition('serializer.mapping.class_metadata_factory')->replaceArgument(
12531262
1, new Reference($config['cache'])
12541263
);
1255-
} elseif (!$container->getParameter('kernel.debug') && class_exists(CacheClassMetadataFactory::class)) {
1264+
} elseif (!$container->getParameter('kernel.debug') && $container->classExists(CacheClassMetadataFactory::class)) {
12561265
$cacheMetadataFactory = new Definition(
12571266
CacheClassMetadataFactory::class,
12581267
array(
@@ -1282,7 +1291,7 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild
12821291
{
12831292
$loader->load('property_info.xml');
12841293

1285-
if (interface_exists('phpDocumentor\Reflection\DocBlockFactoryInterface')) {
1294+
if ($container->classExists(DocBlockFactoryInterface::class)) {
12861295
$definition = $container->register('property_info.php_doc_extractor', 'Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor');
12871296
$definition->addTag('property_info.description_extractor', array('priority' = 10000 > -1000));
12881297
$definition->addTag('property_info.type_extractor', array('priority' => -1001));
@@ -1324,7 +1333,7 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
13241333
$container->setDefinition($name, $definition);
13251334
}
13261335

1327-
if (method_exists(PropertyAccessor::class, 'createCache')) {
1336+
if ($container->classExists(PropertyAccessor::class) && method_exists(PropertyAccessor::class, 'createCache')) {
13281337
$propertyAccessDefinition = $container->register('cache.property_access', AdapterInterface::class);
13291338
$propertyAccessDefinition->setPublic(false);
13301339
$propertyAccessDefinition->setFactory(array(PropertyAccessor::class, 'createCache'));

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Symfony\Component\DependencyInjection\ContainerBuilder;
2222
use Symfony\Component\DependencyInjection\Reference;
2323
use Symfony\Component\Config\FileLocator;
24+
use Symfony\Component\ExpressionLanguage\Expression;
25+
use Symfony\Component\Security\Acl\Model\AclInterface;
2426
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
2527

2628
/**
@@ -70,7 +72,7 @@ public function load(array $configs, ContainerBuilder $container)
7072
$loader->load('security_debug.xml');
7173
}
7274

73-
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
75+
if (!$container->classExists(Expression::class)) {
7476
$container->removeDefinition('security.expression_language');
7577
$container->removeDefinition('security.access.expression_voter');
7678
}
@@ -121,7 +123,7 @@ public function load(array $configs, ContainerBuilder $container)
121123

122124
private function aclLoad($config, ContainerBuilder $container)
123125
{
124-
if (!interface_exists('Symfony\Component\Security\Acl\Model\AclInterface')) {
126+
if (!$container->classExists(AclInterface::class)) {
125127
throw new \LogicException('You must install symfony/security-acl in order to use the ACL functionality.');
126128
}
127129

@@ -643,7 +645,7 @@ private function createExpression($container, $expression)
643645
->register($id, 'Symfony\Component\ExpressionLanguage\SerializedParsedExpression')
644646
->setPublic(false)
645647
->addArgument($expression)
646-
->addArgument(serialize($this->getExpressionLanguage()->parse($expression, array('token', 'user', 'object', 'roles', 'request', 'trust_resolver'))->getNodes()))
648+
->addArgument(serialize($this->getExpressionLanguage($container)->parse($expression, array('token', 'user', 'object', 'roles', 'request', 'trust_resolver'))->getNodes()))
647649
;
648650

649651
return $this->expressions[$id] = new Reference($id);
@@ -708,10 +710,10 @@ public function getConfiguration(array $config, ContainerBuilder $container)
708710
return new MainConfiguration($this->factories, $this->userProviderFactories);
709711
}
710712

711-
private function getExpressionLanguage()
713+
private function getExpressionLanguage($container)
712714
{
713715
if (null === $this->expressionLanguage) {
714-
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
716+
if (!$container->hasDefinition('security.expression_language')) {
715717
throw new \RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
716718
}
717719
$this->expressionLanguage = new ExpressionLanguage();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;
1313

14+
use Symfony\Component\Debug\Exception\FlattenException;
1415
use Symfony\Component\DependencyInjection\ContainerBuilder;
1516
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1618

1719
/**
1820
* Registers the Twig exception listener if Twig is registered as a templating engine.
@@ -28,7 +30,7 @@ public function process(ContainerBuilder $container)
2830
}
2931

3032
// register the exception controller only if Twig is enabled and required dependencies do exist
31-
if (!class_exists('Symfony\Component\Debug\Exception\FlattenException') || !interface_exists('Symfony\Component\EventDispatcher\EventSubscriberInterface')) {
33+
if (!$container->classExists(FlattenException::class) || !$container->classExists(EventSubscriberInterface::class)) {
3234
$container->removeDefinition('twig.exception_listener');
3335
} elseif ($container->hasParameter('templating.engines')) {
3436
$engines = $container->getParameter('templating.engines');

0 commit comments

Comments
 (0)
0