hoathis/symfony-console-bridge

此包已被 弃用 并不再维护。未建议替代包。

Hoa\Console 到 Symfony\Console 的桥梁。

1.15.01.06 2015-01-06 11:50 UTC

This package is auto-updated.

Last update: 2021-10-04 11:06:50 UTC


README

Hoa

Hoa 是一套 模块化可扩展结构化 的 PHP 库。此外,Hoa 致力于成为工业界和研究界之间的桥梁。

Hoathis\SymfonyConsoleBridge Build Status

本 README 中的所有示例均可在测试应用中找到并运行。

安装

使用 Composer,要将此库包含到依赖项中,您需要要求 hoathis/symfony-console-bridge

{
    "require": {
        "hoathis/symfony-console-bridge": "~1.0"
    }
}

请阅读网站以获取有关如何安装的更多信息

最后,安装依赖项

$ composer update hoathis/symfony-console-bridge

如何使用

Symfony

要使用此库与 Symfony 框架一起使用,请使用专用包:hoathis/symfony-console-bundle

输出

Hoathis\SymfonyConsoleBridge\Output\ConsoleOutput 是对原生 ConsoleOutput 的替代品,能够检测输出类型并自动配置详细程度和文本装饰。

以下是一个命令的例子

<?php

$app = new Application();

$app
    ->register('output:verbosity')
    ->setCode(function(InputInterface $input, OutputInterface $output) {
        $output->writeln('<info>I\'m a decorated text only in the console</info>');

        if ($output->getVerbosity() === OutputInterface::VERBOSITY_NORMAL) {
            $output->writeln('I\'ll be displayed with the <comment>normal</comment> verbosity level');
        }

        if ($output->getVerbosity() === OutputInterface::VERBOSITY_VERBOSE) {
            $output->writeln('I\'ll be displayed with the <comment>verbose</comment> verbosity level');
        }

        if ($output->getVerbosity() === OutputInterface::VERBOSITY_VERY_VERBOSE) {
            $output->writeln('I\'ll be displayed with the <comment>very verbose</comment> verbosity level');
        }

        if ($output->getVerbosity() === OutputInterface::VERBOSITY_DEBUG) {
            $output->writeln('I\'ll be displayed with the <comment>debug</comment> verbosity level');
        }
    })
;

运行

$ bin/console output:verbosity
# I'm a decorated text only in the console
# I'll be displayed with the verbose verbosity level

如您在终端中看到的那样,输出默认将被装饰并详细。但是,如果您运行

$ bin/console output:verbosity > output
$ cat -vet output
# I'm a decorated text only in the console$
# I'll be displayed with the very verbose verbosity level$

详细程度将自动切换到非常详细,因为输出已检测到您将其重定向到文件。

以下是用于确定详细程度和文本装饰支持的规则

详细程度 装饰
管道 正常 禁用
重定向 非常详细 禁用
终端 详细 启用

这些规则将仅在您不使用命令行参数提供任何详细程度时使用。如果您想以调试详细程度将输出重定向到文件,只需运行

$ bin/console output:verbosity -vvv > output
$ cat -vet output
# I'm a decorated text only in the console$
# I'll be displayed with the debug verbosity level$

您仍然可以使用 --ansi 选项强制 ANSI 输出

$ bin/console output:verbosity -vvv --ansi | xargs -0 echo -n | cat -vet
# ^[[38;5;189mI'm a decorated text only in the console^[[39;49;0m$
# I'll be displayed with the ^[[38;5;96mdebug^[[39;49;0m verbosity level$

想试试吗?运行 bin/console output:verbosity 来获取实时演示和代码片段。

格式化器

Hoathis\SymfonyConsoleBridge\Formatter\OutputFormatterStyle 允许您执行与原生 symfony/console 格式化器相同的一切,并且还有一些更酷的功能

  • 支持 xterm-8color 颜色名称,
  • 支持 xterm-256color 颜色代码,
  • 自动转换十六进制颜色代码,
  • 支持文本样式(正常、粗体、下划线、闪烁和反相)。

要使用这些新的 OutputFormatterStyle,请使用常规 API

<?php

class Application extends BaseApplication
{
    protected function configureIO(InputInterface $input, OutputInterface $output)
    {
        parent::configureIO($input, $output);

        $formatter = $output->getFormatter();
        $formatter->setStyle('info', new OutputFormatterStyle('#e4cbf4'));
        $formatter->setStyle('comment', new OutputFormatterStyle('#795290'));
        $formatter->setStyle('question', new OutputFormatterStyle('#de8300'));
        $formatter->setStyle('error', new OutputFormatterStyle('white', '#ff3333', array(OutputFormatterStyle::STYLE_BOLD)));
    }

    //...
}

如前一个示例所示,您可以通过使用新格式化器重新定义它们来替换内置样式。

想试试吗?运行 bin/console output:formatter:custombin/console output:formatter:native 获取实时演示和代码片段。

助手

库的真实力量来自于其助手:它们允许您管理每个终端组件。您首先必须手动加载它们

<?php

class Application extends BaseApplication
{
    protected function getDefaultHelperSet()
    {
        $set = parent::getDefaultHelperSet();

        $set->set(new Helper\WindowHelper());
        $set->set(new Helper\CursorHelper());
        $set->set(new Helper\ReadlineHelper());
        $set->set(new Helper\PagerHelper());

        return $set;
    }

    //...
}

每个助手都有一个专用的测试命令。只需运行 bin/console list 即可获取列表。

窗口

窗口助手将允许您操作当前终端窗口。它提供了一些实用方法,每个方法都绑定到一个操作

<?php

$app = new Application();

$app
    ->register('helper:window:animate')
    ->setCode(function(InputInterface $input, OutputInterface $output) use ($app) {
        $window = $app->getHelperSet()->get('window');

        $output->writeln('<info>I\'m going to bring your window to the foreground and back to the foreground after one second</info>');
        sleep(1);
        $window->lower($output);
        sleep(1);
        $window->raise($output);

        $output->writeln('<info>I\'m going to minimize your window and restore it after one second</info>');
        sleep(1);
        $window->minimize($output);
        sleep(1);
        $window->restore($output);
    })
;

还有许多其他实用方法可用

  • setTitlegetTitlegetLabel 用于操作终端标题,
  • setSizegetSizemovesetPositiongetPosition 用于操作窗口位置,
  • minimizerestorelowerraise 用于操作窗口位置,
  • scrollrefreshcopy 用于操作窗口内容。

想试试吗?运行 bin/console helper:window:animate 获取实时演示和代码片段。

光标

光标助手将允许您操作光标。它提供了一些实用方法,每个方法都绑定到一个操作

<?php

$app = new Application();

$app
    ->register('helper:cursor:draw')
    ->setCode(function(InputInterface $input, OutputInterface $output) use ($app) {
        $window = $app->getHelperSet()->get('cursor');

        $colors = ['red', '#FFCC33', 'yellow', 'green', 'blue', '#003DF5', '#6633FF'];

        $helper = new Helper\CursorHelper();
        $helper->hide($output)->move($output, 'up', 1);

        foreach ($colors as $index => $color) {
            $helper->move($output, 'left', 20 - ($index * 4));
            $output->write(sprintf('<bg=%1$s>%2$s</bg=%1$s>', $color, str_repeat(' ', 20)));

            $helper->move($output, 'down')->move($output, 'left', 20);
            $output->write(sprintf('<bg=%1$s>%2$s</bg=%1$s>', $color, str_repeat(' ', 20)));

            $helper->move($output, 'up')->bip($output);

            usleep(250000);
        }

        $helper
            ->move($output, 'down', 2)
            ->move($output, 'left', 100)
            ->reset($output)
            ->show($output);
    })
;

还有许多其他实用方法可用

  • movemoveTo 用于更改光标位置,getPosition 用于检索当前光标位置,
  • saverestore 用于保存和恢复光标位置,
  • clear 用于清除屏幕的整个或部分,
  • hideshowstyle 用于更改光标显示选项,
  • colorizereset 用于管理文本样式,
  • bip 用于发出铃声。

想试试吗?运行 bin/console helper:cursor:draw 获取实时演示和代码片段。

读取行

读取行助手将帮助您从用户那里收集输入。它提供了一些询问和验证用户输入的方法

  • read 将提示用户输入,
  • autocomplete 将显示提示并允许用户输入文本并使用自动完成,
  • select 将显示一个选项列表供用户选择,并允许他们选择一个或多个值,
  • validate 将不断请求输入,直到它通过您提供的验证器进行验证。
<?php

$app = new Application();

$app
    ->register('helper:readline:select')
        ->addOption('multi', null, InputOption::VALUE_NONE)
        ->setCode(function(InputInterface $input, OutputInterface $output) use($app) {
            $readline = $app->getHelperSet()->get('readline');

            $selection = (array) $readline->select(
                $output,
                $input->getOption('multi') ? 'Select some values:' : 'Select a value:',
                [
                    '<info>php</info>' => ReadlineHelper::SEPARATOR,
                    'hoa', 'symfony', 'laravel',
                    '<info>js</info>' => ReadlineHelper::SEPARATOR,
                    'express', 'connect', 'restify',
                ],
                null,
                false,
                $input->getOption('multi')
            );

            $output->writeln(sprintf('<info>You selected</info>: %s', implode(', ', $selection)));
        });

请注意,对于 select,您可以在您的选项列表中提供特殊选择,使用 'label' => ReadlineHelper::SEPARATOR 项作为分隔符显示。

想试试吗?运行 bin/console helper:readline:selectbin/console helper:readline:autocomplete 获取实时演示和代码片段。

分页器

分页助手允许您通过分页器显示输出,以便用户可以轻松阅读和滚动。该助手提供了两个分页器:lessmore。您需要使用一个包含产生输出的代码的闭包来为它们提供数据。

<?php

$app = new Application();

$app
    ->register('helper:pager:less')
        ->setCode(function(InputInterface $input, OutputInterface $output) use($app) {
            $pager = $app->getHelperSet()->get('pager');

            $pager->less(
                $output,
                function() {
                    passthru('cat ' . __DIR__ . '/*.php');
                }
            );
        });

想试试吗?运行 bin/console helper:pager:lessbin/console helper:pager:more 来获取实时演示和代码片段。

终端输出

终端输出助手将帮助您了解用户终端的能力。该助手提供了一个访问所有能力的单一入口:get 方法。以下是如何获取 clear_screen 能力的示例

<?php

$app = new Application();

$app
    ->register('helper:tput:get')
        ->setCode(function(InputInterface $input, OutputInterface $output) use($app) {
            $tput = new TputHelper();
            $capability = 'clear_screen';
            $value = $tput->get($capability);

            $output->writeln(sprintf('<info>%s</info>: %s', $capability, $value));
        });

想试试吗?运行 bin/console helper:tput:capabilitiesbin/console helper:tput:echobin/console helper:tput:get 来获取实时演示和代码片段。