beryllium/llama

Llama Commander 允许您通过 Symfony Console 组件使用匿名函数作为控制台命令。

0.3.1 2016-07-28 06:08 UTC

This package is auto-updated.

Last update: 2024-08-29 03:26:16 UTC


README

Build Status

Llama Commander 允许您通过 Symfony Console 组件使用匿名函数作为控制台命令。

为什么存在它?

Llama Commander 允许开发者直接定义 Symfony Console 命令,而不必为每个命令创建一个类。当开发者在微框架或无框架环境下使用控制台时,可能会从中受益。

规范是什么?

LlamaCommand 类接受以下参数

  • $name:命令的名称(实例化时可选,但必须在构造函数中或通过 ->setName() 或 ->configure() 方法设置,才能调用命令)
  • $configurator:一个可选的可调用函数,用于配置命令。这可以用来设置控制台命令的描述、选项和参数。
  • $executor:控制台命令的核心逻辑。这个可调用函数将接收输入并返回输出,这是编写控制台命令的核心。
  • $interactor:一个可选的可调用函数,用于定义用户交互逻辑。
  • $initializer:一个可选的可调用函数,用于在交互和执行之前初始化参数。

如何使用它?

当你初始化一个新的 LlamaCommand 时,至少需要传递一个名称(字符串)和一个“Executor” lambda(可调用),以确定命令执行的操作。你还可以传递一个 Configurator、Initializer 或 Interactor。

$app     = new Application;        // Silex application
$console = new ConsoleApplication; // Symfony console component

/* ... extra application setup, defining the pheanstalk service, etc ... */

$console->add(new Beryllium\Llama\LlamaCommand(
    'queue:listen',     // Start a queue listener
    null,               // No configuration at this time
    function ($input, $output) use ($app, $console) {
        do {
            $job = $app['pheanstalk']
              ->watch('testtube')
              ->ignore('default')
              ->reserve();

            $output->writeln('Raw data: ' . $job->getData());
            $app['pheanstalk']->delete($job);
        } while (strtolower($job->getData()) !== 'halt');
    }
));

$console->run();

如果你的命令需要选项或参数,你可以指定一个匿名 Configurator 函数

$console->add(new Beryllium\Llama\LlamaCommand(
    'queue:listen',     // Start a queue listener
    function ($config) use ($app, $console) {
        $config->setDescription('Listen for stuff to do')
               ->addArgument(
                   'items',
                   InputArgument::OPTIONAL,
                   'How much stuff to listen for'
               );
    },
    function ($input, $output) use ($app, $console) {
        // ... command code goes here
    }
));

如果你希望你的代码更加精确,你也可以为主要的 lambda 类型提示。这将帮助你的 IDE 给你有关如何与 $input 和 $output 交互的提示

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

$app     = new Application;        // Silex application
$console = new ConsoleApplication; // Symfony console component

/* ... extra application setup ... */

$console->add(new Beryllium\Llama\LlamaCommand(
    'queue:listen',     // Start a queue listener
    null,               // No configuration at this time
    function (InputInterface $input, OutputInterface $output) use ($app, $console) {
        // ... now $input and $output are type-hinted
    }
));

$console->run();

结合上述示例,你将这样向控制台应用程序注册命令

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

$app     = new Application;        // Silex application
$console = new ConsoleApplication; // Symfony console component

/* ... extra application setup, defining the pheanstalk service, etc ... */

$console->add(new Beryllium\Llama\LlamaCommand(
    'queue:listen',
    function ($config) use ($app, $console) {
        $config->setDescription('Listen for stuff to do')
               ->addArgument(
                   'items',
                   InputArgument::OPTIONAL,
                   'How much stuff to listen for'
               );
    },
    function (InputInterface $input, OutputInterface $output) use ($app, $console) {
        $pheanstalk = $app['pheanstalk'];

        do {
            $job = $pheanstalk
              ->watch('testtube')
              ->ignore('default')
              ->reserve();

            $output->writeln('Raw data: ' . $job->getData());
            $pheanstalk->delete($job);
        } while (strtolower($job->getData()) !== 'halt');
    }
));

$console->run();

到此为止。如果您想了解更多关于如何使用 Symfony Console 组件的信息,Symfony 网站有一个非常有用的文档页面