beryllium/llama 问题修复 & 功能扩展

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

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

beryllium/llama

Composer 安装命令:

composer require beryllium/llama

包简介

Llama Commander lets you use anonymous functions as console commands via the Symfony Console Component.

README 文档

README

Build Status

Llama Commander lets you use anonymous functions as console commands via the Symfony Console Component.

Why does it exist?

Llama Commander allows developers to define Symfony Console Commands inline instead of having to create a class for each command. When developers are using the Console in a microframework or no-framework context, they may benefit from this flexibility.

What is the spec?

The class LlamaCommand takes the following parameters:

  • $name: The name for the command (optional on instantiation, but must be set in the constructor or via the ->setName() or ->configure() methods before the command can be invoked)
  • $configurator: An optional callable function to configure the command. This can be used to set the Description, Options, and Arguments for the console command.
  • $executor: The core logic for the console command. This callable will receive the input and return the output at the core of the console command being written.
  • $interactor: An optional callable that defines user interaction logic.
  • $initializer: An optional callable that initializes parameters before interaction and execution.

How do I use it?

When you initialize a new LlamaCommand, you pass it at minimum a name (string) and an "Executor" lambda (callable) that determines what the command does. You can also pass it a Configurator, an Initializer, or an 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();

If your command needs to have options or arguments, you can specify an anonymous Configurator function for that:

$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
    }
));

If you want your code to be more precise, you can also typehint the main lambda. This will help your IDE give you hints about how to interact with $input and $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();

Combining the above examples, you would register the command with the console application like so:

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();

And that's all for now. If you would like to learn more about how to use the Symfony Console Component, the Symfony website has a very helpful documentation page about it.

beryllium/llama 适用场景与选型建议

beryllium/llama 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 96 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 10 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 beryllium/llama 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-17