Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There are no commands defined in the "xxx" namespace

Tags:

php

symfony

I had big project on symfony3. In this project I have ProjectFrameworkBundle.

Project/FrameworkBundle/Console/Command.php

abstract class Command extends ContainerAwareCommand
{
    //...

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // do some regular staff
        $exitCode = $this->executeCommand($input, $output);
        // do some regular staff
    }

    abstract protected function executeCommand(InputInterface $input, OutputInterface $output);

    //...
}

If I'll make any command, extended from Command class, it will work ok (tested).

But, I have another bundle

Project/FrameworkQueue/Console/Command.php

use Project\FrameworkBundle\Console\Command as BaseCommand;
abstract class Command extends BaseCommand
{
    // ...
    protected function executeCommand(InputInterface $input, OutputInterface $output)
    {
        // do some regular staff
        $exitCode = $this->executeJob($input, $output);
        // do some regular staff
    }

    abstract protected function executeJob(InputInterface $input, OutputInterface $output);

    // ...
}

So, when I change amy Command from extends Project\FrameworkBundle\Console\Command to extends Project\QueueBundle\Console\Command it hides from commands list. I tried to delete all staff from executeCommand in QueueBundle, but it not helped me. But if I making any mistake in php code in this command, I see exceptions.

What wrong? Where is my mistake or this is a bug. Where can I found symfony code that collecting and checking available commands?

Thanks!

P.S. Problem is not in file or class naming - I checked it many times. Of course when I changing parent class, I changing function names.

like image 515
Leonid Zakharov Avatar asked Aug 02 '16 04:08

Leonid Zakharov


2 Answers

The problem was in overriding __construct method in QueueBundle\Console\Command. If you will try to do this:

public function __construct($name)
{
    parent::__construct($name);
}

... it will not work. I don't know why, but I moved some logic from here to "before execute" action.

Thanks all!

like image 62
Leonid Zakharov Avatar answered Nov 04 '22 18:11

Leonid Zakharov


If a command extends the ContainerAwareCommand, Symfony will even inject the container. But if not - you have to register your command as service.

# app/config/config.yml services:

   app.command.your_command:
      class: Project\FrameworkBundle\Command\Console\YourCommand
      tags:
          -  { name: console.command }

During compiling kernel Symfony find your command by tag console.command and inject to application command list

To check detail information about this topic you can check official doc - https://symfony.com/doc/current/console/commands_as_services.html

like image 31
Rinat Avatar answered Nov 04 '22 17:11

Rinat