simsoft / console
控制台应用程序。采用Laravel风格构建于Symfony/Console之上。
1.0.0
2024-04-04 02:48 UTC
Requires
- php: ^8
- symfony/console: ^7
- symfony/lock: ^7.0
This package is auto-updated.
Last update: 2024-09-04 04:01:31 UTC
README
控制台应用程序,由 Symfony/Console 构建
安装
composer require simsoft/console
基本使用
在 bootstrap 或入口脚本文件中设置。
console.php
<?php declare(strict_types=1); require "vendor/autoload.php"; use Simsoft\Console\Application; $status = Application::make() ->withCommands([ \App\HelloWorldCommand::class, ]) ->run(); exit($status);
创建一个简单的命令
创建一个简单的 HelloWorldCommand 类。
<?php namespace App; use Simsoft\Console\Command; class HelloWorldCommand extends Command { static string $name = 'screen:welcome'; static string $description = 'Hi Guest'; protected bool $lockable = true; // Enable lock. Default: false. protected function handle(): void { $this->info('Hello World'); } }
添加控制台参数和选项
有关详细教程,请参阅Symfony Console: 控制台输入(参数 & 选项)。
<?php namespace App; use Simsoft\Console\Command; class HelloWorldCommand extends Command { static string $name = 'screen:welcome'; static string $description = 'Hi Guest'; protected bool $lockable = true; // Enable lock. Default: false. protected function init(): void { $this // Add arguments ->addArgument('name', InputArgument::REQUIRED, 'Who do you want to greet?') ->addArgument('last_name', InputArgument::OPTIONAL, 'Your last name?') // Add option ->addOption( 'iterations', 'i', InputOption::VALUE_REQUIRED, 'How many times should the message be printed?', 1 ) ; } protected function handle(): void { // get arguments and options. $name = $this->argument('name'); $lastName = $this->argument('last_name'); $iterations = $this->option('iterations'); $arguments = $this->arguments(); // $arguments contains ['name' => '..user input.. ', 'last_name' => '...'] $options = $this->options(); // $options contains ['iterations' => '..user input.. '] for($i = 0; $i < $iterations; ++$i) { $this->info('Hi'); } $this->error('Display error message'); } }
闭包命令
示例用法
<?php declare(strict_types=1); require "vendor/autoload.php"; use Simsoft\Console\Application; Application::command('example:closure:command', function() { $this->info('Hello World!'); }) $status = Application::make()->run();
定义具有描述的闭包命令。
Application::command('example:closure:command', function() { $this->info('Hello World!'); })->purpose('Simple closure command');
定义具有输入的闭包命令。
use Symfony\Component\Console\Input\InputArgument; Application::command('example:closure:command2', function() { $name = $this->argument('name'); $this->info("I got your name: $name"); }) ->purpose('Get user name') ->input(function(){ $this->addArgument('name', InputArgument::REQUIRED, 'Name required'); });
写入输出
/** * @throws \Throwable */ protected function handle(): void { $this->info('Hello World'); // Write info message $this->comment('Comment text'); $this->question('My Question?'); $this->error('Warning'); // Write error message $this->line('Simple line'); // Write simple un-formatted message $this->errorBlock('Block Header', 'Block message'); $this->newLine(); // Write a single blank line $this->newLine(3); // Write three blank lines }
提示输入
/** * @throws \Throwable */ protected function handle(): void { // Simple question // Other argument: $default. $name = $this->ask('What is your name?'); $this->info("Your name is $name"); // Hide user input question // Other argument: $default $secret = $this->secret('Please tell me a secret?'); $this->info("Your secret is $secret"); // Asking for confirmation. // Other argument: $default. if ($this->confirm('Are you above 18yo (y/n)?')) { $this->info('You have grow up!'); } else { $this->info("You are very young!"); } // Multiple choice questions. // Other arguments: $defaultIndex, $maxAttempts, $allowMultipleSelections. $myChoice = $this->choice('Which color do you like?', ['Yellow', 'Orange', 'Blue']); $this->info("You have selected $myChoice"); }
表格和进度条
表格
protected function handle(): void { $this->table( ['Place', 'Name', 'Score'], [ [1, 'Albert', '100'], [2, 'Jane', '98'], [3, 'Alvin', '95'], [4, 'Mary', '89'], [5, 'Alex', '88'], [6, 'Wong', '87'], ] ); }
进度条
protected function handle(): void { $items = range(1, 10); $this->withProgressBar($items, function($item) { // perform task on $item. }); }
自定义进度条
protected function handle(): void { $max = 20; // Maximum items. $bar = $this->createProgressBar($max); $bar->setBarCharacter('$'); $bar->setProgressCharacter('>>'); $bar->setBarWidth(50); $bar->setEmptyBarCharacter('_'); $bar->start(); for ($i = 1; $i <= $max; ++$i) { // do something. $bar->advance(); } $bar->finish(); }
从其他命令中调用命令
protected function handle(): void { // Call another command. $this->call('example:friendly', [ 'name' => 'Jane', '--age' => 18, ]); // Call another commands silently. $this->callSilently('example:friendly', [ 'name' => 'John', '--age' => 28, ]); }
从控制器中调用命令
在 bootstrap index.php 中注册命令
<?php require_once dirname(__DIR__) . '/vendor/autoload.php'; use Simsoft\Console\Application; Application::commands([ \Example\Commands\HelloWorldCommand::class, \Example\Commands\FriendlyCommand::class, \Example\Commands\QuestionsCommand::class, \Example\Commands\WinnersCommand::class, \Example\Commands\MoneyComeCommand::class, ]); Application::command('example:closure:command', function() { $this->info('Hello World!'); })->purpose('Simple closure command'); Application::command('example:closure:command2', function() { $name = $this->argument('name'); $this->info("I got your name: $name"); $age = $this->option('age'); $this->info("Your age is $age"); })->purpose('Get user name')->input(function(){ $this->addArgument('name', InputArgument::REQUIRED); $this->addOption('age'); });
执行命令
<?php namespace App; use Simsoft\Console\Application; class AppController { public function index(): void { Application::call('example:closure:command2', [ 'name' => 'Marry', '--age' => 25, ]); } }
许可证
Simsoft 验证器采用 MIT 许可证。有关详细信息,请参阅LICENSE 文件。