捆绑的交互式参数

时间:2015-08-07 12:32:58

标签: php symfony yaml bundles

我在Symfony2中开发我的第一个捆绑包,是在其构造中使用一些参数的服务,并且如果我保留这个结构,则在 AcmeBundle / Resource / config / parameters.yml <中正常工作/强>:

parameters:
    id: IDVAL
    secret: SECRETVAL

services:
    my_service:
        class: MyClass
        arguments: [%id%, %secret%]

但是我想知道是否有任何方法可以使用Interactive Management of the parameters.yml File,因为通过使用作曲家AcmeBundle / Resource / config / parameters.yml.dist来安装捆绑包而不是app / config /parameters.yml.dist

提前问候和感谢。

1 个答案:

答案 0 :(得分:1)

当您创建可重复使用的捆绑包时,我不知道如何通过作曲家知道这一点。
相反,您可以使用symfony&#39; Configuration Component根据public void Starter() { try { logsys.Enqueue("Service started"); //Setup the socket and message buffer tcpSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); int cport = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["CPORT"].ToString()); tcpSock.Bind(new IPEndPoint(IPAddress.Parse(System.Configuration.ConfigurationManager.AppSettings["CID"].ToString()), cport)); buffer = new byte[1024]; //td = new Thread(readdata); //Start listening for a new message. EndPoint newClientEP = new IPEndPoint(IPAddress.Parse(System.Configuration.ConfigurationManager.AppSettings["CID"].ToString()), cport); tcpSock.Connect(newClientEP); tcpSock.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock); //td.Start(); } catch (Exception ex) { logsys.Enqueue("GPS_Service Starter Error: " + ex.Message); } } 文件配置您的捆绑包,并使这些参数成为必需,这样当您安装捆绑包时,用户就会拥有提供这些参数。对于你的用例,它将是:

config.yml
// in YourBundle\DependencyInjection\Configuration
public function getConfigTreeBuilder(){
  $treeBuilder = new TreeBuilder();
  $rootNode = $treeBuilder->root('your_bundle_alias');
  $rootNode
    ->children()
      ->scalarNode('param1')->isRequired()->end()
      ->scalarNode('param1')->isRequired()->end()
    ->end();
 }

以这种方式编写Bundle Configuration会强制用户在// in YourBundle\DependencyInjection\YourExtension public function load(array $configs, ContainerBuilder $container){ $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); // ... // get the parameters entered in the config file, and configured above $param1 = $config['param1']; $param2 = $config['param2']; // inject them into my_service $container->getDefinition('my_service') ->setArguments(array($param1, $param2)); // ... } 文件中为您的包提供一个条目,如下所示:

config.yml