0

I am using PhpStorm with Symfony. What I am trying to do is to debug a Symfony command from inside the IDE by using the debug button (Shift + F9).

I am getting the following error.

PHP Fatal error: Class 'Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand' not found in /home/user/Projects/project1/symfony/src/AppBundle/Command/testScriptCommand.php on line 8 PHP Stack trace:

It's weird as I have followed the Symfony documentation for creating commands and I have included the following classes:

<?php

namespace AppBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class testScriptCommand extends ContainerAwareCommand
{
    protected function configure(): void
    {
        $this->setName('app:test-script');
    }

    protected function execute(InputInterface $input, OutputInterface $output): void
    {
        echo 1;
    }
}

The debugger works inside the IDE until line 8 and once try to continue it fails with the already mentioned fatal error.

It seems to me as line 4 is not actually importing the ContainerAwareCommand that is needed.

Any ideas?

4
  • 1
    If you run this file directly, then you don't have composer's autoloaders loaded, therefore, PHP couldn't find ContainerAwareCommand. And by the way - use doesn't mean "importing " anything. It just let you use class name instead of full qualified class names every time in further code. Commented Mar 9, 2019 at 9:17
  • 1
    Also you shouldn't try to execute this file at all. It's just a class and it won't execute anything, but just load the class. You should debug bin/console with argument app:test-script Commented Mar 9, 2019 at 9:20
  • @JakubMatczak you are right in your comments. What I was tried to achieve was to use the debugger inside a Symfony command. So far I found out how to debug by executing the command in the CLI that immediately redirects me to PhpStorm. [stackoverflow.com/questions/26081522/…. As it seems this way Symfony loads along with all it's dependencies. I do not know if there is a another way so that I could execute it from the debug script button (Shift + F9) but anyway this one works fine. Commented Mar 9, 2019 at 22:25
  • @spyrAlex Create and use Run/Debug Configuration of "PHP Script" type, then select it and hit "Debug" button. See comment #2 by Jakub Commented Mar 10, 2019 at 12:06

2 Answers 2

2

Extend Symfony\Component\Console\Command\Command

Dependency Inject the ContainerInterface with your commands constructor, something like this - in my case using autowired services:

    /** @var ContainerInterface $container */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        parent::__construct();
        $this->container = $container;
    }

Then you should be able to call fe. $this->container->getParameter('project.parameter')

Sign up to request clarification or add additional context in comments.

Comments

0

What documentation did you followed?

For creating Commands you need to extend Command, no ContainerAwareCommand

// src/Command/CreateUserCommand.php
namespace App\Command;

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

class CreateUserCommand extends Command
{
    // the name of the command (the part after "bin/console")
    protected static $defaultName = 'app:create-user';

    protected function configure()
    {
        // ...
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // ...
    }
}

For more information: https://symfony.com/doc/current/console.html

EDIT:

Adding info...

ContainerAwareCommand is for Symfony version <= 2.6 https://symfony.com/doc/2.6/cookbook/console/console_command.html Soooo old

1 Comment

Thank you for your interest. Since sometime ago I was using Symfony 3.3 and so I used this link from their official documentation. symfony.com/doc/3.3/console.html We have updated to Symdony 3.4 and now it's not in the documentation. However the ContainerAwareCommand class continues to exists after the upgrade and works nicely using the CLI. So far I was only able to debug the script by executing the command from the CLI. I was wondering if there is a way to do debug button (Shift + F9).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.