Symfony捆绑包配置服务参数

时间:2019-07-08 18:52:24

标签: php symfony

因此,我在Symfony中创建了新的Bundle,现在,我想为该Bundle进行配置。看起来像这样:

App \ DireSolutions \ TestUI \ Resources \ config \ services.yaml

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  dire_solutions.ui_test.simple_service:
    public: true
    class: App\DireSolutions\TestUI\Service\SimpleService
    arguments:
      $client_id: '123'
      $client_secret: 'secret'

App \ DireSolutions \ TestUI \ DependencyInjection \ DireSolutionsTestUIConfiguration.php

class DireSolutionsTestUIConfiguration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('dire_solutions_test_ui');

        $rootNode
            ->children()
            ->arrayNode('twitter')
            ->children()
            ->integerNode('client_id')->end()
            ->scalarNode('client_secret')->end()
            ->end()
            ->end()// twitter
            ->end();

        return $treeBuilder;
    }
}

App \ DireSolutions \ TestUI \ DependencyInjection \ DireSolutionsTestUIExtension.php

class DireSolutionsTestUIExtension extends Extension
{
    /**
     * @param array $configs
     * @param ContainerBuilder $container
     * @throws \Exception
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new YamlFileLoader(
            $container,
            new FileLocator(__DIR__ . '/../Resources/config')
        );
        $loader->load('services.yaml');

        $configuration = new DireSolutionsTestUIConfiguration();

        $config = $this->processConfiguration($configuration, $configs);

        /** @var Definition $definition */
        $definition = $container->getDefinition('dire_solutions.ui_test.simple_service');

        $definition->setArgument('$client_id', $config['twitter']['client_id']);
        $definition->setArgument('$client_secret', $config['twitter']['client_secret']);
    }

}

App \ DireSolutions \ TestUI \ Service \ SimpleService.php

class SimpleService
{

    /**
     * SimpleService constructor.
     * @param ParameterBagInterface $params
     * @param string $client_id
     * @param string $client_secret
     */
    public function __construct($client_id, $client_secret)
    {
        dump($client_id);
        dump($client_secret);
        exit;
    }
}

使用此配置,我得到Cannot autowire service "App\DireSolutions\TestUI\Service\SimpleService": argument "$client_id" of method "__construct()" has no type-hint, you should configure its value explicitly.

无论我进行什么更改,都无法手动将这些参数连接到服务中。

1 个答案:

答案 0 :(得分:0)

将您的services.yaml更改为

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  App\DireSolutions\TestUI\Service\SimpleService:
    arguments:
      $client_id: "123"
      $client_secret: "secret"

如果这样不起作用,您可以尝试使用以下方式显式绑定参数

services:
      _defaults:
        autowire: true
        autoconfigure: true
        public: false
        bind: 
          $client_id: "123"
          $client_secret: "secret"

bind甚至允许使用int,string等php内置类型。

最后一个选择是使用别名显式定义服务,这在某些情况下可能会有所帮助。

还要更改您的

$definition = $container>getDefinition('dire_solutions.ui_test.simple_service');

$definition = $container>getDefinition('App\DireSolutions\TestUI\Service\SimpleService');

或创建一个别名,如上所述。