Skip to content

Commit 994a7ef

Browse files
[DependencyInjection] Add ContainerBuilder::willBeAvailable() to help with conditional configuration
1 parent 1b7375f commit 994a7ef

File tree

3 files changed

+121
-89
lines changed

3 files changed

+121
-89
lines changed

DependencyInjection/Configuration.php

Lines changed: 67 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
2222
use Symfony\Component\Config\Definition\ConfigurationInterface;
2323
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
24+
use Symfony\Component\DependencyInjection\ContainerBuilder;
2425
use Symfony\Component\DependencyInjection\Exception\LogicException;
2526
use Symfony\Component\Form\Form;
2627
use Symfony\Component\HttpClient\HttpClient;
@@ -30,6 +31,7 @@
3031
use Symfony\Component\Mailer\Mailer;
3132
use Symfony\Component\Messenger\MessageBusInterface;
3233
use Symfony\Component\Notifier\Notifier;
34+
use Symfony\Component\PropertyAccess\PropertyAccessor;
3335
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
3436
use Symfony\Component\RateLimiter\Policy\TokenBucketLimiter;
3537
use Symfony\Component\Serializer\Serializer;
@@ -108,8 +110,19 @@ public function getConfigTreeBuilder()
108110
->end()
109111
;
110112

113+
$willBeAvailable = static function (string $package, string $class, string $parentPackage = null) {
114+
$parentPackages = (array) $parentPackage;
115+
$parentPackages[] = 'symfony/framework-bundle';
116+
117+
return ContainerBuilder::willBeAvailable($package, $class, $parentPackages);
118+
};
119+
120+
$enableIfStandalone = static function (string $package, string $class) use ($willBeAvailable) {
121+
return !class_exists(FullStack::class) && $willBeAvailable($package, $class) ? 'canBeDisabled' : 'canBeEnabled';
122+
};
123+
111124
$this->addCsrfSection($rootNode);
112-
$this->addFormSection($rootNode);
125+
$this->addFormSection($rootNode, $enableIfStandalone);
113126
$this->addHttpCacheSection($rootNode);
114127
$this->addEsiSection($rootNode);
115128
$this->addSsiSection($rootNode);
@@ -119,25 +132,25 @@ public function getConfigTreeBuilder()
119132
$this->addRouterSection($rootNode);
120133
$this->addSessionSection($rootNode);
121134
$this->addRequestSection($rootNode);
122-
$this->addAssetsSection($rootNode);
123-
$this->addTranslatorSection($rootNode);
124-
$this->addValidationSection($rootNode);
125-
$this->addAnnotationsSection($rootNode);
126-
$this->addSerializerSection($rootNode);
127-
$this->addPropertyAccessSection($rootNode);
128-
$this->addPropertyInfoSection($rootNode);
129-
$this->addCacheSection($rootNode);
135+
$this->addAssetsSection($rootNode, $enableIfStandalone);
136+
$this->addTranslatorSection($rootNode, $enableIfStandalone);
137+
$this->addValidationSection($rootNode, $enableIfStandalone, $willBeAvailable);
138+
$this->addAnnotationsSection($rootNode, $willBeAvailable);
139+
$this->addSerializerSection($rootNode, $enableIfStandalone, $willBeAvailable);
140+
$this->addPropertyAccessSection($rootNode, $willBeAvailable);
141+
$this->addPropertyInfoSection($rootNode, $enableIfStandalone);
142+
$this->addCacheSection($rootNode, $willBeAvailable);
130143
$this->addPhpErrorsSection($rootNode);
131-
$this->addWebLinkSection($rootNode);
132-
$this->addLockSection($rootNode);
133-
$this->addMessengerSection($rootNode);
144+
$this->addWebLinkSection($rootNode, $enableIfStandalone);
145+
$this->addLockSection($rootNode, $enableIfStandalone);
146+
$this->addMessengerSection($rootNode, $enableIfStandalone);
134147
$this->addRobotsIndexSection($rootNode);
135-
$this->addHttpClientSection($rootNode);
136-
$this->addMailerSection($rootNode);
148+
$this->addHttpClientSection($rootNode, $enableIfStandalone);
149+
$this->addMailerSection($rootNode, $enableIfStandalone);
137150
$this->addSecretsSection($rootNode);
138-
$this->addNotifierSection($rootNode);
139-
$this->addRateLimiterSection($rootNode);
140-
$this->addUidSection($rootNode);
151+
$this->addNotifierSection($rootNode, $enableIfStandalone);
152+
$this->addRateLimiterSection($rootNode, $enableIfStandalone);
153+
$this->addUidSection($rootNode, $enableIfStandalone);
141154

142155
return $treeBuilder;
143156
}
@@ -176,13 +189,13 @@ private function addCsrfSection(ArrayNodeDefinition $rootNode)
176189
;
177190
}
178191

179-
private function addFormSection(ArrayNodeDefinition $rootNode)
192+
private function addFormSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
180193
{
181194
$rootNode
182195
->children()
183196
->arrayNode('form')
184197
->info('form configuration')
185-
->{!class_exists(FullStack::class) && class_exists(Form::class) ? 'canBeDisabled' : 'canBeEnabled'}()
198+
->{$enableIfStandalone('symfony/form', Form::class)}()
186199
->children()
187200
->arrayNode('csrf_protection')
188201
->treatFalseLike(['enabled' => false])
@@ -675,13 +688,13 @@ private function addRequestSection(ArrayNodeDefinition $rootNode)
675688
;
676689
}
677690

678-
private function addAssetsSection(ArrayNodeDefinition $rootNode)
691+
private function addAssetsSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
679692
{
680693
$rootNode
681694
->children()
682695
->arrayNode('assets')
683696
->info('assets configuration')
684-
->{!class_exists(FullStack::class) && class_exists(Package::class) ? 'canBeDisabled' : 'canBeEnabled'}()
697+
->{$enableIfStandalone('symfony/asset', Package::class)}()
685698
->fixXmlConfig('base_url')
686699
->children()
687700
->scalarNode('version_strategy')->defaultNull()->end()
@@ -763,13 +776,13 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
763776
;
764777
}
765778

766-
private function addTranslatorSection(ArrayNodeDefinition $rootNode)
779+
private function addTranslatorSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
767780
{
768781
$rootNode
769782
->children()
770783
->arrayNode('translator')
771784
->info('translator configuration')
772-
->{!class_exists(FullStack::class) && class_exists(Translator::class) ? 'canBeDisabled' : 'canBeEnabled'}()
785+
->{$enableIfStandalone('symfony/translation', Translator::class)}()
773786
->fixXmlConfig('fallback')
774787
->fixXmlConfig('path')
775788
->fixXmlConfig('enabled_locale')
@@ -816,16 +829,16 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
816829
;
817830
}
818831

819-
private function addValidationSection(ArrayNodeDefinition $rootNode)
832+
private function addValidationSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone, callable $willBeAvailable)
820833
{
821834
$rootNode
822835
->children()
823836
->arrayNode('validation')
824837
->info('validation configuration')
825-
->{!class_exists(FullStack::class) && class_exists(Validation::class) ? 'canBeDisabled' : 'canBeEnabled'}()
838+
->{$enableIfStandalone('symfony/validator', Validation::class)}()
826839
->children()
827840
->scalarNode('cache')->end()
828-
->booleanNode('enable_annotations')->{!class_exists(FullStack::class) && class_exists(Annotation::class) ? 'defaultTrue' : 'defaultFalse'}()->end()
841+
->booleanNode('enable_annotations')->{!class_exists(FullStack::class) && $willBeAvailable('doctrine/annotations', Annotation::class, 'symfony/validator') ? 'defaultTrue' : 'defaultFalse'}()->end()
829842
->arrayNode('static_method')
830843
->defaultValue(['loadValidatorMetadata'])
831844
->prototype('scalar')->end()
@@ -906,15 +919,15 @@ private function addValidationSection(ArrayNodeDefinition $rootNode)
906919
;
907920
}
908921

909-
private function addAnnotationsSection(ArrayNodeDefinition $rootNode)
922+
private function addAnnotationsSection(ArrayNodeDefinition $rootNode, callable $willBeAvailable)
910923
{
911924
$rootNode
912925
->children()
913926
->arrayNode('annotations')
914927
->info('annotation configuration')
915-
->{class_exists(Annotation::class) ? 'canBeDisabled' : 'canBeEnabled'}()
928+
->{$willBeAvailable('doctrine/annotations', Annotation::class) ? 'canBeDisabled' : 'canBeEnabled'}()
916929
->children()
917-
->scalarNode('cache')->defaultValue(interface_exists(Cache::class) ? 'php_array' : 'none')->end()
930+
->scalarNode('cache')->defaultValue($willBeAvailable('doctrine/cache', Cache::class, 'doctrine/annotation') ? 'php_array' : 'none')->end()
918931
->scalarNode('file_cache_dir')->defaultValue('%kernel.cache_dir%/annotations')->end()
919932
->booleanNode('debug')->defaultValue($this->debug)->end()
920933
->end()
@@ -923,15 +936,15 @@ private function addAnnotationsSection(ArrayNodeDefinition $rootNode)
923936
;
924937
}
925938

926-
private function addSerializerSection(ArrayNodeDefinition $rootNode)
939+
private function addSerializerSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone, $willBeAvailable)
927940
{
928941
$rootNode
929942
->children()
930943
->arrayNode('serializer')
931944
->info('serializer configuration')
932-
->{!class_exists(FullStack::class) && class_exists(Serializer::class) ? 'canBeDisabled' : 'canBeEnabled'}()
945+
->{$enableIfStandalone('symfony/serializer', Serializer::class)}()
933946
->children()
934-
->booleanNode('enable_annotations')->{!class_exists(FullStack::class) && class_exists(Annotation::class) ? 'defaultTrue' : 'defaultFalse'}()->end()
947+
->booleanNode('enable_annotations')->{!class_exists(FullStack::class) && $willBeAvailable('doctrine/annotations', Annotation::class, 'symfony/serializer') ? 'defaultTrue' : 'defaultFalse'}()->end()
935948
->scalarNode('name_converter')->end()
936949
->scalarNode('circular_reference_handler')->end()
937950
->scalarNode('max_depth_handler')->end()
@@ -950,13 +963,14 @@ private function addSerializerSection(ArrayNodeDefinition $rootNode)
950963
;
951964
}
952965

953-
private function addPropertyAccessSection(ArrayNodeDefinition $rootNode)
966+
private function addPropertyAccessSection(ArrayNodeDefinition $rootNode, callable $willBeAvailable)
954967
{
955968
$rootNode
956969
->children()
957970
->arrayNode('property_access')
958971
->addDefaultsIfNotSet()
959972
->info('Property access configuration')
973+
->{$willBeAvailable('symfony/property-access', PropertyAccessor::class) ? 'canBeDisabled' : 'canBeEnabled'}()
960974
->children()
961975
->booleanNode('magic_call')->defaultFalse()->end()
962976
->booleanNode('magic_get')->defaultTrue()->end()
@@ -969,19 +983,19 @@ private function addPropertyAccessSection(ArrayNodeDefinition $rootNode)
969983
;
970984
}
971985

972-
private function addPropertyInfoSection(ArrayNodeDefinition $rootNode)
986+
private function addPropertyInfoSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
973987
{
974988
$rootNode
975989
->children()
976990
->arrayNode('property_info')
977991
->info('Property info configuration')
978-
->{!class_exists(FullStack::class) && interface_exists(PropertyInfoExtractorInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
992+
->{$enableIfStandalone('symfony/property-info', PropertyInfoExtractorInterface::class)}()
979993
->end()
980994
->end()
981995
;
982996
}
983997

984-
private function addCacheSection(ArrayNodeDefinition $rootNode)
998+
private function addCacheSection(ArrayNodeDefinition $rootNode, callable $willBeAvailable)
985999
{
9861000
$rootNode
9871001
->children()
@@ -1008,7 +1022,7 @@ private function addCacheSection(ArrayNodeDefinition $rootNode)
10081022
->scalarNode('default_psr6_provider')->end()
10091023
->scalarNode('default_redis_provider')->defaultValue('redis://localhost')->end()
10101024
->scalarNode('default_memcached_provider')->defaultValue('memcached://localhost')->end()
1011-
->scalarNode('default_pdo_provider')->defaultValue(class_exists(Connection::class) ? 'database_connection' : null)->end()
1025+
->scalarNode('default_pdo_provider')->defaultValue($willBeAvailable('doctrine/dbal', Connection::class) ? 'database_connection' : null)->end()
10121026
->arrayNode('pools')
10131027
->useAttributeAsKey('name')
10141028
->prototype('array')
@@ -1117,13 +1131,13 @@ private function addPhpErrorsSection(ArrayNodeDefinition $rootNode)
11171131
;
11181132
}
11191133

1120-
private function addLockSection(ArrayNodeDefinition $rootNode)
1134+
private function addLockSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
11211135
{
11221136
$rootNode
11231137
->children()
11241138
->arrayNode('lock')
11251139
->info('Lock configuration')
1126-
->{!class_exists(FullStack::class) && class_exists(Lock::class) ? 'canBeDisabled' : 'canBeEnabled'}()
1140+
->{$enableIfStandalone('symfony/lock', Lock::class)}()
11271141
->beforeNormalization()
11281142
->ifString()->then(function ($v) { return ['enabled' => true, 'resources' => $v]; })
11291143
->end()
@@ -1179,25 +1193,25 @@ private function addLockSection(ArrayNodeDefinition $rootNode)
11791193
;
11801194
}
11811195

1182-
private function addWebLinkSection(ArrayNodeDefinition $rootNode)
1196+
private function addWebLinkSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
11831197
{
11841198
$rootNode
11851199
->children()
11861200
->arrayNode('web_link')
11871201
->info('web links configuration')
1188-
->{!class_exists(FullStack::class) && class_exists(HttpHeaderSerializer::class) ? 'canBeDisabled' : 'canBeEnabled'}()
1202+
->{$enableIfStandalone('symfony/weblink', HttpHeaderSerializer::class)}()
11891203
->end()
11901204
->end()
11911205
;
11921206
}
11931207

1194-
private function addMessengerSection(ArrayNodeDefinition $rootNode)
1208+
private function addMessengerSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
11951209
{
11961210
$rootNode
11971211
->children()
11981212
->arrayNode('messenger')
11991213
->info('Messenger configuration')
1200-
->{!class_exists(FullStack::class) && interface_exists(MessageBusInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
1214+
->{$enableIfStandalone('symfony/messenger', MessageBusInterface::class)}()
12011215
->fixXmlConfig('transport')
12021216
->fixXmlConfig('bus', 'buses')
12031217
->validate()
@@ -1392,13 +1406,13 @@ private function addRobotsIndexSection(ArrayNodeDefinition $rootNode)
13921406
;
13931407
}
13941408

1395-
private function addHttpClientSection(ArrayNodeDefinition $rootNode)
1409+
private function addHttpClientSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
13961410
{
13971411
$rootNode
13981412
->children()
13991413
->arrayNode('http_client')
14001414
->info('HTTP Client configuration')
1401-
->{!class_exists(FullStack::class) && class_exists(HttpClient::class) ? 'canBeDisabled' : 'canBeEnabled'}()
1415+
->{$enableIfStandalone('symfony/http-client', HttpClient::class)}()
14021416
->fixXmlConfig('scoped_client')
14031417
->beforeNormalization()
14041418
->always(function ($config) {
@@ -1728,13 +1742,13 @@ private function addHttpClientRetrySection()
17281742
;
17291743
}
17301744

1731-
private function addMailerSection(ArrayNodeDefinition $rootNode)
1745+
private function addMailerSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
17321746
{
17331747
$rootNode
17341748
->children()
17351749
->arrayNode('mailer')
17361750
->info('Mailer configuration')
1737-
->{!class_exists(FullStack::class) && class_exists(Mailer::class) ? 'canBeDisabled' : 'canBeEnabled'}()
1751+
->{$enableIfStandalone('symfony/mailer', Mailer::class)}()
17381752
->validate()
17391753
->ifTrue(function ($v) { return isset($v['dsn']) && \count($v['transports']); })
17401754
->thenInvalid('"dsn" and "transports" cannot be used together.')
@@ -1784,13 +1798,13 @@ private function addMailerSection(ArrayNodeDefinition $rootNode)
17841798
;
17851799
}
17861800

1787-
private function addNotifierSection(ArrayNodeDefinition $rootNode)
1801+
private function addNotifierSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
17881802
{
17891803
$rootNode
17901804
->children()
17911805
->arrayNode('notifier')
17921806
->info('Notifier configuration')
1793-
->{!class_exists(FullStack::class) && class_exists(Notifier::class) ? 'canBeDisabled' : 'canBeEnabled'}()
1807+
->{$enableIfStandalone('symfony/notifier', Notifier::class)}()
17941808
->fixXmlConfig('chatter_transport')
17951809
->children()
17961810
->arrayNode('chatter_transports')
@@ -1833,13 +1847,13 @@ private function addNotifierSection(ArrayNodeDefinition $rootNode)
18331847
;
18341848
}
18351849

1836-
private function addRateLimiterSection(ArrayNodeDefinition $rootNode)
1850+
private function addRateLimiterSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
18371851
{
18381852
$rootNode
18391853
->children()
18401854
->arrayNode('rate_limiter')
18411855
->info('Rate limiter configuration')
1842-
->{!class_exists(FullStack::class) && class_exists(TokenBucketLimiter::class) ? 'canBeDisabled' : 'canBeEnabled'}()
1856+
->{$enableIfStandalone('symfony/rate-limiter', TokenBucketLimiter::class)}()
18431857
->fixXmlConfig('limiter')
18441858
->beforeNormalization()
18451859
->ifTrue(function ($v) { return \is_array($v) && !isset($v['limiters']) && !isset($v['limiter']); })
@@ -1901,13 +1915,13 @@ private function addRateLimiterSection(ArrayNodeDefinition $rootNode)
19011915
;
19021916
}
19031917

1904-
private function addUidSection(ArrayNodeDefinition $rootNode)
1918+
private function addUidSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
19051919
{
19061920
$rootNode
19071921
->children()
19081922
->arrayNode('uid')
19091923
->info('Uid configuration')
1910-
->{class_exists(UuidFactory::class) ? 'canBeDisabled' : 'canBeEnabled'}()
1924+
->{$enableIfStandalone('symfony/uid', UuidFactory::class)}()
19111925
->addDefaultsIfNotSet()
19121926
->children()
19131927
->enumNode('default_uuid_version')

0 commit comments

Comments
 (0)