buzzingpixel/corbomite-cli

1.0.5 2019-02-25 03:12 UTC

This package is auto-updated.

Last update: 2024-09-25 15:50:08 UTC


README

是 BuzzingPixel 的 Corbomite 项目的一部分。

为 PHP CLI 注册和运行命令提供轻量级框架。

用法

在您的 CLI 前端控制器中,使用依赖注入器调用内核(注意 APP_BASE_PATH 必须定义)

<?php
declare(strict_types=1);

use corbomite\di\Di;
use corbomite\cli\Kernel as CliKernel;

define('APP_BASE_PATH', __DIR__);
define('APP_VENDOR_PATH', APP_BASE_PATH . '/vendor');

require APP_VENDOR_PATH . '/autoload.php';

Di::get(CliKernel::class)($argv);

操作(命令)

您的应用程序或 composer 包可以提供命令行上运行的命令。要这样做,在 composer.json 的 extra 对象中设置 cliActionConfigFilePath

{
    "name": "vendor/name",
    "extra": {
        "cliActionConfigFilePath": "src/actionConfig.php"
    }
}

您的配置文件路径的返回值应该是以下格式的数组

<?php
declare(strict_types=1);

return [
    'group-name' => [
        'description' => 'Very short description of group',
        'commands' => [
            'my-command' => [
                'description' => 'Very short description of command',
                'class' => \some\action\ClassName::class,
                'method' => 'myMethod', // defaults to __invoke()
            ],
            'another-command' => [
                'description' => 'Very short description of command',
                'class' => \some\action\ClassName::class,
            ],
        ],
    ],
    'another-name' => [
        'description' => 'Very short description of group',
        'commands' => [
            'my-command' => [
                'description' => 'Very short description of command',
                'class' => \some\action\ClassName::class,
            ],
        ],
    ],
];

内核将在使用 new 创建之前,从 Corbomite Dependency Injector 中尝试获取您的类,这样您就可以根据需要设置操作类的依赖注入。

调用的操作方法将接收一个 \corbomite\cli\models\CliArgumentsModel 类型的参数。

问题服务

您可以使用问题服务在控制台中提出问题并等待用户的响应

<?php
declare(strict_types=1);

namespace my\name\space;

use corbomite\cli\models\CliArgumentsModel;
use corbomite\cli\services\CliQuestionService;

class CreateMigrationAction
{
    private $cliQuestionService;

    public function __construct(
        CliQuestionService $cliQuestionService
    ) {
        $this->cliQuestionService = $cliQuestionService;
    }

    public function __invoke(CliArgumentsModel $argModel)
    {
        // Optionally check for argument on the command line before asking
        if (! $val = $argModel->getArgumentByIndex(2)) {
            $val = $this->cliQuestionService->ask(
                '<fg=cyan>Ask some question: </>'
            );
        }

        // ...do something with $val
    }
}

许可

版权所有 2018 BuzzingPixel, LLC

根据 Apache 许可协议版本 2.0(“许可证”)授权;除非适用法律要求或已书面同意,否则不得使用此文件,但需遵守许可证。您可以在 https://apache.ac.cn/licenses/LICENSE-2.0 获取许可证副本。

除非适用法律要求或书面同意,否则在许可证下分发的软件按“现状”基础分发,不提供任何明示或暗示的保证或条件。有关许可证的特定语言管辖权限和限制,请参阅许可证。