From c30c6d2674a1ed2f2e710d40104253791f594e3e Mon Sep 17 00:00:00 2001 From: johnkrovitch Date: Fri, 27 Nov 2020 17:00:39 +0100 Subject: [PATCH] Add the encrypt Doctrine types automatically --- .../SidusEncryptionExtension.php | 24 ++++++++++++++++++- src/Resources/config/services/doctrine.yaml | 9 +++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/Resources/config/services/doctrine.yaml diff --git a/src/DependencyInjection/SidusEncryptionExtension.php b/src/DependencyInjection/SidusEncryptionExtension.php index 296c4d2..0c3e386 100644 --- a/src/DependencyInjection/SidusEncryptionExtension.php +++ b/src/DependencyInjection/SidusEncryptionExtension.php @@ -10,9 +10,12 @@ namespace Sidus\EncryptionBundle\DependencyInjection; +use Sidus\EncryptionBundle\Doctrine\Type\EncryptStringType; +use Sidus\EncryptionBundle\Doctrine\Type\EncryptTextType; use Sidus\EncryptionBundle\Registry\EncryptionManagerRegistry; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Loader; @@ -23,12 +26,13 @@ * * @author Vincent Chalnot */ -class SidusEncryptionExtension extends Extension +class SidusEncryptionExtension extends Extension implements PrependExtensionInterface { public function load(array $configs, ContainerBuilder $container): void { $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/services')); $loader->load('encryption.yml'); + $loader->load('doctrine.yml'); $loader->load('registry.yml'); $loader->load('security.yml'); $loader->load('session.yml'); @@ -40,4 +44,22 @@ public function load(array $configs, ContainerBuilder $container): void $registry->replaceArgument('$defaultCode', $config['preferred_adapter']); $container->setParameter('sidus.encryption.throw_exceptions', $config['throw_exception']); } + + public function prepend(ContainerBuilder $container) + { + $doctrineConfiguration = $container->getExtensionConfig('doctrine'); + + if (empty($doctrineConfiguration['dbal'])) { + $doctrineConfiguration['dbal'] = []; + } + + if (empty($doctrineConfiguration['dbal']['types'])) { + $doctrineConfiguration['dbal']['types'] = []; + } + $doctrineConfiguration['dbal']['types'] += [ + 'encrypt_string' => EncryptStringType::class, + 'encrypt_text' => EncryptTextType::class + ]; + $container->prependExtensionConfig('doctrine', $doctrineConfiguration); + } } diff --git a/src/Resources/config/services/doctrine.yaml b/src/Resources/config/services/doctrine.yaml new file mode 100644 index 0000000..9aba4e6 --- /dev/null +++ b/src/Resources/config/services/doctrine.yaml @@ -0,0 +1,9 @@ +services: + doctrine.dbal.connection_factory: + class: Sidus\EncryptionBundle\Doctrine\Connection\Factory\ConnectionFactory + alias: Sidus\EncryptionBundle\Doctrine\Connection\Factory\ConnectionFactory + autowire: true + autoconfigure: true + arguments: + $typesConfig: "%doctrine.dbal.connection_factory.types%" + $encryptionManager: '@Sidus\EncryptionBundle\Registry\EncryptionManagerRegistry'