Permalink
Browse files

Moved the overwriting of the encoder factory to a compiler pass

This removes the need to register FOSUserBundle after SecurityBundle
and make the overwriting more friendly with other bundles choosing to
overwrite it.

Closes #241
  • Loading branch information...
1 parent 7381e96 commit 0118ed495b757f10422f0eca3b49f3692fa2d2b3 @stof stof committed Jul 8, 2011
Showing with 53 additions and 3 deletions.
  1. +41 −0 DependencyInjection/Compiler/SecurityEncoderFactoryPass.php
  2. +11 −0 FOSUserBundle.php
  3. +1 −3 Resources/config/security.xml
View
41 DependencyInjection/Compiler/SecurityEncoderFactoryPass.php
@@ -0,0 +1,41 @@
+<?php
+
+/*
+ * This file is part of the FOSUserBundle package.
+ *
+ * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace FOS\UserBundle\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Alias;
+
+/**
+ * Overwrites the existing encoder factory and injects the old one in the FOSUserBundle implementation
+ *
+ * @author Christophe Coevoet <stof@notk.org>
+ */
+class SecurityEncoderFactoryPass implements CompilerPassInterface
+{
+ public function process(ContainerBuilder $container)
+ {
+ if ($container->hasAlias('security.encoder_factory')) {
+ // security.encoder_factory is an alias.
+ // Register a private alias for this service to inject it as the parent
+ $container->setAlias(new Alias('fos_user.encoder_factory.parent', false), (string) $container->getAlias('security.encoder_factory'));
+ } else {
+ // security.encoder_factory is a definition.
+ // Register it again as a private service to inject it as the parent
+ $definition = $container->getDefinition('security.encoder_factory');
+ $definition->setPublic(false);
+ $container->setDefinition('fos_user.encoder_factory.parent', $definition);
+ }
+
+ $container->setAlias('security.encoder_factory', 'fos_user.encoder_factory');
+ }
+}
View
11 FOSUserBundle.php
@@ -12,11 +12,22 @@
namespace FOS\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use FOS\UserBundle\DependencyInjection\Compiler\SecurityEncoderFactoryPass;
/**
* @author Matthieu Bontemps <matthieu@knplabs.com>
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
*/
class FOSUserBundle extends Bundle
{
+ /**
+ * {@inheritdoc}
+ */
+ public function build(ContainerBuilder $container)
+ {
+ parent::build($container);
+
+ $container->addCompilerPass(new SecurityEncoderFactoryPass());
+ }
}
View
4 Resources/config/security.xml
@@ -14,11 +14,9 @@
<argument>%security.encoder.digest.class%</argument>
<argument>%fos_user.encoder.encode_as_base64%</argument>
<argument>%fos_user.encoder.iterations%</argument>
- <argument type="service" id="security.encoder_factory.generic" />
+ <argument type="service" id="fos_user.encoder_factory.parent" />
</service>
- <service id="security.encoder_factory" alias="fos_user.encoder_factory" />
-
<service id="fos_user.security.interactive_login_listener" class="%fos_user.security.interactive_login_listener.class%">
<argument type="service" id="fos_user.user_manager" />
<tag name="kernel.event_listener" event="security.interactive_login" method="onSecurityInteractiveLogin" />

0 comments on commit 0118ed4

Please sign in to comment.