nonetallt / jinitialize-plugin
创建新 jinitialize 插件的样板设置。
Requires
- php: >=7.0.0
- nonetallt/jinitialize-core: dev-master
- symfony/console: ^3.4
Requires (Dev)
- consolidation/robo: ~1
- henrikbjorn/lurker: ^1.2
- phpunit/phpunit: ^6
This package is not auto-updated.
Last update: 2024-09-24 17:52:48 UTC
README
创建新 jinitialize 插件的样板设置。
安装
composer create-project nonetallt/jinitialize-plugin [project name]
Composer 设置
为了使您的插件在 jinitialize 项目中注册,您必须在 composer.json 文件的 extra 部分中定义插件信息。
extra: { "jinitialize-plugin": { "name": "MyPlugin", "commands": [ "Nonetallt\\Jinitialize\\Plugin\\Example\\Commands\\MyCommand::class" ], "procedures": [ "procedures/my-procedure.json" ], "settings": [ "mySetting1", "mySetting2" ] } }
name
插件名称。这将用作此插件导出的命令的命名空间,因此最好使其简短。
commands
一个数组,包括您希望此插件导出的命令类的完全限定名称。
procedures
一个数组,包括您希望此插件导出的从项目根目录到 procedure 文件的文件路径。
settings
用户可以在他们的 jinitialize .env 文件中定义的字符串数组。设置对于定义常用的默认值很有用,而不是每次都提示用户输入。设置的例子可以是 "defaultUser" 和 "defaultPassword"。
保持命令部分最新
您可以使用以下命令自动将 src/Commands 文件夹中的所有命令类导出到您的 composer.json 文件中
vendor/bin/robo update:commands
命令
JinitializeCommand 类 继承自 Symfony\Component\Console\Command\Command 并定义了一些您在创建自己的子类时可能需要的有用辅助器。
use Nonetallt\Jinitialize\Plugin\JinitializeCommand; class ExampleCommand extends JinitializeCommand { protected function configure() { $this->setName('nameofcommand'); $this->setDescription('desc'); $this->setHelp('extended description here'); } protected function handle($input, $output, $style) { // Run code on command execution // Ask for user input, suggest default value of 'World' $name = $style->ask('What is your name?', 'World'); $style->title("Hello $name!"); $this->export('userName', $name); } public function revert() { // Revert changes made by handle if possible } public function recommendsRoot() { // bool, wether command should be executed with administrative priviliges return false; } }
abort(string $message);
通过抛出 Nonetallt\Jinitialize\Exceptions\CommandAbortedException 停止命令的执行。然后运行程序的进程将尝试通过调用它们的 revert() 方法来撤销已执行的命令。
belongsToProcedure();
如果命令对象注册为程序的组成部分,则返回 true,否则返回 false。
configure();
从命令类实现。用于定义基本命令信息、输入选项和参数。
export(string $key, $value);
将给定的键 - 值对存储到应用程序容器中。这应该用于使其他插件能够访问由命令定义的有用值。例如,数据库插件可能定义一个用于创建新数据库的命令,然后导出创建的数据库名称,以便以后可以在应用程序的设置中定义数据库名称。
getPluginName();
获取此命令注册的插件名称。
getUser();
获取 Nonetallt\Jinitialize\Plugin\ShellUser 的实例。该类有以下辅助方法
- isRoot()
- getName()
- getInfo()
- getId()
handle($input, $output, $style);
当命令运行时执行代码的主要方法。这些参数为您提供访问 Symfony 控制台 I/O 的权限。有关 I/O 使用的示例,请参阅 symfony 控制台文档。
import(string $key);
获取应用容器中存储的值。导入的值应仅用作默认选项或建议,因为如果尚未执行给定插件的命令,它们可以为null。值只能从同一插件内部导入。对于跨插件导入,请使用占位符参数或环境变量。
recommendsExecuting();
此方法应返回一个数组,包含应在运行此命令之前执行的命令的类名。当用户独立运行命令或作为不先执行推荐方法的过程的一部分时,将通知用户推荐值。
requiresExecuting();
此方法应返回一个数组,包含必须在运行此命令之前执行的命令的类名。如果尝试执行此命令之前未执行所需命令的过程,将抛出异常。如果在此过程之外尝试执行此命令,也将抛出异常。
测试
要测试在您的插件中运行命令,您可以使用来自jinitialize-core的扩展PHPunit TestCase类。使用registerLocalPlugin($pathToComposer)来注册定义在您的composer.json文件中的插件。如果覆盖了TestCase的setUp()或tearDown()方法,请勿忘记调用父方法。
<?php namespace Tests\Unit; use Nonetallt\Jinitialize\Testing\TestCase; class ExampleTest extends TestCase { public function testExample() { // The arguments and options accepted by the command $args = [ 'arg1' => 'value', 'arg2' => 'value', '--option1' => 'value' ]; // The values the user would input in order they are asked for by the command $input = [ 'userInput1', 'userInput2' ]; // Running commands by class name $this->runCommand(ExampleCommand::class, $args, $input); // Running commands using their method signature $this->runCommand('plugin:command', [], $input); } protected function setUp() { parent::setUp(); $this->registerLocalPlugin(__DIR__.'/../../composer.json'); } }
方法
- runCommand(string $command, array $args = [], array $input = []);
- runCommandsAsProcedure(array $commands, array $args = [], array $input = []);
传递给runCommand和runCommandsAsProcedure的命令可以是完全限定的类名,或者使用与通常使用jinitialize相同的语法进行方法调用的调用。
$this->runCommand(MyCommand::class, ['arg1' => 1, '--option1' => 2]); $this->runCommand('plugin:command 1 --option1=2');
当$command或$commands是类名时,才应使用$args参数。当命令是签名调用时,将使用调用的参数,而忽略$args。
断言
扩展的TestCase类目前有以下自定义断言
- assertContainerEquals(array $data)
- assertContainerContains(array $data)
这些可以用来定义导出变量的状态。