scf/simple-command-factory

简单的命令工厂。

1.0.2 2020-06-23 05:06 UTC

This package is auto-updated.

Last update: 2024-09-23 14:37:48 UTC


README

Build Latest Stable Version License Total Downloads

简单命令工厂

scf 是一个简单、小巧、轻量级的命令工厂。它包含一个命令来帮助生成更多命令的样板代码,并提供了足够的功能来完成更多小任务。请随意创建问题/贡献!。

快速亮点

  • 我在一些当前正在进行的家庭项目中使用这个工具,所以我会确保从 v1.0.0 版本开始维护包的稳定性。
  • 这个工具受到了 Laravel Artisan 命令和 Symfony 命令行包的启发。

安装

# composer create-project scf/simple-command-factory <app>
composer create-project scf/simple-command-factory simple-command-factory
# or
git clone https://github.com/joshuaGlass808/simple-command-factory.git

cd simple-command-factory/
composer install

之后,您可以开始创建命令

./scf create:command --command-name='ExampleCommand' --signature='print:message'
# or
php scf create:command --command-name='ExampleCommand' --signature='print:message'

运行上述命令将产生以下输出

Building file: simple-command-factory/app/Commands/ExampleCommand.php
New class (test) create: simple-command-factory/app/Commands/ExampleCommand.php
Don't forget to add ExampleCommand to the App/Kernel class.

这将创建一个类似以下文件

<?php

namespace App\Commands;

use SCF\Contracts\CommandContract;
use SCF\Commands\BaseCommand;
use SCF\Traits\CommandTrait;

class ExampleCommand extends BaseCommand implements CommandContract
{
    use CommandTrait;

    public string $signature = 'print:message';
    public array $argumentMap = [];

    public function execute(): void
    {
        // Get started!
    }
}

示例命令类

此存储库在 App\Commands 命名空间中提供了一个已设置的示例命令。请随意使用它!

<?php declare(strict_types=1);

namespace App\Commands;

use SCF\Contracts\CommandContract;
use SCF\Commands\BaseCommand;
use SCF\Traits\CommandTrait;
use SCF\Styles\TextStyle;

class ExampleCommand extends BaseCommand implements CommandContract
{
    use CommandTrait;

    public string $signature = 'print:message';
    public array $argumentMap = [
        '--message=' => 'Message to be printed',
        '--show'     => 'For boolean style flags, leave out the = at the end. Default is false unless used'
    ];

    /**
     * Method called to run the command.
     */
    public function execute(): void
    {
        // Get started!
        $start = microtime(true);
        $args = $this->getArgs();
        if ($args['show']) {
            $start = microtime(true);
            $this->success("Message: {$args['message']}\n");
        }
        $this->warn("Environment: {$this->env['ENV']}\n");
        $this->warn("Config DB Driver: {$this->config['database-driver']}\n");
        $this->output('Execution took: ' . (microtime(true) - $start) . " seconds\n", TextStyle::CYAN);
    }
}

在我们可以使用它之前,请确保我们在 App\Kernel 中注册了它。在 Kernel.php 中

use App\Commands\ExampleCommand;

...

	/**
	 * Register your Commands here.
	 */
	const COMMANDS = [
		ExampleCommand::class,
	];

示例用法

./scf -h
./scf --help
./scf create:command --command-name='DesktopImageRotator'

./scf print:message --message='hello world' --show
#
# OUTPUT: hello world

# without the --show flag, show will default to false and not show the message
./scf print:message --message='hello world'

一旦您在 Kernel 中注册了新的命令,您将能够在帮助消息中看到它们

示例帮助

$ ./scf -h
Usage: ./scf <command:signature> [--args=...]
       ./scf -h

    create:command
        --path= : override default path (app/Commands/).
        --command-name= : Name of the command class you wish to create.
        --signature= : set the signature

    print:message
        --message= : Message to be printed
        --show : For boolean style flags, leave out the = at the end. Default is false unless used

Options:
    --help|-h : Display this help message.

文档

以下是一些在上面的示例中未展示的内容

demo

  • 命令类中的彩色文本
    • $this->output('string', TextColor::CYAN);
    • $this->warn('输出黄色文本');
    • $this->success('输出绿色文本');
    • $this->error('输出红色文本');
    • $this->error('String', true); // True 将日志记录该字符串

即将推出

  • 一种将命令行参数设置为必需的方法,以及在某种程度上强制类型的方法。