如何公开语义配置

时间:2012-03-21 23:04:03

标签: php symfony dependency-injection

在为Symfony2框架构建第三方软件包的过程中,我遇到了允许在app / config.yml文件中进行配置的问题。我想这样做,所以捆绑的用户(主要是我自己)将不必进入捆绑包进行配置更改。

我的捆绑Configuration.php文件读取:

<?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;
    }
}


包扩展文件(Ms2474AuthNetBundleExtension.php)读取:

<?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');
    }
}


现在出现问题:
当我尝试将以下内容添加到app / config.yml文件时:

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

我收到以下两个错误:

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"

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


问题是:
我在这做错了什么?我查看了documentation,并将我的代码与其他捆绑包(例如FOSUserBundle)进行了比较。

1 个答案:

答案 0 :(得分:1)

首先,它应该被称为Ms2474AuthNetExtension,而不是Ms2474AuthNetBundleExtension

其次,除非您为扩展设置了自定义命名空间并手动加载,否则配置选项应位于ms2474_auth_net下,而不是authorize_net