Skip to content

Commit c30c6d2

Browse files
committed
Add the encrypt Doctrine types automatically
1 parent daa315b commit c30c6d2

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/DependencyInjection/SidusEncryptionExtension.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
namespace Sidus\EncryptionBundle\DependencyInjection;
1212

13+
use Sidus\EncryptionBundle\Doctrine\Type\EncryptStringType;
14+
use Sidus\EncryptionBundle\Doctrine\Type\EncryptTextType;
1315
use Sidus\EncryptionBundle\Registry\EncryptionManagerRegistry;
1416
use Symfony\Component\DependencyInjection\ContainerBuilder;
1517
use Symfony\Component\Config\FileLocator;
18+
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
1619
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1720
use Symfony\Component\DependencyInjection\Loader;
1821

@@ -23,12 +26,13 @@
2326
*
2427
* @author Vincent Chalnot <[email protected]>
2528
*/
26-
class SidusEncryptionExtension extends Extension
29+
class SidusEncryptionExtension extends Extension implements PrependExtensionInterface
2730
{
2831
public function load(array $configs, ContainerBuilder $container): void
2932
{
3033
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/services'));
3134
$loader->load('encryption.yml');
35+
$loader->load('doctrine.yml');
3236
$loader->load('registry.yml');
3337
$loader->load('security.yml');
3438
$loader->load('session.yml');
@@ -40,4 +44,22 @@ public function load(array $configs, ContainerBuilder $container): void
4044
$registry->replaceArgument('$defaultCode', $config['preferred_adapter']);
4145
$container->setParameter('sidus.encryption.throw_exceptions', $config['throw_exception']);
4246
}
47+
48+
public function prepend(ContainerBuilder $container)
49+
{
50+
$doctrineConfiguration = $container->getExtensionConfig('doctrine');
51+
52+
if (empty($doctrineConfiguration['dbal'])) {
53+
$doctrineConfiguration['dbal'] = [];
54+
}
55+
56+
if (empty($doctrineConfiguration['dbal']['types'])) {
57+
$doctrineConfiguration['dbal']['types'] = [];
58+
}
59+
$doctrineConfiguration['dbal']['types'] += [
60+
'encrypt_string' => EncryptStringType::class,
61+
'encrypt_text' => EncryptTextType::class
62+
];
63+
$container->prependExtensionConfig('doctrine', $doctrineConfiguration);
64+
}
4365
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
doctrine.dbal.connection_factory:
3+
class: Sidus\EncryptionBundle\Doctrine\Connection\Factory\ConnectionFactory
4+
alias: Sidus\EncryptionBundle\Doctrine\Connection\Factory\ConnectionFactory
5+
autowire: true
6+
autoconfigure: true
7+
arguments:
8+
$typesConfig: "%doctrine.dbal.connection_factory.types%"
9+
$encryptionManager: '@Sidus\EncryptionBundle\Registry\EncryptionManagerRegistry'

0 commit comments

Comments
 (0)