承接 ssnepenthe/apheleia-cli 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ssnepenthe/apheleia-cli

Composer 安装命令:

composer require ssnepenthe/apheleia-cli

包简介

An alternative syntax for writing WP-CLI commands

README 文档

README

Apheleia CLI provides an alternate approach to writing WP-CLI commands. It eliminates the need for docblock command definitions and should allow you to take full advantage of the autocomplete features in your favorite editor.

The syntax for Apheleia commands is loosely modeled after the symfony/console package.

Warning

This package is currently in development and is subject to breaking changes without notice until v1.0 has been tagged.

It is one in a series of WordPress toys I have been working on with the intention of exploring ways to modernize the feel of working with WordPress.

As the label suggests, it should be treated as a toy.

Installation

composer require ssnepenthe/apheleia-cli

Usage

I think this is best explained through examples, so let's demonstrate some different ways we might implement the example hello command from the WP-CLI handbook commands cookbook.

The preferred approach is to create self-contained command classes.

Extend the Command class using the configure method to define the command signature and the handle method to define the command logic to be executed when the command is called:

use ApheleiaCli\Argument;
use ApheleiaCli\Command;
use ApheleiaCli\Option;
use WP_CLI;

class HelloCommand extends Command
{
    public function configure(): void
    {
        $this->setName('example hello')
            ->setDescription('Prints a greeting.')
            ->addArgument(
                (new Argument('name'))
                    ->setDescription('The name of the person to greet.')
            )
            ->addOption(
                (new Option('type'))
                    ->setDescription('Whether or not to greet the person with success or error.')
                    ->setDefault('success')
                    ->setOptions('success', 'error')
            )
            ->setUsage("## EXAMPLES\n\n\twp example hello newman")
            ->setWhen('after_wp_load');
    }

    public function handle($args, $assocArgs)
    {
        [$name] = $args;

        $type = $assocArgs['type'];
        WP_CLI::$type("Hello, $name!");
    }
}

Commands are registered using the CommandRegistry.

use ApheleiaCli\CommandRegistry;

$registry = new CommandRegistry();

$registry->add(new HelloCommand());

$registry->initialize();

There is a significant difference between the command we have created and the original version: WP-CLI does not have any description text to display for the parent example command when you run wp help example.

There are a couple of different approaches we can take to fix this.

The first is to register a dedicated namespace alongside our HelloCommand:

$registry->namespace('example', 'Implements example command.');
$registry->add(new HelloCommand());

The second is to take advantage of command groups.

First we need to remove the parent command portion of our command name:

class HelloCommand extends Command
{
    public function configure(): void
    {
        $this->setName('hello')
            // etc...
            ;
    }

    // ...
}

And then we use the group method on our registry. The callback provided to the group method will receive a registry instance that has been scoped such that any commands added within the callback will automatically be registered as children of the example command:

$registry->group('example', 'Implements example command.', function (CommandRegistry $registry) {
    $registry->add(new HelloCommand());
});

It is also possible to define a command without extending the Command class:

$registry->add(
    (new Command())
        ->setName('hello')
        ->setDescription('Prints a greeting.')
        ->addArgument(
            (new Argument('name'))
                ->setDescription('The name of the person to greet.')
        )
        ->addOption(
            (new Option('type'))
                ->setDescription('Whether or not to greet the person with success or error.')
                ->setDefault('success')
                ->setOptions('success', 'error')
        )
        ->setUsage("## EXAMPLES\n\n\twp example hello newman")
        ->setWhen('after_wp_load')
        ->setHandler(function ($args, $assocArgs) {
            [$name] = $args;

            $type = $assocArgs['type'];
            WP_CLI::$type("Hello, $name!");
        })
);

Advanced Usage

By default, command handlers should be written more-or-less the same as they would if you were working directly with WP-CLI. That is to say they should always expect to receive a list of command arguments as the first parameter and an associative array of command options as the second:

$command->setHandler(function (array $args, array $assocArgs) {
    // ...
});

However, commands can modify handler signatures by overriding their handlerInvokerClass property.

This package only ships with one alternative handler invoker: the PhpDiHandlerInvoker. It uses the php-di/invoker package to call command handlers.

Before it can be used, you must install php-di/invoker:

composer require php-di/invoker

Then set the handler invoker on your command (or a base command from which all of your commands extend):

use ApheleiaCli\Invoker\PhpDiHandlerInvoker;

class HelloCommand extends Command
{
    protected $handlerInvokerClass = PhpDiHandlerInvoker::class;

    // ...
}

With this in place, command handlers can now ask for command parameters by name:

class HelloCommand extends Command
{
    // ...

    public function handle($name, $type)
    {
        WP_CLI::$type("Hello, $name!");
    }
}

ssnepenthe/apheleia-cli 适用场景与选型建议

ssnepenthe/apheleia-cli 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 863 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 07 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ssnepenthe/apheleia-cli 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ssnepenthe/apheleia-cli 我们能提供哪些服务?
定制开发 / 二次开发

基于 ssnepenthe/apheleia-cli 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 863
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-07-04