From 0c8124d94a82856cfa63e3e2381fde29066b93bc Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Tue, 17 Jan 2023 13:01:01 -0500 Subject: [PATCH] Add Doctrine toggle --- src/DependencyInjection/Configuration.php | 10 +++++ src/DependencyInjection/MoneyExtension.php | 37 +++++++++---------- .../DependencyInjection/ConfigurationTest.php | 3 ++ .../MoneyExtensionTest.php | 2 - 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index d1b0ae8..b5d62b7 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -45,6 +45,16 @@ public function getConfigTreeBuilder(): TreeBuilder ->end() ->end() + ->arrayNode('doctrine') + ->addDefaultsIfNotSet() + ->children() + ->booleanNode('enabled') + ->info('Whether the Doctrine integration is enabled.') + ->defaultTrue() + ->end() + ->end() + ->end() + ->arrayNode('currencies') ->example(['EUR' => 2, 'USD' => 2]) ->scalarPrototype() diff --git a/src/DependencyInjection/MoneyExtension.php b/src/DependencyInjection/MoneyExtension.php index c02d90b..24cfdc0 100644 --- a/src/DependencyInjection/MoneyExtension.php +++ b/src/DependencyInjection/MoneyExtension.php @@ -13,6 +13,7 @@ namespace Yceruto\MoneyBundle\DependencyInjection; +use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; use Money\Currencies; use Money\Formatter\IntlLocalizedDecimalFormatter; use Money\Formatter\IntlMoneyFormatter; @@ -23,7 +24,6 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\Extension; -use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; use Symfony\Component\Form\Forms; use Twig\Environment; @@ -31,26 +31,8 @@ use Yceruto\MoneyBundle\DependencyInjection\Compiler\FormattersPass; use Yceruto\MoneyBundle\Formatter\IntlNumberFormatterFactory; -class MoneyExtension extends Extension implements PrependExtensionInterface +class MoneyExtension extends Extension { - public function prepend(ContainerBuilder $container): void - { - if ($container->hasExtension('doctrine')) { - $container->prependExtensionConfig('doctrine', [ - 'orm' => [ - 'mappings' => [ - 'Money' => [ - 'is_bundle' => false, - 'type' => 'xml', - 'dir' => \dirname(__DIR__, 2).'/config/doctrine/mapping/', - 'prefix' => 'Money', - ], - ], - ], - ]); - } - } - public function load(array $configs, ContainerBuilder $container): void { $config = $this->processConfiguration($this->getConfiguration([], $container), $configs); @@ -90,5 +72,20 @@ public function load(array $configs, ContainerBuilder $container): void if (class_exists(Environment::class) && $config['twig']['enabled']) { $loader->load('twig.php'); } + + if (class_exists(DoctrineBundle::class) && $config['doctrine']['enabled']) { + $container->prependExtensionConfig('doctrine', [ + 'orm' => [ + 'mappings' => [ + 'Money' => [ + 'is_bundle' => false, + 'type' => 'xml', + 'dir' => \dirname(__DIR__, 2).'/config/doctrine/mapping/', + 'prefix' => 'Money', + ], + ], + ], + ]); + } } } diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index 7e4241b..d9ef115 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -55,6 +55,7 @@ public function configProvider(): iterable 'output' => [ 'form' => ['enabled' => true], 'twig' => ['enabled' => true], + 'doctrine' => ['enabled' => true], 'currencies' => [], 'formatters' => [ 'intl' => [ @@ -117,6 +118,7 @@ public function configProvider(): iterable ], 'form' => ['enabled' => true], 'twig' => ['enabled' => true], + 'doctrine' => ['enabled' => true], ], ]; @@ -160,6 +162,7 @@ public function configProvider(): iterable ], 'form' => ['enabled' => true], 'twig' => ['enabled' => true], + 'doctrine' => ['enabled' => true], 'formatters' => [ 'intl' => [ 'number_locale' => 'en_US', diff --git a/tests/DependencyInjection/MoneyExtensionTest.php b/tests/DependencyInjection/MoneyExtensionTest.php index 6229a78..b88e952 100644 --- a/tests/DependencyInjection/MoneyExtensionTest.php +++ b/tests/DependencyInjection/MoneyExtensionTest.php @@ -137,8 +137,6 @@ public function testMoneyDoctrineMapping(): void $container->loadFromExtension('doctrine', [ 'dbal' => [], ]); - - (new MoneyExtension())->prepend($container); }); /** @var Registry $doctrine */