gitory/pimple-cli

Pimple应用的CLI命令

v1.0.0 2014-09-02 20:09 UTC

This package is auto-updated.

Last update: 2024-09-24 19:29:42 UTC


README

Build Status Latest Stable Version License Total Downloads Scrutinizer Code Quality

PimpleCli在Packagist上的页面

PimpleCli是一个易于创建命令行应用程序的工具。

PimpleCli与Pimple容器(例如:Silex应用程序)和命令行应用程序(例如:使用Symfony的Console组件)一起工作。PimpleCli的作用是发现Pimple容器中的命令,使它们可在命令行应用程序中使用。

命令需要注册为以'.command'结尾的服务。命令可以是命令行应用程序理解的任何东西(类、可调用函数等)。当使用Symfony\Component\Console\Application时,命令应该扩展Symfony\Component\Console\Command\Command。您可以查看Console组件文档以开始。

安装

通过Composer

{
    "require": {
        "gitory/pimple-cli": "~1.0"
    }
}

示例

Silex 2

composer.json

{
    "require": {
        "silex/silex": "~2.0@dev",
        "gitory/pimple-cli": "~1.0",
        "symfony/console": "~2.0"
    }
}

GreetCommand.php

namespace Acme\DemoBundle\Command;

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

class GreetCommand extends Command
{
    protected function configure()
    {
        $this
            ->setName('demo:greet')
            ->setDescription('Greet someone')
            ->addArgument('name', InputArgument::REQUIRED, 'Who do you want to greet?')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $name = $input->getArgument('name');
        $text = 'Hello '.$name;
        $output->writeln($text);
    }
}

index.php

require_once __DIR__.'/vendor/autoload.php';
require_once 'GreetCommand.php';

$silexApp = new Silex\Application();
$silexApp->register(new Gitory\PimpleCli\ServiceCommandServiceProvider());

// add your command as services ending in '.command' in your DI
$silexApp['user.new.command'] = function () {
    return new Acme\DemoBundle\Command\GreetCommand();
};

$consoleApp = new Symfony\Component\Console\Application();
$consoleApp->addCommands($silexApp['command.resolver']->commands());
$consoleApp->run();

在CLI中启动:php index.php demo:greet John

command command