nikolajev / console-lite
轻量级PHP控制台工具
dev-master
2022-01-28 18:54 UTC
Requires
- php: >=7.1
This package is auto-updated.
Last update: 2024-09-29 00:21:59 UTC
README
确保您的项目的composer.json文件包含
{
"minimum-stability": "dev",
"config": {
"bin-dir": "bin"
}
}
composer require nikolajev/console-lite
这将在您的项目内部创建一个bin/lite(composer符号链接)文件。
配置Lite控制台
在您的项目根目录内创建/console-lite.json文件
namespace - 必需
naming exceptions - 可选
如果ConsoleLite命令文件目录在src中
{
"namespace": "App\\ConsoleLite\\"
}
如果composer自动加载psr-4命名空间ConsoleLite已注册
{
"namespace": "ConsoleLite\\",
"naming exceptions": {
"new": "NewCommand"
}
}
命名约定
bin/lite create命令将执行<AnyNamespace\>ConsoleLite\Create类
new关键字在PHP中已被保留,无法声明class New
在这种情况下,您可以在naming exceptions部分指定此类异常
配置composer
NB!!!如果ConsoleLite命令文件目录在src中,则不需要这样做
在您的项目的composer.json中
{
"autoload": {
"psr-4": {
"ConsoleLite\\": "ConsoleLite/"
}
}
}
composer dump-autoload
创建命令类文件
/ConsoleLite/NewCommand.php 或 /src/ConsoleLite/NewCommand.php
<?php
namespace ConsoleLite; // || namespace App\ConsoleLite;
use Nikolajev\ConsoleLite\ConsoleCommand;
class NewCommand extends ConsoleCommand
{
public function exec()
{
$this->getArguments(); // array ['file']
$this->getArgument(0); // string 'file'
$this->getOptions(); // array ['ext' => 'txt']
$this->optionIsSet('ext'); // bool true
$this->getOptionValue('ext'); // string 'txt'
// Any execution logics here
}
}
现在您可以使用bin/lite new file --ext=txt