efrane/console-additions

Symfony Console 辅助类

v0.8.0 2023-05-14 09:47 UTC

README

Build Status Latest Stable Version Latest Unstable Version License

Console Additions

工具,使使用 Symfony Console 更加出色。

安装

此包可在 Packagist 上获取

composer require efrane/console-additions

附加功能

批量

此类提供了对 Symfony Console 应用程序中命令批量的支持。这在编写诸如部署或更新脚本的命令行命令时非常有用,这些命令以特定的顺序调用许多其他命令,例如缓存更新、数据库迁移等。

Command::execute 中的使用

\EFrane\ConsoleAdditions\Command\Batch::create($this->getApplication(), $output)
    ->add('my:command --with-option')
    ->add('my:other:command for-this-input')
    ->run();

Shell 命令

批量可以是一系列交织的 console 应用程序和系统 shell 命令。这是一个高级特性,需要作为附加依赖项的 symfony/process 包。

由于 shell 命令在内部创建 Process 对象,Batch API 提供了添加 shell 命令的方法

  • addShell 添加具有给定配置的过程(有关详细信息,请参阅 Batch::addShell)
  • addShellCb(string $cmd, callable $cb) 创建过程并将其传递给回调以进行进一步配置。这对于需要某种进程管道的命令特别有用。

抑制错误

可以在不抛出异常的情况下运行命令的批量。

输出

此包提供了额外的控制台输出接口

文件输出

FileOutput 将所有数据写入文件流,并具有具体的口味

  • NativeFileOutput 使用本机 PHP 文件流函数,因此是本地目的地的良好选择。根据您的服务器 PHP 流协议配置,它甚至可能适用于远程目的地。

  • FlysystemFileOutput 另一方面,将流数据传递到 league/flysystem 适配器,从而能够将数据发送到任何 Flysystem 支持的目的地,即 S3、Dropbox、FTP 等。

多路复用输出

MultiplexedOutput 可以用于将多个输出接口组合成一个。这是文件输出的逻辑伴侣,因为通常人们可能希望将输出发送到用户的控制台和一些其他目的地。一个简单的内部设置可能如下所示

    class Command extends \Symfony\Component\Console\Command {
        public function execute(InputInterface $input, OutputInterface $output) {
            // send output to multiple destinations
            $output = new \EFrane\ConsoleAdditions\Output\MultiplexedOutput([
                $output,
                new \EFrane\ConsoleAdditions\Output\NativeFileOutput('command.log')
            ]);
            
            // normal console command
            
        }
    }