vxm / yii2-console-menu
为 Yii2 优化的控制台菜单
1.0.0
2019-04-10 14:52 UTC
Requires
- php: >=7.1
- nunomaduro/laravel-console-menu: ~2.1
- yiisoft/yii2: >=2.0.13
This package is auto-updated.
Last update: 2024-09-17 06:01:05 UTC
README
关于它
这是一个基于 nunomaduro/laravel-console-menu 的扩展,它是一个 php-school/cli-menu 的包装器,用于 Yii2 控制台控制器。
要求
安装
使用 Composer 安装 Yii2 控制台菜单
composer require vxm/yii2-console-menu
用法
快速设置
use yii\console\Controller; /** * @method \vxm\consoleMenu\Menu menu(string $title = '', array $options = []) */ class TestController extends Controller { /** * Execute the action. * * @return void */ public function actionTest() { $option = $this->menu('Pizza menu', [ 'Freshly baked muffins', 'Freshly baked croissants', 'Turnovers, crumb cake, cinnamon buns, scones', ])->open(); $this->stdout("You have chosen the option number #$option"); } }
使用问题进行设置
use yii\console\Controller; /** * @method \vxm\consoleMenu\Menu menu(string $title = '', array $options = []) */ class TestController extends Controller { /** * Execute the action. * * @return void */ public function actionTest() { $option = $this->menu('Pizza menu') ->addOption('mozzarella', 'Mozzarella') ->addOption('chicken_parm', 'Chicken Parm') ->addOption('sausage', 'Sausage') ->addQuestion('Make your own', 'Describe your pizza...') ->addOption('burger', 'Prefer burgers') ->setWidth(80) ->open(); $this->stdout("You have chosen the text option: $option"); } }
使用高级选项进行设置,在这种情况下,是一个密码
use yii\console\Controller; /** * @method \vxm\consoleMenu\Menu menu(string $title = '', array $options = []) */ class TestController extends Controller { /** * Execute the action. * * @return void */ public function actionTest() { $menu = $this->menu('Pizza menu') ->addOption('mozzarella', 'Mozzarella') ->addOption('chicken_parm', 'Chicken Parm') ->addOption('sausage', 'Sausage') ->addQuestion('Make your own', 'Describe your pizza...'); $itemCallable = function (\PhpSchool\CliMenu\CliMenu $cliMenu) use ($menu) { $cliMenu->askPassword() ->setValidator(function ($password) { return $password === 'secret'; }) ->setPromptText('Secret password?') ->ask(); $menu->setResult('Free spice!'); $cliMenu->close(); }; $menu->addItem('Add extra spice for free (password needed)', $itemCallable); $option = $menu->addOption('burger', 'Prefer burgers') ->setWidth(80) ->open(); $this->stdout("You have chosen the text option: $option"); } }
外观
可用的颜色: black
、red
、green
、yellow
、blue
、magenta
、cyan
、white
。
$this->menu($title, $options) ->setForegroundColour('green') ->setBackgroundColour('black') ->setWidth(200) ->setPadding(10) ->setMargin(5) ->setExitButtonText("Abort") // remove exit button with ->disableDefaultItems() ->setUnselectedMarker('❅') ->setSelectedMarker('✏') ->setTitleSeparator('*-') ->addLineBreak('<3', 2) ->addStaticItem('AREA 2') ->open();
查看完整的文档 这里。