Symfony 4.1-手动连接单个服务需要_defaults:public:true

时间:2018-12-10 00:43:26

标签: symfony dependency-injection

我有以下services.yaml文件:#此文件是配置您自己的服务的入口点。     #packages /子目录中的文件配置依赖项。

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    locale: 'en'

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false       # Allows optimizing the container by removing unused services; this also means
                            # fetching services directly from the container via $container->get() won't work.
                            # The best practice is to be explicit about your dependencies anyway.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

    App\Service\Processor\TestClauses:
        public: true


    App\Service\Processor\Factory:
        arguments:
          - 'App\Service\Processor\TestClauses'
          -
            - 'MilkProductionProcessor'

即。我很高兴能自动进行所有接线,但是这项服务需要一个数组作为输入。

除非我公开所有服务,否则这似乎不起作用。我对文档https://symfony.com/doc/4.1/service_container.html#public-versus-private-services的理解是,我只需要公开要手动注入的服务

“根服务”被注入到命令中。当我运行此命令时:

1)我得到的是services.yaml

[WARNING] Some commands could not be registered:                               

In Factory.php line 15:

  Argument 1 passed to App\Service\Processor\Factory::__construct() must impl  
  ement interface App\Service\Processor\TestClausesInterface, string given, c  
  alled in /home/jochen/projects/freshagenda/symfony/var/cache/dev/Container7  
  4x3zkp/getProcessFilesCommandService.php on line 16                          




  There are no commands defined in the "app" namespace.  

  Did you mean this?                                     
      doctrine:mapping       

2)当我使services:_defaults:public为真

它向前移动

1 个答案:

答案 0 :(得分:1)

App\ServiceThatNeedsArrayAsInput:
    arguments:
       $array: ...

其他所有东西都可以自动接线和自动配置。在ServiceThatNeedsArrayAsInput的构造函数中,您应该从参数中收到$array-与早期版本的不同之处在于,您明确地说出要绑定到services.yml

中定义的参数的变量
// ServiceThatNeedsArrayAsInput.php 
public function __construct(array $array) {} // Only array from arguments
public function __construct(array $array, AutowiredService $service) {} // Just add it here and DI will autoinject it, no need to change services.yml 

您的示例

我不完全确定您要在这里做什么,但是如果要注入自动装配的服务,则无需在services.yml中显式定义它-检查上面的示例-您只需要向构造函数添加Classname 。

App\Service\Processor\Factory:
    arguments:
      $array: ['MilkProductionProcessor']