vendor/headsnet/money-bundle/src/HeadsnetMoneyBundle.php line 23

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony HeadsnetMoneyBundle.
  4.  *
  5.  * (c) Headstrong Internet Services Ltd 2022
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Headsnet\MoneyBundle;
  11. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
  12. use Doctrine\DBAL\Connection;
  13. use Doctrine\DBAL\Exception;
  14. use Doctrine\DBAL\Types\Type;
  15. use Doctrine\Persistence\ManagerRegistry;
  16. use Headsnet\MoneyBundle\Doctrine\DBAL\Types\CurrencyType;
  17. use Symfony\Component\DependencyInjection\ContainerBuilder;
  18. use Symfony\Component\DependencyInjection\ContainerInterface;
  19. use Symfony\Component\HttpKernel\Bundle\Bundle;
  20. class HeadsnetMoneyBundle extends Bundle
  21. {
  22.     public function build(ContainerBuilder $container): void
  23.     {
  24.         parent::build($container);
  25.         $this->addDoctrineMapping($container);
  26.     }
  27.     /**
  28.      * @throws Exception
  29.      */
  30.     public function boot(): void
  31.     {
  32.         $this->addCurrencyColumnType();
  33.     }
  34.     private function addDoctrineMapping(ContainerBuilder $container): void
  35.     {
  36.         $modelDir realpath(__DIR__ '/Doctrine/Mapping');
  37.         $mappings = [
  38.             $modelDir => 'Money',
  39.         ];
  40.         if (class_exists(DoctrineOrmMappingsPass::class)) {
  41.             $container->addCompilerPass(
  42.                 DoctrineOrmMappingsPass::createXmlMappingDriver(
  43.                     $mappings,
  44.                     ['doctrine.orm.entity_manager'],
  45.                     false
  46.                 )
  47.             );
  48.         }
  49.     }
  50.     /**
  51.      * @throws Exception
  52.      */
  53.     private function addCurrencyColumnType(): void
  54.     {
  55.         if (!Type::hasType('currency')) {
  56.             Type::addType('currency'CurrencyType::class);
  57.         }
  58.         /** @var ContainerInterface $container Keep PHPStan happy */
  59.         $container $this->container;
  60.         /** @var ManagerRegistry $registry */
  61.         $registry $container->get('doctrine');
  62.         /** @var Connection $connection */
  63.         foreach ($registry->getConnections() as $connection) {
  64.             $connection->getDatabasePlatform()->registerDoctrineTypeMapping('currency''currency');
  65.         }
  66.     }
  67. }