ricardopedias/freep-console

基于PHP的终端命令实现工具包

v1.2.0 2022-06-28 20:00 UTC

This package is auto-updated.

Last update: 2024-09-26 22:09:48 UTC


README

PHP Version License Codacy Badge Codacy Badge

摘要

此存储库包含在PHP应用程序中轻松实现终端命令管理器的必要功能。

composer require ricardopedias/freep-console

有关详细信息,请参阅 文档摘要

使用方法

1. 创建命令

基于抽象类 Freep\Console\Command 实现“my-command”命令

class MyCommand extends Command
{
    protected function initialize(): void
    {
        $this->setName("my-command");
        $this->addOption(
            new Option('-r', '--read', 'Read a text file', Option::REQUIRED)
        );
    }

    protected function handle(Arguments $arguments): void
    {
        $this->info("Hello");
    }
}

2. 创建脚本

创建一个文件,例如命名为“myconsole”,并添加以下内容

#!/bin/php
<?php
include __DIR__ . "/vendor/autoload.php";

array_shift($argv);

$terminal = new Freep\Console\Terminal("/root/of/super/application");
$terminal->loadCommandsFrom("/directory/of/commands");
$terminal->run($argv);

3. 运行脚本

./myconsole my-command -r
# will display: Hello
./myconsole my-command --help
# will display:
#
# Command: my-command
# Run the 'my-command' command
# 
# How to use:
# ./myconsole my-command [options]
# 
# Options:
# -h, --help   Display command help
# -r, --read   Read a text file
./myconsole --help
# will display:
#
# How to use:
# ./myconsole command [options] [arguments]
# 
# Options:
# -h, --help   Display command help
#
# Available commands:
# help           Display command help
# my-command     Run the 'my-command' command

特性

  • 适用于PHP 8.0或更高版本;
  • 采用最佳实践和最高质量编码;
  • 文档完善且IDE友好;
  • 使用TDD(测试驱动开发)开发;
  • 使用PHPUnit实现单元测试;
  • 用❤️ & ☕制作。

致谢

Ricardo Pereira Dias