popphp/

Pop PHP 框架的 Pop Console 组件

4.2.2 2024-02-28 18:20 UTC

README

Build Status Coverage Status

Join the chat at https://popphp.slack.com Join the chat at https://discord.gg/TZjgT74U7E

概述

pop-console 为从控制台终端运行应用程序并提供格式化输出到终端窗口提供了一个层。它支持命令及其参数,以及基于 ANSI 的控制台颜色。它可以很容易地与 Pop 框架构建的应用程序一起使用,以将 CLI 请求路由到应用程序。

pop-consolePop PHP 框架 的一个组件。

注意

以下代码表示基本示例。理想情况下,您可以连接一个应用程序以使用控制台将内容输出到终端屏幕,但不用于设置路由、控制器和动作。请参阅 Pop PHP 教程 示例应用程序,以了解如何使用 Pop PHP 连接基于 CLI 的应用程序,并使用 Pop PHP 设置路由。

顶部

安装

使用 Composer 安装 pop-console

composer require popphp/pop-console

或者,在您的 composer.json 文件中引入它

"require": {
    "popphp/pop-console" : "^4.1.0"
}

顶部

快速开始

输出到控制台

您可以使用控制台对象来管理和部署输出到控制台,包括前置标题和后置页脚。

use Pop\Console\Console;

$console = new Console();
$console->setHeader('My Application'); // Set a global header at the start of the script
$console->setFooter('The End');        // Set a global footer at the end of the script

$console->append('Here is some console information.');
$console->append('Hope you enjoyed it!');
$console->send();

上述代码将输出

    My Application
    
    Here is some console information.
    Hope you enjoyed it!

    The End

控制台换行和边距

默认情况下,控制台对象强制执行 80 个字符的换行宽度,并提供 4 个空格的边距以提高可读性。这些值可以根据应用程序的需要进行更改。

use Pop\Console\Console;

$console = new Console(40, 2); // wrap width of 40, margin of 2 spaces
$console->append(
    'Here is some console information. This is a really long string. It will have to wrap.'
);
$console->send();
  Here is some console information. This
  is a really long string. It will have to
  wrap.

顶部

响应缓冲区

追加与写入

在上面的示例中,使用 append() 方法与 send() 方法一起使用。方法 append() 将内容追加到响应缓冲区,只有当调用方法 send() 时,内容才会输出到终端屏幕。这对于在发送之前需要执行多个步骤来创建响应缓冲区很有用。

使用方法 write() 允许您实时将内容输出到终端屏幕,而无需调用 send() 方法。这对于在应用程序中实时将内容推送到终端屏幕非常有用。

use Pop\Console\Console;

$console = new Console(40);
$console->write(
    'Here is some console information. This is a really long string. It will have to wrap.'
);

换行和边距

默认情况下,调用 append()write() 方法将在内容开头产生边距值,并在内容末尾产生换行符。如果这不是期望的行为,可以通过布尔标志来控制此操作

use Pop\Console\Console;

$console = new Console(40);
$console->write('Here ', false);          // No new line, but use margin
$console->write('is ', false, false);     // No new line, no margin
$console->write('some ', false, false);   // No new line, no margin
$console->write('content.', true, false); // Use new line, but no margin

顶部

颜色

在支持颜色的控制台终端上,您可以使用 colorize() 方法将文本输出到控制台着色

use Pop\Console\Console;
use Pop\Console\Color;

$console = new Console();
$console->write(
    'Here is some ' . 
    $console->colorize('IMPORTANT', Color::BOLD_RED) .
    ' console information.'
);

colorize() 方法还可在 Pop\Console\Color 类的静态方法上使用

use Pop\Console\Console;
use Pop\Console\Color;

$console = new Console();
$console->write(
    'Here is some ' . 
    Color::colorize('IMPORTANT', Color::BOLD_RED) .
    ' console information.'
);

可用的颜色常量包括

  • 正常
  • 黑色
  • 红色
  • 绿色
  • 黄色
  • 蓝色
  • 品红色
  • 青色
  • 白色
  • 亮黑色
  • 亮红色
  • 亮绿色
  • 亮黄色
  • 亮蓝色
  • 亮品红色
  • 亮青色
  • 亮白色
  • 粗黑
  • 粗红
  • 粗绿
  • 粗黄
  • 粗蓝
  • 粗品红
  • 粗青
  • 粗白
  • 高亮加粗黑色
  • 高亮加粗红色
  • 高亮加粗绿色
  • 高亮加粗黄色
  • 高亮加粗蓝色
  • 高亮加粗品红色
  • 高亮加粗青色
  • 高亮加粗白色

顶部

line()方法提供了一种打印水平线规则到终端的方法。默认的线字符是破折号-,但可以向方法传递任何字符。

use Pop\Console\Console;

$console = new Console();
$console->line();
    ----------------------------------------

它将默认为控制台对象的换行宽度。如果没有可用的换行宽度,它将采用终端的宽度,除非指定了自定义宽度。

use Pop\Console\Console;

$console = new Console();
$console->line('=', 20);
    ====================

顶部

标题

header()方法提供了一种输出带有下划线强调的单独文本块的方法。

use Pop\Console\Console;

$console = new Console(80);
$console->header('Hello World');
    Hello World
    -----------

也可以控制字符、大小和对齐方式。

use Pop\Console\Console;

$console = new Console();
$console->header('Hello World', '=', 40, 'center');
                   Hello World
    ========================================

顶部

警报

警报是以特殊格式化的框形式出现的,它为用户提供了一种关于重要信息和通知的样式和增强体验。

use Pop\Console\Console;

$console = new Console(40);
$console->alertDanger('Hello World', 'auto');
$console->alertWarning('Hello World', 'auto');
$console->alertSuccess('Hello World', 'auto');
$console->alertInfo('Hello World', 'auto');
$console->alertPrimary('Hello World', 'auto');
$console->alertSecondary('Hello World', 'auto');
$console->alertDark('Hello World', 'auto');
$console->alertLight('Hello World', 'auto');
$console->alertBox('Hello World', '-', '|', 'auto');

alertBox()方法生成一个无色的警报框,边框由字符字符串组成。上述代码将在控制台终端产生以下输出:

Alerts

顶部

提示

您可以触发一个提示以获取用户信息。

use Pop\Console\Console;

$console = new Console();
$name    = $console->prompt('Please provide your name: ');
$console->write('Hello ' . $name . '!');
$ ./app
    Please provide your name:  Nick
    Hello Nick!

您还可以强制执行一组特定的选项以及大小写敏感。如果将大小写敏感标志设置为true,则提示将不接受与提供的选项值不匹配的值。

use Pop\Console\Console;

$console = new Console();
$letter  = $console->prompt(
    'Which is your favorite letter: A, B, C, or D? ',
    ['A', 'B', 'C', 'D'],
    true
);
$console->write('Your favorite letter is ' . $letter . '.');
$ ./app
    Which is your favorite letter: A, B, C, or D? B
    Your favorite letter is B.

确认

confirm()方法是一种简写形式的提示,用于询问用户是否确实想要继续,否则应用程序将退出。

use Pop\Console\Console;

$console = new Console();
$console->confirm();
$console->write('The user said yes.');
    Are you sure? [Y/N] y
    The user said yes.

顶部

命令

命令对象允许您定义命令的名称、参数和帮助字符串值,并将命令添加到控制台对象中。

use Pop\Console\Console;
use Pop\Console\Command;

$command1 = new Command('users');
$command1->setParams('--list [<id>]');
$command1->setHelp('This is the users help screen');

$command2 = new Command('roles');
$command2->setParams('--list [<id>]');
$command2->setHelp('This is the roles help screen');

$console = new Console();
$console->addCommand($command1);
$console->addCommand($command2);

顶部

帮助屏幕

如上例所示,将命令注册到控制台对象中允许您调用help()方法以查看自动生成的帮助屏幕。

$console->help();
    users --list [<id>]    This is the users help screen
    roles --list [<id>]    This is the roles help screen

然而,控制台对象有addCommandsFromRoutes()方法,该方法与Pop\Router\Cli\Match对象一起工作,以自动生成命令及其参数和帮助字符串。

use Pop\Console\Console;

$this->console->addCommandsFromRoutes($cliRouteMatch, './myapp');

此控制台将使用CLI路由匹配对象并解析所有命令,使它们可供控制台对象利用以生成帮助屏幕。

帮助颜色

通过设置帮助屏幕颜色,可以提供额外的表现控制层。您可以选择最多4种颜色,这些颜色将用于按名称和参数分割命令字符串,并将它们着色以使不同部分以有组织的格式突出显示。

让我们看看pop-kettle组件的抽象构造函数。

    public function __construct(Application $application, Console $console)
    {
        $this->application = $application;
        $this->console     = $console;

        $this->console->setHelpColors(
            Color::BOLD_CYAN, Color::BOLD_GREEN, Color::BOLD_MAGENTA
            );
        $this->console->addCommandsFromRoutes(
            $application->router()->getRouteMatch(), './kettle'
        );
    }

    public function help()
    {
        $this->console->help();
    }

在上面的构造函数方法中,设置了帮助颜色,然后应用程序对象将CLI路由匹配对象推入控制台方法的addCommandsFromRoutes()。第二个参数./kettle是附加到帮助中每一行的脚本前缀。这两行就是生成彩色和有组织的pop-kettle帮助屏幕的全部内容,该屏幕在控制器的help()方法中被调用。

输出看起来像这样

Console Help

顶部