weebel / console
PHP应用程序的轻量级和可扩展的命令行界面(CLI)。
Requires
- league/climate: ^3.8
- weebel/contracts: dev-master
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- mockery/mockery: ^1.5
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-latest
- symfony/var-dumper: ^6.0
- weebel/container: ^0.1.2
- weebel/dispatcher: dev-master
This package is not auto-updated.
Last update: 2024-09-22 18:23:27 UTC
README
这是一个简单灵活的PHP控制台包,允许您轻松构建命令行应用程序。
安装
安装此包最简单的方式是通过composer
composer require weebel/console
用法
要使用控制台内核,您必须有一个实现了Psr\Container\ContainerInterface
接口的容器类。一个流行的例子是Laravel容器。以下是一个使用Laravel容器在项目主引导可执行文件(您可以在项目的bin目录中将文件命名为console)中的简单用法示例:
#!/usr/bin/env php <?php require __DIR__ . '/../vendor/autoload.php'; $commandContainer = \Weebel\Console\CommandContainer::getInstance(); $commandContainer->register(ExampleCommand::class); $container = \Illuminate\Container\Container::getInstance(); \Weebel\Console\StandAlone\ConsoleKernelBooter::start($container);
在上面的示例中,我们首先包含自动加载文件以引入所有必要的依赖项。然后,我们检索CommandContainer实例,并将其与ExampleCommand类注册。最后,我们创建容器实例并将其传递给ConsoleKernelBooter
类以启动控制台应用程序。
创建命令
要创建一个命令,扩展Weebel\Console\Command
类并添加一个唯一的名称常量和描述常量。您还可以通过定义$validOptions属性作为数组来指定命令的有效选项。以下是一个问候命令的示例:
<?php namespace App; use Weebel\Console\Command; use Weebel\Console\Concerns\HasClimate; class GreetingCommand extends Command { use HasClimate; public const NAME = 'app:greeting'; public const DESCRIPTION = 'Saying good morning in some languages'; protected array $validOptions = [ 'lang' => 'Language for greeting | Optional' ]; public function __invoke(string $name) { switch ($this->lang) { case 'en': $greeting = "Good morning"; break; case 'nl': $greeting = "Goede morgen"; break; case 'de': $greeting = "Guten Tag"; break; default: $greeting = "Good morning"; break; } $this->out("$greeting $name"); } }
注册您的命令后,您可以在终端使用以下语法执行它:
bin/console app:greeting John --lang=eng
自定义控制台输出
该包提供了一个HasClimate
特性,可用于自定义控制台输出。您可以在您的命令中使用此特性来添加颜色、格式化和其他自定义输出。以下是一些如何使用HasClimate特性自定义控制台输出的示例:
- 添加颜色
$this->red('This text will be displayed in red'); $this->yellow('This text will be displayed in yellow');
- 格式化文本
$this->bold('This text will be displayed in bold'); $this->italic('This text will be displayed in italic'); $this->underline('This text will be displayed underlined');
- 使用制表符显示消息
$this->tab(2)->out('This message will be displayed with two tabs');
- 使用换行符显示消息
$this->out('This message will be displayed')->br()->out('on a new line');
使用HasClimate
特性,您可以完全控制控制台输出的显示方式,从而创建美观且易于阅读的输出。
扩展功能
该包提供了一种方法来扩展其功能,允许您为其触发的事件创建监听器。其中一个事件是当从输入中解析命令时触发的CommandResolved
事件。此事件提供了有关解析的命令及其选项的信息。
- 为了使用此功能,您需要将您的EventDispatcher实现传递给内核引导器。您的事件调度器应实现
Psr\EventDispatcher\EventDispatcherInterface
。
以下是在控制台内核中使用流行的Symfony事件调度器的示例:
#!/usr/bin/env php <?php require __DIR__ . '/vendor/autoload.php'; use Symfony\Component\EventDispatcher\EventDispatcher; use Weebel\Console\CommandContainer; use Weebel\Console\StandAlone\ConsoleKernelBooter; use Illuminate\Container\Container; $commandContainer = CommandContainer::getInstance(); $commandContainer->register(GreetingCommand::class); $container = Container::getInstance(); $dispatcher = new EventDispatcher(); ConsoleKernelBooter::start($container, $dispatcher);
许可证
Weebel Console是开源软件,采用MIT许可证。