mpoiriert/nucleus-console-bundle

允许在服务方法上创建一个带有注释的命令

安装: 702

依赖项: 1

建议者: 1

安全性: 0

星标: 0

关注者: 3

分支: 0

开放问题: 0

类型:symfony-bundle

dev-master 2014-03-26 12:03 UTC

This package is auto-updated.

Last update: 2024-09-09 13:27:22 UTC


README

Build Status

在服务方法上使用注释,您可以通过CLI创建可访问的命令。

要在您的应用程序中使用它,您必须注册2个包,因为存在对 nucleus-bundle 的依赖。

<?php

// in AppKernel::registerBundles()
$bundles = array(
    // ...
    new Nucleus\Bundle\CoreBundle\NucleusCoreBundle(),
    new Nucleus\Bundle\BinderBundle\NucleusConsoleBundle(),
    // ...
);

完成后,您可以在服务的任何公共方法上使用 Nucleus\Console\CommandLine 注释。

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

class TestService
{
    /**
     * Comment from the method
     *
     * @\Nucleus\Console\CommandLine(name="my:test:hello")
     *
     * @param string $name The name of the person you want to say hello to
     * @param OutputInterface $output
     * @param InputInterface $output
     * @param Command $parentCommand
     */
    public function hello($name, OutputInterface $output, InputInterface $input, Command $parentCommand)
    {
        $output->write('Hello ' . $name . ' !');
    }
}

命令的描述将从 phpDoc 的第一行解析。所有参数都被视为选项(意味着您需要使用 --optionName 来设置它),您可以定义一个默认值如果该选项是可选的。@param 前面的注释将被用于您命令的 --help。您不需要遵循任何接口,例如在 symfony 中的 Controller,所有参数都将根据其名称或强类型注入。由于服务不需要扩展 symfony 的 Command 类,您可以从 "parentCommand" 注入一个 "父命令",从而可以访问从 Command 对象公开访问的所有内容(例如,如果您想访问助手...)。

如果您没有指定注释的 name 属性,则将使用服务名称后面的方法名称。