2012-03-21 61 views
2

在爲Symfony2框架構建第三方bundle的過程中,我遇到了允許配置在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'); 
    } 
} 


現在的問題:
當我嘗試將以下內容添加到應用/ confi g.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

首先,它應該被稱爲Ms2474AuthNetExtension,而不是Ms2474AuthNetBundleExtension。其次,除非您爲擴展名設置了自定義名稱空間並手動加載,否則配置選項應該在ms2474_auth_net之下,而不是authorize_net