定制 nowise/uup-application-options 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

nowise/uup-application-options

Composer 安装命令:

composer require nowise/uup-application-options

包简介

Uniform CLI command line and HTTP request options.

README 文档

README

Supports transparent/uniform handling of runtime options from CLI command and HTTP request.

This package supports short/long options, other options and reading password from terminal (masked echo output). For HTTP request options, an optional filter can be applied.

USAGE:

In your command action class, define a method that checks whether to return command line options (CLI) or from request option (HTTP).

private function getApplicationOptions(): ApplicationOptionsInterface
{
    if (php_sapi_name() == 'cli') {
        return new CommandLineOptions();
    } else {
        return new HttpRequestOptions();
    }
}

Calling getApplicationOptions will provide uniform access to application options, whether these come from command line or the HTTP request. From your application perspective the origin of options is transparent.

From within the application, a number of convenient methods can be used for checking if options was passed and retrieve them with type safety.

public function setup() 
{
    $this->options = $this->getApplicationOptions();
}

public function execute(): void 
{
    // 
    // Example of setting defaults:
    // 
    if ($this->options->isMissing('user')) {
        $this->options->setOption('user', 'adam');
    }
    
    // 
    // Take action if option is defined:
    // 
    if ($this->options->hasOption('email')) {
        $this->sendEmail($this->options->getString('email'));
    }
}

A number of other getters exist, for example for boolean, float and integer values. These takes a second default value that is returned of option is missing.

ORIGIN:

If origin matters, it can be checked:

if ($this->options->getOrigin() == ApplicationOptionsInterface::ORIGIN_HTTP) {
    throw new RuntimeException("This action can't be run from a HTTP context");
}

OPTION ALIAS:

For command line options, the default behavior is to return options by stripping leading dashes ('-'). To support short command options mapped to long options, pass a mapping array.

private function getApplicationOptions(): ApplicationOptionsInterface
{
    if (php_sapi_name() == 'cli') {
        return new CommandLineOptions([
            '-f' => 'file',
            '-U' => 'user'
        ]);
    } else {
        return new HttpRequestOptions();
    }
}

These two short option will now be an alias for their equivalent long option. Some builtin aliases are implicit handled for command line options. These short options are:

  • -h => help
  • -V => version
  • -d => debug
  • -v => verbose
  • -q => quiet

These are processed before user defined mappings, making it possible to easily redefine the builtin mapping.

OPTION VALUES:

Option values are any value after the equal character ('='). For options without a value, the option key will be read having boolean true as its value.

DASHES:

Processing of command line options (CLI) will strip leading dashes and use the remaining string as the command option key.

OPTION FILTERING:

Command line options are considered safe. For HTTP request, it's possible to pass an array of sanitation filter to be applied.

private function getApplicationOptions(): ApplicationOptionsInterface
{
    if (php_sapi_name() == 'cli') {
        return new CommandLineOptions();
    } else {
        return new HttpRequestOptions(
            new HttpRequestFilter([
                'user'  => FILTER_SANITIZE_STRING,
                'email' => FILTER_SANITIZE_EMAIL
            ])
        );
    }
}

The default behavior is to not filter HTTP request options. For larger applications, some framework for purify HTML input might be used that could be wrapped in a class that implements the FilterInterface and used instead of passing an instance of the HttpRequestFilter class.

BOOLEANS:

Special treatment of boolean options are implemented. For example, option values "1", "true", "on" and "yes" yields true. Analogous "0", "false", "off" and "no" yields false.

Example: Call getBoolean to have the value for filter option evaluated as boolean.

$this->options->getBoolean("filter");

nowise/uup-application-options 适用场景与选型建议

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

它主要适用于以下技术方向: 「http」 「cli」 「command」 「uup」 「application-options」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 nowise/uup-application-options 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2022-01-07