Skip to content

Commit

Permalink
Merge pull request #10 from yceruto/doctrine_toggle
Browse files Browse the repository at this point in the history
Add Doctrine toggle
  • Loading branch information
yceruto authored Jan 17, 2023
2 parents d031039 + 0c8124d commit 0d04d3a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
10 changes: 10 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
37 changes: 17 additions & 20 deletions src/DependencyInjection/MoneyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Yceruto\MoneyBundle\DependencyInjection;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Money\Currencies;
use Money\Formatter\IntlLocalizedDecimalFormatter;
use Money\Formatter\IntlMoneyFormatter;
Expand All @@ -23,34 +24,15 @@
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;
use Yceruto\MoneyBundle\DependencyInjection\Compiler\CurrenciesPass;
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);
Expand Down Expand Up @@ -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',
],
],
],
]);
}
}
}
3 changes: 3 additions & 0 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function configProvider(): iterable
'output' => [
'form' => ['enabled' => true],
'twig' => ['enabled' => true],
'doctrine' => ['enabled' => true],
'currencies' => [],
'formatters' => [
'intl' => [
Expand Down Expand Up @@ -117,6 +118,7 @@ public function configProvider(): iterable
],
'form' => ['enabled' => true],
'twig' => ['enabled' => true],
'doctrine' => ['enabled' => true],
],
];

Expand Down Expand Up @@ -160,6 +162,7 @@ public function configProvider(): iterable
],
'form' => ['enabled' => true],
'twig' => ['enabled' => true],
'doctrine' => ['enabled' => true],
'formatters' => [
'intl' => [
'number_locale' => 'en_US',
Expand Down
2 changes: 0 additions & 2 deletions tests/DependencyInjection/MoneyExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ public function testMoneyDoctrineMapping(): void
$container->loadFromExtension('doctrine', [
'dbal' => [],
]);

(new MoneyExtension())->prepend($container);
});

/** @var Registry $doctrine */
Expand Down

0 comments on commit 0d04d3a

Please sign in to comment.