8000 [Console] Always use lazy commands? · Issue #8843 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Console] Always use lazy commands? #8843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
javiereguiluz opened this issue Dec 6, 2017 · 2 comments
Closed

[Console] Always use lazy commands? #8843

javiereguiluz opened this issue Dec 6, 2017 · 2 comments
Assignees
Labels
actionable Clear and specific issues ready for anyone to take them. Console hasPR A Pull Request has already been submitted for this issue.
Milestone

Comments

@javiereguiluz
Copy link
Member

This issue was reported in the Symfony Slack.

  1. A user was reading https://symfony.com/doc/current/console.html
  2. Then he run make:command
  3. He didn't see ->setName('...') in the generated command, so he added ->setName('app:mycommand') himself.
  4. Then he saw this error: ">>>There are no commands defined in the "app:mycommand" namespace"

The problem is:

  1. MakerBundle generates lazy commands by default (https://github.com/symfony/maker-bundle/blob/bf97703ddb68c6b37bd6bab5f5ebd5c7542ca1ef/src/Resources/skeleton/command/Command.tpl.php#L14), as explained in https://symfony.com/doc/3.4/console/commands_as_services.html#lazy-loading
  2. But most of the console docs don't use lazy commands.

What should we do here? Should we change all console docs to always use lazy commands?

@vekien
Copy link
Contributor
vekien commented Dec 7, 2017

Symfony 4.0

I've just encountered this exact same error message without using Maker but instead following the docs everything works up until you require services, unsure if this is a similar issue and the error message is not clear. For example:

This works fine

<?php

namespace App\Command;

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

class TestCommand extends Command
{
    protected function configure()
    {
        $this
            ->setName('app:test')
            ->setDescription('Test command');
        ;
    }

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

This errors with message: There are no commands defined in the "app" namespace.

<?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use App\Service\Test;

class TestCommand extends Command
{
    private $test;

    public function __construct(Test $test)
    {
        $this->test = $test;
    }

    protected function configure()
    {
        $this
            ->setName('app:test')
            ->setDescription('Test command');
        ;
    }

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

This should work according to: https://symfony.com/doc/current/console.html#getting-services-from-the-service-container "Getting Services from the Service Container"

PHPStorm notices error: Missing parent constructor call, there is a __construct() in Command which is extended.

You can change your constructor to:

public function __construct(Test $test)
{
    parent::__construct();
    $this->test = $test;
}

Which works... Don't really like the parent call.

The main frustrations I ran into it:

  • Following the docs will lead you to this problem
  • The error message is not obvious, if I didn't have a good IDE I would be confused...

Using Maker

If you use Maker it should probably be stated somewhere not to add setName, do you even need it? I can run a command without it:

php bin/console make:command TestTwo
php bin/console TestTwo
// [OK] You have a new command! Now make it your own! Pass --help to see your options.

Once you add setName you get the error the OP stated. You will also run into the same issue I am having as soon as you try auto inject services via the constructor. (2 issues causing the same error)

weaverryan added a commit that referenced this issue Dec 13, 2017
…ice Container" section (viion)

This PR was submitted for the master branch but it was merged into the 3.4 branch instead (closes #8849).

Discussion
----------

Add constructor call to the "Getting Services from the Service Container" section

Parent constructor call is required when using auto-wired services in a command constructor, without it you will receive the error:

```
There are no commands defined in the "app" namespace.
```

Some discussion of what I ran into: #8843

Commits
-------

88fd7a7 Update console.rst
@nicolas-grekas
Copy link
Member

Yes please, lazy commands all the time!

@HeahDude HeahDude added the actionable Clear and specific issues ready for anyone to take them. label Jul 1, 2018
@HeahDude HeahDude added this to the 3.4 milestone Jul 1, 2018
@javiereguiluz javiereguiluz self-assigned this Jul 24, 2019
@javiereguiluz javiereguiluz added the hasPR A Pull Request has already been submitted for this issue. label Jul 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actionable Clear and specific issues ready for anyone to take them. Console hasPR A Pull Request has already been submitted for this issue.
Projects
None yet
Development

No branches or pull requests

4 participants
0