如何将配置参数放入命令中

Cho*_*ord 4 php symfony symfony-3.4

我正在尝试将配置参数放入命令中。

app/config/config.yml我已经在键下添加了参数parameters

我可以在控制器中使用它$this->getParameter("PRAMETER_NAME")来手动执行一些操作。

我正在编写一个命令来通过 CRON 作业自动执行相同的操作。

我找不到如何获取命令中的参数。

fyr*_*rye 9

参数绑定

在 Symfony 3.4 或更高版本中,推荐的方法是使用AutowiringArgument Binding。允许将变量名声明“绑定”为配置文件中定义的所有服务的参数,而无需显式指定使用该参数的每个服务。

在定义命令服务和自动装配的同一配置文件中,将选项以及参数所需的变量名称添加bind到规范中。_defaults

应用程序/配置/services.yml

parameters:
    PARAMETER_NAME: 'test'

services:
    _defaults:
        bind:
            $PARAMETER_NAME: '%PARAMETER_NAME%'
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    AppBundle\:
        resource: '../../src/AppBundle/*'
        exclude: '../../src/AppBundle/{DependencyInjection,Entity,Tests}'

    # Additional Services Below Here
Run Code Online (Sandbox Code Playgroud)

之后,当将参数值指定为服务构造函数的参数时,Symfony 会自动将参数值传递给绑定变量名。

src/AppBundle/Command/MyCommand.php

namespace AppBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MyCommand extends Command
{

    private $PARAMETER_NAME;

    public function __construct($PARAMETER_NAME)
    {
        $this->PARAMETER_NAME = $PARAMETER_NAME;
        parent::__construct();
    }

    // ...

    public function execute(InputInterface $input, OutputInterface $output)
    {
        dump($this->PARAMETER_NAME);
        exit;
    }

    public static function getDefaultName()
    {
        return 'app:my_command';
    }
}
Run Code Online (Sandbox Code Playgroud)

参数注入

另一种避免需要重写的方法__construct是使用方法来注入参数值,方法是使用 扩展服务定义calls

应用程序/配置/services.yml

parameters:
    PARAMETER_NAME: 'test'

services:
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    AppBundle\:
        resource: '../../src/AppBundle/*'
        exclude: '../../src/AppBundle/{DependencyInjection,Entity,Tests}'

    AppBundle\Command\MyCommand:
        calls:
            - [setParameterName, ['%PARAMETER_NAME%']]

    # Additional Services Below Here
Run Code Online (Sandbox Code Playgroud)

src/AppBundle/Command/MyCommand.php

namespace AppBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MyCommand extends Command
{

    private $parameterName;

    // ...

    public function execute(InputInterface $input, OutputInterface $output)
    {
        dump($this->parameterName);
        exit;
    }

    public function setParameterName($value)
    {
        $this->parameterName = $value;
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,可以使用依赖项注入将 Container 或 ParameterBag 注入到命令中,使其功能与控制器类似。

强烈建议不要注入整个 Container 或 ParameterBag 。 仅注入所需的参数和服务

在以下任一示例中,请确保启用autowire和。autoconfigure

应用程序/配置/services.yml

parameters:
    PARAMETER_NAME: 'test'

services:
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    AppBundle\:
        resource: '../../src/AppBundle/*'
        exclude: '../../src/AppBundle/{DependencyInjection,Entity,Tests}'

    # Additional Services Below Here
Run Code Online (Sandbox Code Playgroud)

容器感知命令

(自 Symfony 4.2 起已弃用 - 在 Syfmony 5.0+ 中已删除)

使用的ContainerAwareCommand工作方式与参数注入类似,但不是调用setParameterName(). 启用autowiring并启用后,Symfony 将使用从 实现的autoconfig自动注入整个容器。setContainer()ContainerAwareInterface

src/AppBundle/Command/MyCommand.php

namespace AppBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MyCommand extends ContainerAwareCommand
{

    // ...

    public function execute(InputInterface $input, OutputInterface $output)
    {
        dump($this->getContainer()->getParameter('PARAMETER_NAME'));
        exit;
    }
}
Run Code Online (Sandbox Code Playgroud)

参数包注入

需要 Symfony 4.1+

要在启用依赖注入时注入ParameterBag ,请添加到服务构造函数中。autowireParameterBagInterface

src/AppBundle/Command/MyCommand.php

namespace AppBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class MyCommand extends Command
{

    private $parameterBag;

    public function __construct(ParameterBagInterface $parameterBag)
    {
         $this->parameterBag = $parameterBag;
         parent::__construct();
    }

    // ...

    public function execute(InputInterface $input, OutputInterface $output)
    {
        dump($this->parameterBag->get('PARAMETER_NAME'));
        exit;
    }

    public static function getDefaultName()
    {
        return 'app:my_command';
    }
}
Run Code Online (Sandbox Code Playgroud)