Symfony2自定义控制台命令无法正常工作

时间:2011-09-13 16:13:51

标签: symfony

我在src / MaintenanceBundle / Command中创建了一个新类,将其命名为GreetCommand.php并将以下代码放入其中:

<?php

namespace SK2\MaintenanceBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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 ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('maintenance:greet')
            ->setDescription('Greet someone')
            ->addArgument('name', InputArgument::OPTIONAL, 'Who do you want to greet?')
            ->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters')
        ;
    }

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

        if ($input->getOption('yell')) {
            $text = strtoupper($text);
        }

        $output->writeln($text);
    }
}

?>

试图通过

来调用它
  

app / console maintenance:问候Fabien

但我总是得到以下错误:

  

[InvalidArgumentException]   “maintenance”命名空间中没有定义命令。

有什么想法吗?

11 个答案:

答案 0 :(得分:55)

我遇到了这个问题,这是因为我的PHP类和文件的名称并没有以Command结尾。

Symfony将自动注册以Command结尾并位于捆绑包的Command目录中的命令。如果您想手动注册您的命令,这本食谱条目可能会有所帮助:http://symfony.com/doc/current/cookbook/console/commands_as_services.html

答案 1 :(得分:16)

我弄清楚它为什么不起作用:我只是忘了在AppKernel.php中注册Bundle。但是,其他提议的答案是相关的,可能有助于解决其他情况!

按照惯例:命令文件需要驻留在bundle的命令目录中,并且名称以Command结尾。

在AppKernel.php中

public function registerBundles()
{
    $bundles = [
        ...
        new MaintenanceBundle\MaintenanceBundle(),
    ];

    return $bundles;
}

答案 2 :(得分:15)

我遇到了类似的问题并想出了另一种可能的解决方案:

如果覆盖默认的__construct方法,Symfony将不会自动注册Command,因此您必须采用前面提到的服务方法或删除__construct覆盖并进行初始化执行方法或配置方法中的步骤。

实际上是否有人知道如何做init&#34; stuff&#34;在Symfony命令中?

我花了一点时间来解决这个问题。

答案 3 :(得分:2)

除了MonocroM的答案之外,我的命令也遇到了同样的问题,Symfony默默地忽略了它,因为我的命令构造函数有1个必需的参数。

我刚删除它并调用父__construct()方法(Symfony 2.7)并且效果很好;)

答案 4 :(得分:1)

我认为您必须在parent::configure()方法

中致电configure

答案 5 :(得分:1)

就我而言,它正在抱怨&#34;工作流程&#34;虽然框架正确提供了WorkflowDumpCommand,但是命名空间。

但是,它无法运行,因为我没有定义任何工作流,因此命令的isEnabled()方法返回false

答案 6 :(得分:0)

如果您正在使用命令构造函数,并且正在使用延迟加载/自动装配,那么您的命令将不会自动注册。要解决此问题,您可以添加$defaultName变量:

class SunshineCommand extends Command
{
    protected static $defaultName = 'app:sunshine';

    // ...
} 

链接到Symfony docs

答案 7 :(得分:0)

我尝试在configure方法内使用通过构造函数传递的服务:

class SomeCommand extends Command {

    private $service;

    public function __construct(SomeService $service) {
        $this->service = $service;
    }

    protected function configure(): void {
        $this->service->doSomething(); // DOES NOT WORK
    }
}

答案 8 :(得分:0)

当我尝试使用PHPUnit测试命令执行时,我遇到了同样的错误。

这是由于错误的类导入:

# if you have the cluster object
cluster.dashboard_link

# or if you have a client
client.dashboard_link

应该是

use Symfony\Component\Console\Application;

cf。 Other stack thread

答案 9 :(得分:0)

Symfony使用自动配置功能自动将依赖项注入到您的服务中,并将您的服务注册为Command,event等。

因此,首先请确保您在配置文件夹中有services.yaml。使用autoconfigure:true。
这是默认设置

然后确保所有文件与班级名称完全相同。 因此,如果您具有SimpleClass,则文件必须为SimpleClass.php

答案 10 :(得分:0)

如果由于__constructor而有问题, 转到services.yml并添加如下内容:

const automation = require('../automations/fake.automation');

// MOCKS
const mockedBlingProduct = require('../mocks/bling-product.mocks.json');
const mockedShopifyCreatedProduct = require('../mocks/shopify-created-product.mocks.json');

// SERVICES
const BlingProductService = require('../services/bling-product.service');
const ShopifyProductService = require('../services/shopify-product.service');

describe('Automation test', () => {
  beforeAll(() => {
    jest.spyOn(BlingProductService.prototype, 'list').mockImplementation(() => Promise.resolve(mockedBlingProduct));

    jest.spyOn(ShopifyProductService.prototype, 'list').mockImplementation(() => Promise.resolve(mockedShopifyCreatedProduct));
  });

  afterAll(() => {
     jest.restoreAllMocks();
  });

  it('should return status SUCCESS', async () => {
    const result = await automation.run();
    expect(result).toEqual({ status: 'SUCCESS' });
  });
});