herrera-io/cli-app 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

herrera-io/cli-app

最新稳定版本:2.0.0

Composer 安装命令:

composer require herrera-io/cli-app

包简介

A service container-based Symfony Console application template.

关键字:

README 文档

README

Build Status

CLI App is a template for creating console applications based on Symfony Console and the Herrera.io Service Container. Think Silex, but for console applications, and easier.

Example

use Herrera\Cli\Application;

$app = new Application(
    array(
        'app.name' => 'MyApp',
        'app.version' => '1.2.3',
    )
);

$myCommand = $app->add(
    'myCommand',
    function ($in, $out) {
        $out->writeln('Hello, ' . $in->getArgument('name') . '!');

        return 123;
    }
);

$myCommand->addArgument('name');

$app->run();

Running the example:

$ php myApp.php myCommand world
Hello, world!

Installation

Add it to your list of Composer dependencies:

$ composer require "herrera-io/cli-app=~2.0"

Usage

Creating a new application is as simple as instantiating the Application class. The class itself is an extension of the Container class from the Herrera.io service container library.

use Herrera\Cli\Application;

$app = new Application(
    array(
        'app.name' => 'Example',
        'app.version' => '1.0',
    )
);

The purpose of app.name and app.version will be later discussed in the section titled Configuration. It is one of many customizable options for the application.

Default Services

When the application is instantiated two services are registered:

  • Herrera\Cli\Provider\ErrorHandlingServiceProvider — Replaces the current error handler with one provided by the service. The custom error handler will simply convert all errors into instances of ErrorException and throw them. The handler will respect the current error_reporting() setting.
  • Herrera\Cli\Provider\ConsoleServiceProvider — The console service provider that is used by the application to configure, add commands, add helpers, and run.

The Application class is designed so that you can replace the default registered services by overriding a single method. You can also extend the method to register additional default services.

class CustomApplication extends Application
{
    /**
     * @override
     */
    protected function registerDefaultServices()
    {
        parent::registerDefaultServices();

        $this->register(new Service());
    }
}

Adding a Command

To add a new command to the application, you will need to call the add() method. This method will create a new command and return it for further, optional, configuration. The command returned is an instance of the Symfony\Component\Console\Command\Command class.

$command = $app->add(
    'commandName',
    function ($in, $out) {
        // command code
    }
);

$command->addArgument('argumentName');

Adding a Helper

To add a helper to the application, you will need to call the set() method. This method will register the helper with the current helper set. Any instance of Symfony\Component\Console\Helper\HelperInterface is accepted.

$app->set(new Helper());

App Container as Helper

The Application container is registered as a helper in the console instance. This will make it easier to access the container in order to use other services within a command that extends the Command class.

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CustomCommand extends Command
{
    protected function configure()
    {
        $this->setName('customCommand');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $app = $this->getHelperSet()->get('app');
    }
}

Running

Running the application is simple:

$status = $app->run();

If auto exiting is disabled, $status will hold the exit status code.

Configuration

The majority of the all available configuration parameters and servics lie within the ConsoleServiceProvider that is registered with the Application service container. The default parameters and services can be modified until the console service is used. Any further changes will not take any effect.

These are the default console parameters:

use Symfony\Component\Console\Output\ConsoleOutput;

array(
    // the name of the application
    'app.name' => 'UNKNOWN',

    // the version of the application
    'app.version' => 'UNKNOWN',

    // automatically exit once the app has run?
    'console.auto_exist' => true,

    // the overriding list of $_SERVER['argv'] values
    'console.input.argv' => null,

    // the default array of input definitions
    'console.input.definition' => null,

    // the default verbosity level
    'console.output.verbosity' => ConsoleOutput::VERBOSITY_NORMAL,

    // the default "use decorator" flag
    'console.output.decorator' => null,
)

These are the default console services:

// the Symfony `Console` instance
$app['console'];

// creates new `Command` instances
$app['console.command_factory'];

// the Symfony `ArgvInput` instance
$app['console.input'];

// the Symfony `ConsoleOutput` instance
$app['console.output'];

// the Symfony `OutputFormatter` instance
$app['console.output.formatter'];

// runs `Application->run()` with input and output services
$app['console.run'];

herrera-io/cli-app 适用场景与选型建议

herrera-io/cli-app 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 783 次下载、GitHub Stars 达 11, 最近一次更新时间为 2013 年 02 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-02-05