shaggy8871/console

Console是一个轻量级的PHP控制台框架

1.0.6 2020-04-11 20:47 UTC

This package is auto-updated.

Last update: 2024-09-12 06:54:36 UTC


README

Console是一个轻量级的PHP控制台路由框架。它易于开始使用,几乎不需要配置,并且可以在现有项目中运行而无需大规模重写。

安装

在composer.json中

"require": {
    "shaggy8871/console": "dev-master"
}

然后运行

composer install

示例console.php文件

<?php
include_once "vendor/autoload.php";

$console = new Console\Runner();

// Add one or more commands here
$console->registerAll([
    'app:command' => [
        'class' => 'Commands\CustomCommand',
        'description' => 'Add your custom command description'
    ]
]);

// Start your engines...
try {
    $console->run();
} catch(Console\Exception\CommandNotFoundException $e) {
    echo $e->getMessage() . "\n--------\n\n" . $console->getHelp();
    die();
}

示例命令类

<?php

namespace Commands;

use Console\CommandInterface;
use Console\Args;

class CustomCommand implements CommandInterface
{

    public function execute(Args $args)
    {

        // Convert short-form to long-form arguments
        $args->setAliases([
            'l' => 'longform'
        ]);

        // Write custom code
        // ... Do something with $args->longform or $args->getAll()

    }

}

颜色装饰

要使用颜色或样式装饰输出,请使用以下方法

<?php

use Console\Decorate;

echo Decorate::color('Hello, world!', 'red bold bg_white');

颜色参考

要引入256-color VGA palette,请使用方便的vgaColorvgaBackground助手。

<?php

use Console\Decorate;

echo Decorate::color('Hello, world!', [
    Decorate::vgaBackground(9), 
    Decorate::vgaColor(52)
]);

StdOut

一些控制台应用程序需要根据输出是渲染到控制台还是日志文件来启用或禁用ANSI输出。StdOut类允许在这两种模式之间切换。

要渲染一个或多个装饰过的字符串

<?php

use Console\StdOut;

StdOut::write([
    ["Hello, world!\n", 'red'],
    ["This is a second line, but in green\n", 'green']
]);

要禁用ANSI但仍然输出,请先调用disableAnsi()。所有ANSI字符将在未来的输出中被删除。您可以通过调用enableAnsi()重新启用ANSI模式。

<?php

use Console\StdOut;

StdOut::disableAnsi();
StdOut::write([
    ["Hello, world!\n", 'red'],
    ["This is a second line, but in green\n", 'green']
]);