Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a way to configure the Symfony PRNG with Doctrine #91

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -51,6 +51,17 @@ public function getConfigTreeBuilder()
$this->addDbalSection($rootNode);
$this->addOrmSection($rootNode);

$rootNode
->children()
->arrayNode('prng')
->children()
->scalarNode('connection')->cannotBeEmpty()->isRequired()->end()
->scalarNode('table_name')->defaultValue('seed_table')->cannotBeEmpty()->end()
->end()
->end()
->end()
;

return $treeBuilder;
}

Expand Down
26 changes: 26 additions & 0 deletions DependencyInjection/DoctrineExtension.php
Expand Up @@ -47,6 +47,32 @@ public function load(array $configs, ContainerBuilder $container)
if (!empty($config['orm'])) {
$this->ormLoad($config['orm'], $container);
}

if (!empty($config['prng'])) {
$this->secureRandomLoad($config['prng'], $container);
}
}

protected function secureRandomLoad(array $config, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('prng.xml');

$container
->getDefinition('doctrine.security.seed_provider')
->replaceArgument(0, new Reference('doctrine.dbal.'.$config['connection'].'_connection'))
->replaceArgument(1, $config['table_name'])
;
$container->setAlias('security.prng_seed_provider', 'doctrine.security.seed_provider');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see an issue here: if you register DoctrineBundle after SecurityBundle, this will overwrite the service configured explicitly by the suer in the SecurityBundle config

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which means that you would have configured 2 different seed providers. Who would do that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe someone wants to have an implementation wrapping the Doctrine implementation ? In such case, using DoctrineBundle to configure the doctrine implementation still makes sense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, you would not use the configuration from the bundle but you would directly set the seed provider in the security configuration.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not using the configuration of DoctrineBundle would mean that you have to reimplement all services provided by DoctrineBundle when using this config.


$container
->getDefinition('doctrine.security.prng_schema_listener')
->addTag('doctrine.event_listener', array('connection' => $config['connection'], 'event' => 'postGenerateSchema', 'lazy' => true))
;
$container
->getDefinition('doctrine.security.prng_schema')
->replaceArgument(0, $config['table_name'])
;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions Resources/config/prng.xml
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="doctrine.security.seed_provider.class">Symfony\Bridge\Doctrine\Security\DoctrineSeedProvider</parameter>
<parameter key="doctrine.security.prng_schema.class">Symfony\Bridge\Doctrine\Security\PrngSchema</parameter>
<parameter key="doctrine.security.prng_schema_listener.class">Symfony\Bridge\Doctrine\Security\EventListener\PrngSchemaListener</parameter>
</parameters>

<services>
<service id="doctrine.security.seed_provider" class="%doctrine.security.seed_provider.class%">
<argument></argument><!-- Connection -->
<argument></argument><!-- Table Name -->
</service>
<service id="doctrine.security.prng_schema" class="%doctrine.security.prng_schema.class%" public="false">
<argument></argument><!-- Table Name -->
</service>
<service id="doctrine.security.prng_schema_listener" class="%doctrine.security.prng_schema_listener.class%">
<argument type="service" id="doctrine.security.prng_schema" />
</service>
</services>
</container>
6 changes: 6 additions & 0 deletions Resources/config/schema/doctrine-1.0.xsd
Expand Up @@ -10,6 +10,7 @@
<xsd:all>
<xsd:element name="dbal" type="dbal" minOccurs="0" maxOccurs="1" />
<xsd:element name="orm" type="orm" minOccurs="0" maxOccurs="1" />
<xsd:element name="prng" type="prng" minOccurs="0" maxOccurs="1" />
</xsd:all>
</xsd:complexType>
</xsd:element>
Expand Down Expand Up @@ -187,4 +188,9 @@
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>

<xsd:complexType name="prng">
<xsd:attribute name="connection" type="xsd:string" />
<xsd:attribute name="table_name" type="xsd:string" />
</xsd:complexType>
</xsd:schema>