php - 如何在带有命令的独立应用程序中使用来自 symfony 的 DependencyInjection?

标签 php symfony dependency-injection symfony-console

我一直在使用 symfony/console 来制作命令并像这样注册它们,一切正常:
垃圾箱/控制台:

#!/usr/bin/env php
<?php
require_once __DIR__ . '/../vendor/autoload.php';

use App\Commands\LocalitiesCommand;
use Symfony\Component\Console\Application;

$app = new Application();
$app->add(new LocalitiesCommand(new LocalitiesGenerator()));
$app->run();
src/Commands/LocalitiesCommand.php:
<?php

declare(strict_types=1);

namespace App\Commands;

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


final class LocalitiesCommand extends Command
{
    protected static $defaultName = 'app:generate-localities';

    public function __construct(private LocalitiesGenerator $localitiesGenerator)
    {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->setDescription('Generate localities.json file')
            ->setHelp('No arguments needed.');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->localitiesGenerator->generateJsonLocalities();
        $output->writeln("File localities.json generated!");
        return Command::SUCCESS;
    }
}
现在我想用 symfony/dependency-injection 自动注入(inject)服务,我正在阅读文档并做了一些更改:
新品 垃圾箱/控制台:
#!/usr/bin/env php
<?php
require_once __DIR__ . '/../vendor/autoload.php';

use App\Commands\LocalitiesCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;

$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/src/config'));
$loader->load('services.yaml');
$container->compile();


$app = new Application();
$app->add(new LocalitiesCommand());
$app->run();
配置/服务.yaml:
services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false
但是当我实例化我的命令时,仍然要求我在构造函数中添加我的服务。为什么它不起作用?

最佳答案

首先,让我们澄清一个误解:

But still asks me to add my service in the constructor when I instantiate my command. Why is it not working?


如果您拨打 new Foo() ,那么您将不再获得 Autowiring DI 的好处。如果你想使用 Autowiring 和自动依赖注入(inject),你需要让 Symfony 为你工作。当您拨打 new ,您正在手动实例化对象,您需要自己处理 DI。
有了这个,你将如何做到这一点?

一、composer.json使用基本依赖项和自动加载器声明:
完整的目录结构最终将是这样的:
<project_dir>
├── composer.json 
├── app 
├── src/
│    ├── ConsoleCommand/
│    │       └── FooCommand.php
│    └── Text/
│          └── Reverser.php
├── config/
│    ├── services.yaml
现在,每个部分:composer.json包含所有依赖项和自动加载器的文件:
{
    "require": {
        "symfony/dependency-injection": "^5.3",
        "symfony/console": "^5.3",
        "symfony/config": "^5.3",
        "symfony/yaml": "^5.3"
    },
    "autoload": {
        "psr-4": {
            "App\\": "src"
        }
    }
}
前端 Controller 脚本,运行应用程序的文件( app ,在我的情况下):
#!/usr/bin/env php
<?php declare(strict_types=1);

use Symfony\Component;

require __DIR__ . '/vendor/autoload.php';

class App extends Component\Console\Application
{

    public function __construct(iterable $commands)
    {
        $commands = $commands instanceof Traversable ? iterator_to_array($commands) : $commands;

        foreach ($commands as $command) {
            $this->add($command);
        }

        parent::__construct();
    }
}

$container = new Component\DependencyInjection\ContainerBuilder();
$loader    = new Component\DependencyInjection\Loader\YamlFileLoader($container, new Component\Config\FileLocator(__DIR__ . '/config'));

$loader->load('services.yaml');
$container->compile();

$app = $container->get(App::class);
$app->run();
项目的服务容器配置:
# config/services.yaml
services:
  _defaults:
    autowire: true

  _instanceof:
    Symfony\Component\Console\Command\Command:
      tags: [ 'app.command' ]

  App\:
    resource: '../src/*'

  App:
    class: \App
    public: true
    arguments:
      - !tagged_iterator app.command
FooCommand类(class):
<?php declare(strict_types=1);

// src/ConsoleCommand/FooCommand.php

namespace App\ConsoleCommand;

use App\Text\Reverser;
use Symfony\Component\Console;

class FooCommand extends Console\Command\Command
{

    protected static $defaultName = 'foo';

    public function __construct(private Reverser $reverser)
    {
        parent::__construct(self::$defaultName);
    }

    protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int
    {
        $output->writeln('Foo was invoked');
        $output->writeln($this->reverser->exec('the lazy fox'));

        return self::SUCCESS;
    }
}
以上取决于App\Text\Reverser服务,DI组件会自动为我们注入(inject):
<?php declare(strict_types=1);

namespace App\Text;

class Reverser
{

    public function exec(string $in): string
    {
        return \strrev($in);
    }
}
安装和转储自动加载器后,通过执行 php app (1) 我知道 foo命令可用(2):
enter image description here
我可以执行 php app foo ,并且该命令使用其注入(inject)的依赖项正确执行:
enter image description here
一个自包含的 Symfony 控制台应用程序,具有最少的依赖项和自动依赖项注入(inject)。
(非常相似示例的所有代码, here )。

关于php - 如何在带有命令的独立应用程序中使用来自 symfony 的 DependencyInjection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68754974/

相关文章:

jquery - Symfony2 中的 Composer 创建两次相同的资源(jquery、jqueryui)

javascript - 提交后将值保留在输入表单中

php - Symfony 2 - 在 preSubmit 事件订阅者上向表单元素添加错误

c# - 在多线程应用程序中正确使用 IOC(装饰器或拦截器?)

perl - 在 Dancer2 中,保留模块的路由方法可以访问的类似服务对象的实例的首选方法是什么?

c# - DependencyInjection : Initialize class, 在被调用之前(必需)?

php - WordPress上传媒体问题 fatal error : Maximum function nesting level of '256' reached

php - 将对象数组分配给 session

javascript - 警告框(确认框)中的撇号或逗号不起作用

PHP strtotime 和 MySQL 日期格式