Skip to content

[Console] Hidden option using PHP 8 AsCommand attribute #41942

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
duboiss opened this issue Jul 2, 2021 · 0 comments
Closed

[Console] Hidden option using PHP 8 AsCommand attribute #41942

duboiss opened this issue Jul 2, 2021 · 0 comments

Comments

@duboiss
Copy link
Contributor

duboiss commented Jul 2, 2021

Symfony version(s) affected: 5.3.0 to current latest 5.3.3

Description
I always get a different behavior between hidden option in php 8 attribute (#[AsCommand]) and the setHidden() method in configure method even with the fix #41686. Also with cache:clear.

How to reproduce
I've created a project (see below) to simplify the tests, but :

  • Declare the DefaultCommand class in src/Command:
<?php

namespace App\Command;

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

class DefaultCommand extends Command
{
    protected static $defaultName = 'app:default';

    protected function configure(): void
    {
        $this
            ->setDescription('wrapper')
            ->setHidden(true)
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): ?int
    {
        /** @var Application $application */
        $application = $this->getApplication();

        $command = $application->find('list');
        $arguments = ['namespace' => 'app'];

        $listInput = new ArrayInput($arguments);

        return $command->run($listInput, $output);
    }
}
  • Declare a new Application.php class in src folder and use it in bin/console instead of the default Application class (just need to change the namespace):

src/Application.php

<?php

declare(strict_types=1);

namespace App;

use App\Command\DefaultCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application as SymfonyApplication;
use Symfony\Component\HttpKernel\KernelInterface;

class Application extends SymfonyApplication
{
    public function __construct(KernelInterface $kernel)
    {
        parent::__construct($kernel);

        if ($defaultName = DefaultCommand::getDefaultName()) {
            $this->setDefaultCommand($defaultName);
        }
    }
}

bin/console

#!/usr/bin/env php
<?php

use App\Kernel;
use App\Application;

if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) {
    throw new LogicException('Symfony Runtime is missing. Try running "composer require symfony/runtime".');
}

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

return function (array $context) {
    $kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);

    return new Application($kernel);
};
  • Make a random command, for exemple app:hello with or without PHP 8 attributes.
  • See the output of bin/console. Update DefaultCommand with PHP 8 #[AsCommand] attribute and observe the difference.

OR use the following project as exemple
https://github.com/duboiss/sfcommand

git clone https://github.com/duboiss/sfcommand
cd sfcommand
composer install
bin/console

git switch php8
bin/console

Additional context
Without attribute
image

With attribute
image

This is probably due to the fact that it has become lazy. Is there any way to keep the PHP 8 attribute and disable lazy (if that is the cause)?

@duboiss duboiss added the Bug label Jul 2, 2021
@fabpot fabpot closed this as completed Jul 8, 2021
fabpot added a commit that referenced this issue Jul 8, 2021
… PHP8 (a1812)

This PR was merged into the 5.3 branch.

Discussion
----------

[Console] Fix save correct name in setDefaultCommand() for PHP8

| Q             | A
| ------------- | ---
| Branch?       | 5.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #41942 | License       | MIT
| Doc PR        |

Fix save correct name in setDefaultCommand() for PHP8

Commits
-------

8a41694 fix setDefaultCommand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants