zein / console
PHP 的控制台库
v1.1.1
2022-10-02 14:51 UTC
Requires
- php: >=7.4
README
用于在命令行中运行 PHP 的库。灵感来自 symphony/console。
如何使用
在开始使用此库之前,请确保您的 autoload.php 文件已创建 App 和 Commands 命名空间。
目录结构
app
---- AppConsole.php
commands
---- Make.php
app.php
vendor
Make.php
<?php namespace Commands; use Zein\Console\Command\Contract; class Make extends Contract { protected array $signatures = [ 'make:plugin' => ['description' => 'Make a plugin', 'input' => '{pluginname}'] ]; protected array $commandOptions = [ '--type' => 'Set plugin type' ]; public function handle() { // Retrieve all option $option = $this->option(); // Retrieve single option $option = $this->option('type'); // Retrive all argument $argument = $this->argument(); // Retrieve single argument $argument = $this->argument('type'); // running code here } }
AppConsole.php
<?php namespace App; use Zein\Console\{Console,Argument,Output\Output}; class AppConsole extends Console { private object $argument; public function __construct() { $this->argument = new Argument; $this->argument->strict = false; $this->argument->fetch(); } public function run() { if (!$this->argument->get()) { Output::help($this->commandClass); } $Parameter = $this->argument->getParameter(); $Option = $this->argument->getOption(); // Run command $Command = $this->{$this->seperateCommand($Parameter[0])}; $CommandInstance = new $Command($Option, $Parameter); $CommandInstance->handle(); } }
app.php
<?php use App\AppConsole; require __DIR__ . '/vendor/autoload.php'; $AppConsole = new AppConsole; $AppConsole->register([ 'make' => \Commands\Make::class, ]); $AppConsole->run();
运行
php app.php make:plugin dummies --type=report