Dieses Blog durchsuchen

Montag, 31. Oktober 2016

symfony 3: get bundleconfiguration from everywhere. Authenticationprovider

In some cases you need to access your bundleconfiguration in contexts, where you have no container, or the container does not contain the configuration, because you are in a firewall livecycle like in the authentication provider context.

So there is hope. After 2 hours dumping arround, I found a working solution.

We simply have to install a Bundleextension in your bundle and implement the "PrependExtensionInterface". This one allows to prepend configurations.


Step 1) Add a BundleExtension to your bundle like that:
<?php
namespace Mittax\WsseBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
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;
use Symfony\Component\Yaml\Yaml;
/**
* This is the class that loads and manages your bundle configuration.
*
* @link http://symfony.com/doc/current/cookbook/bundles/extension.html
*/
class MittaxWsseExtension extends Extension implements PrependExtensionInterface, CompilerPassInterface
{
/**
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
// ... do something during the compilation
}
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$container->setParameter('mittax.wsse.salt', $config['salt']);
}
public function prepend(ContainerBuilder $container)
{
$config = Yaml::parse(file_get_contents(__DIR__.'/../Resources/config/config.yml'));
foreach ($config as $key => $configuration) {
$container->setParameter('mittax.wsse.salt', $configuration['salt']);
$container->prependExtensionConfig($key, $configuration);
}
}
}


As you can see, there is a prepend method. This method loads your bundleconfiguration and adds the needed items to the container from which you can get the config item later. In Authenticationcontexts the bundleconfig isn`t loaded by default, so you have to do that all to inject the configitems.


Step 2) Add a config.yml to <Bundlename>/Resources/config:
mittax_wsse:
salt: cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
view raw config.yaml hosted with ❤ by GitHub

This is done to keep your bundleconfigs only in the bundlecontext. So you can later access your bundleconfig. And only that.

Step 3) Now you can get your config:
$container = new ContainerBuilder();
$extension = new MittaxWsseExtension();
$container->registerExtension($extension);
$container->loadFromExtension($extension->getAlias());
$container->compile();
$salt = $this->_getContainer()->getParameter('mittax.wsse.salt');
view raw someFile.php hosted with ❤ by GitHub


At last we have to publish our new configitems in the <BundleName>/DependencyInjection/Configuration.php
<?php
namespace Mittax\WsseBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* This is the class that validates and merges configuration from your app/config files.
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html}
*/
class Configuration implements ConfigurationInterface
{
const WSSSE_SALT = 10;
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('mittax_wsse');
// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
$rootNode
->children()
->scalarNode('salt')
->defaultValue('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e')
->isRequired()
->cannotBeEmpty()
->end()
->integerNode('lifetime')
->defaultValue(600)
->end()
->scalarNode('encoder')
->defaultValue('Mittax\WsseBundle\DependencyInjection\Security\Encoders\Sha512')
->end()
->booleanNode('preventreplayattacks')
->defaultValue(true)
->end()
->end();
return $treeBuilder;
}
}

That is it!

Keine Kommentare:

Kommentar veröffentlichen