Skip to content

Commit

Permalink
Add the encrypt Doctrine types automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkrovitch committed Nov 27, 2020
1 parent daa315b commit c30c6d2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/DependencyInjection/SidusEncryptionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,12 +26,13 @@
*
* @author Vincent Chalnot <[email protected]>
*/
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');
Expand All @@ -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);
}
}
9 changes: 9 additions & 0 deletions src/Resources/config/services/doctrine.yaml
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit c30c6d2

Please sign in to comment.