Shattuck
Shattuck

Reputation: 2780

How to expose semantic configuration

In the process of building a third party bundle for the Symfony2 framework I have run into a problem with allowing configuration to happen in the app/config.yml file. I want to do this so users of the bundle (mostly myself) won't have to go into the bundle to make configuration changes.

My bundle Configuration.php file reads:

<?php

namespace Ms2474\AuthNetBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder() {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('authorize_net');
        $rootNode
            ->children()
                ->scalarNode('login_id')->defaultNull()->end()
                ->scalarNode('transaction_key')->defaultNull()->end()
                ->booleanNode('sandbox')->defaultValue(true)->end()
                ->scalarNode('log_file')->defaultValue(false)->end()
            ->end();

        return $treeBuilder;
    }
}


The bundle extension file (Ms2474AuthNetBundleExtension.php) reads:

<?php

namespace Ms2474\AuthNetBundle\DependencyInjection;

use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Definition\Processor;

class Ms2474AuthNetBundleExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $processor = new Processor();
        $configuration = new Configuration();
        $config = $processor->process($configuration->getConfigTree(), $configs);

        if (null === $config['sandbox']) {
            $config['sandbox'] = $container->getParameter('kernel.debug');
        }

        $container->setParameter('authorize_net.login_id', $config['login_id']);
        $container->setParameter('authorize_net.transaction_key', $config['transaction_key']);
        $container->setParameter('authorize_net.sandbox', $config['sandbox']);

        if (isset($config['log_file'])) {
            $container->setParameter('authorize_net.log_file', $config['log_file']);
        }

        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
}


Now for the problem:
When I try to add the following to the app/config.yml file:

authorize_net:
    login_id: 1234
    transaction_key: 1234
    sandbox: true
    log_file: false

I get the following two errors:

InvalidArgumentException: There is no extension able to load the configuration for "authorize_net" (in /path/to/app/config/config.yml). Looked for namespace "authorize_net", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "jms_security_extra", "jms_aop", "fos_user", "jms_serializer", "fos_rest", "stof_doctrine_extensions", "vich_uploader", "gri_user", "gri_campaign", "gri_authorized_contact", "web_profiler", "sensio_distribution"

and:

FileLoaderLoadException: Cannot import resource "/path/to/app/config/config.yml" from "/path/to/app/config/config_dev.yml".


And the question:
What am I doing wrong here? I have looked through the documentation and also compared my code to other bundles such as the FOSUserBundle.

Upvotes: 2

Views: 1262

Answers (1)

Problematic
Problematic

Reputation: 17678

First, it should be called Ms2474AuthNetExtension, not Ms2474AuthNetBundleExtension.

Second, unless you've set up a custom namespace for the extension and loaded it manually, the configuration options should be under ms2474_auth_net, not authorize_net.

Upvotes: 1

Related Questions