如何在独立应用程序中使用symfony的DependencyInjection命令?

h79rfbju  于 2023-10-23  发布在  其他
关注(0)|答案(2)|浏览(167)

我一直在使用symfony/console来制作命令并注册它们,一切都很好:

bin/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自动注入服务,我正在阅读文档并做了一些更改:
新建bin/console:

#!/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();

config/services.yaml:

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

但是当我示例化我的命令时,仍然要求我在构造函数中添加我的服务。为什么它不起作用?

lf3rwulv

lf3rwulv1#

首先,让我们澄清一个误解:
但是当我示例化我的命令时,仍然要求我在构造函数中添加我的服务。为什么它不起作用?
如果你调用new Foo(),那么你将不再获得自动连接DI的好处。如果你想使用自动连接和自动依赖注入,你需要让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"
        }
    }
}

前端控制器脚本,即运行应用程序的文件(在我的例子中是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类:

<?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组件自动为我们注入:

<?php declare(strict_types=1);

namespace App\Text;

class Reverser
{

    public function exec(string $in): string
    {
        return \strrev($in);
    }
}

在安装并转储自动加载器之后,通过执行php app(1),我可以使用foo命令(2):

我可以执行php app foo,并且使用其注入的依赖关系正确执行命令:

一个独立的Symfony控制台应用程序,具有最小的依赖性和自动依赖注入。
(All一个非常相似的例子的代码,here)。

6pp0gazn

6pp0gazn2#

无需疯狂的设置,只需将命令添加为CliApplication依赖项并将其注册到构造函数中:

class MyCliApplication extends Application
{

    public function __construct(
        FindSocksCommand $findSocks,
        FindPantsCommand $findPants
    )
    {
        parent::__construct('td2ac CLI');
        $this->addCommands(
            [
                $findSocks,
                $findPants
            ]
        );
    }

}

$app = $container->get(MyCliApplication::class);
$app->run();

相关问题