degraciamathieu / clike
此包已被弃用且不再维护。未建议替代包。
此包允许您运行一个类似于命令系统的设计模式的类
v1.2.0
2020-07-01 08:46 UTC
Requires
- php: >=7.1.0
Requires (Dev)
- mockery/mockery: ^1.3
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2023-03-29 00:28:15 UTC
README
DeGraciaMathieu/Clike
命令设计模式对于创建可以由后续单独模块实现更多功能的模块化组件的命令行外壳应用程序非常有用。
安装
在以下控制台中运行命令以将包下载到您的项目
composer require degraciamathieu/clike
用法
创建一个命令
命令是一个必须实现接口 DeGraciaMathieu\Clike\Contracts\Command::class
的类。
以下是一个有效的命令示例。
use DeGraciaMathieu\Clike\Lines; use DeGraciaMathieu\Clike\Contracts; class Clear implements Contracts\Command { /** * Get the command description */ public function description() :string { return 'Command description...'; } /** * Check if the command is executable */ public function authorized() :bool { return true; } /** * Bind of this command */ public function binding() :string { return '/clear'; } /** * Code executed by this command */ public function process() :void { // } /** * Output of this command */ public function output() :array { return [ new Lines\Info('Output text...'), ]; } }
执行一个命令
现在让我们来玩我们的 Clear 命令。
use DeGraciaMathieu\Clike\Command; $command = new Command(); $command->execute(new Clear());
在确认我们可以使用 authorized
方法使用此命令后,此代码将执行我们的命令的 process
方法。
最终执行 output
方法,显示以下结果。
// array:2 [ // "timestamp" => 1531339693 // "lines" => array:1 [ // 0 => array:2 [ // "type" => "info" // "content" => "Output text..." // ] // ] // ]
在终端中执行命令
use DeGraciaMathieu\Clike\Terminal; $terminal = new Terminal([ Clear::class, ]); $terminal->execute('/clear');
使用终端检索所有可用命令
use DeGraciaMathieu\Clike\Terminal; $terminal = new Terminal([ Clear::class, ]); $terminal->getAvailableCommands(); // [ // [ // "binding" => "/clear" // "description" => "Command description..." // ] // ]