ampersa / axo
Axo - 一个简单的控制台应用程序微框架
2.4.0
2018-02-08 20:56 UTC
Requires
- php: >=7.0
- larapack/dd: ^1.1
- monolog/monolog: ^1.22
- nesbot/carbon: ^1.22
- vlucas/phpdotenv: ~2.4
Requires (Dev)
- codedungeon/phpunit-result-printer: ^0.5.0
- mockery/mockery: ^0.9.9
- phpunit/phpunit: ~6.0
README
控制台应用程序微框架
一个用于创建控制台应用程序和快速微服务的简单框架
用法
运行 php axo
以列出可用命令
$ php axo
Available commands:
--------------------
example An example command
new Create a new command
version Print the current running version of Axo
创建新的命令
$ php axo new --help
Create a new command
USAGE
php axo new NAME COMMAND
ARGUMENTS
name The name of the command file
command The command signature to run the new command
OPTIONS
--namespace The namespace to create the command in
--description A description for the command
--directory The directory to store the new command in
--help Displays this help text
--quiet Silences the commands output
例如
$ php axo new ProcessCommand process --description="Process a file"
全局选项
--help Print the help text for the command, if available
--quiet/-q Silence any output from the command
命令
命令放置在 commands/ 目录中,并自动枚举。
命令模板
<?php namespace Commands; use System\Log\Log; use System\Console\Command; class ExampleCommand extends Command { /** * Signature formats: * COMMAND (DESCRIPTION) {--OPTION|-O} {--OPTIONAL?} {ARGUMENT} {ARGUMENT?} * 1 2 3 4 5 6 * * 1) The command to run this Command via run * 2) An optional description that displays in the command list * 3) A compulsary option, with short alias * 4) An optional option * 5) A compulsary argument * 6) An optional argument */ protected $signature = 'example (Prints the date and time) {--timestamp|-t} {format} {timezone?}'; /** * Setup the Command * @return void */ public function setup() { // The $signature can instead be specified fluently // // $this->setCommand('example') // ->setDescription('Example command - prints the date and time') // ->requiresOption(['timestamp', 't']) // ->requiresArgument('format') // ->acceptsArgument('timezone'); } /** * Provides the help text for this command * @return string */ public function help() { return <<<EOS This is the help text for this command. Here we can explain the various options available. Options --------- -t/--timestamp Provides the timestamp to use, defaults to current -q/--quiet Silences output EOS; } /** * Run the program * @return Response */ public function run() { $timestamp = $this->option(['timestamp', 't'], time()); $this->output(date($this->argument('format'), $timestamp)); } }
依赖注入
类型提示的依赖项会自动注入到命令构造函数和 run()
方法中。
您必须在自定义构造函数中调用 parent::__construct();
以确保命令正确初始化。
参数也会自动注入到 run()
方法中,变量名匹配
<?php namespace Commands; use Carbon\Carbon; use System\Console\Command; class ExampleCommand extends Command { protected $signature = 'example (Prints the date and time) {format}'; /** @var Carbon\Carbon */ protected $carbon; public function __construct(Carbon $carbon) { parent::__construct(); $this->carbon = $carbon; } public function run(string $format) { $this->output('Format: '. $format); $this->output($this->carbon->now()->format($format)); } }
$ php axo example d M Y
Format: d M Y
08 Feb 2018
控制台输出
样式化输出
可以通过实用函数实现美观的控制台输出,例如
$this->output() $this->info() $this->highlight() $this->warn() $this->danger() $this->error()
您还可以使用第二个参数对文本应用样式
$this->danger('Danger text', ['underline', 'bold']);
进度条
显示长时间运行任务的进度条
// Init a progress bar // Pass the upper limit of the progress as the first argument $this->progressBar(10); // Set a title $this->setProgressBarTitle('Progress...'); // Set a message to display on completion $this->setProgressBarCompleteMessage('Completed task'); // Begin $this->startProgressBar(); for ($number = 0; $number < 10; $number++) { // Increment the progress bar $this->advanceProgressBar(); // Or, advance by multiple // $this->advanceProgressBar(2); // Alternatively, set the progress manually // $this->setProgressBar(5); } // Mark the progress bar as complete $this->completeProgressBar();
示例
Progress...
5/10 [############# ] 50%
快捷方式使用
axo 可以在没有 php 前缀的情况下运行
$ chmod +x axo
$ ./axo command