diff --git a/src/Maker/MakeController.php b/src/Maker/MakeController.php index 8e5f74055..623e3fff4 100644 --- a/src/Maker/MakeController.php +++ b/src/Maker/MakeController.php @@ -18,6 +18,7 @@ use Symfony\Bundle\MakerBundle\Generator; use Symfony\Bundle\MakerBundle\InputConfiguration; use Symfony\Bundle\MakerBundle\Str; +use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil; use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator; use Symfony\Bundle\TwigBundle\TwigBundle; use Symfony\Component\Console\Command\Command; @@ -25,6 +26,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Routing\Annotation\Route; /** @@ -33,6 +35,17 @@ */ final class MakeController extends AbstractMaker { + private $phpCompatUtil; + + public function __construct(PhpCompatUtil $phpCompatUtil = null) + { + if (null === $phpCompatUtil) { + @trigger_error(sprintf('Passing a "%s" instance is mandatory since version 1.42.0', PhpCompatUtil::class), \E_USER_DEPRECATED); + } + + $this->phpCompatUtil = $phpCompatUtil; + } + public static function getCommandName(): string { return 'make:controller'; @@ -100,6 +113,11 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen public function configureDependencies(DependencyBuilder $dependencies): void { + // @legacy - Remove method when support for Symfony 5.4 is dropped + if (null !== $this->phpCompatUtil && 60000 <= Kernel::VERSION_ID && $this->phpCompatUtil->canUseAttributes()) { + return; + } + $dependencies->addClassDependency( Annotation::class, 'doctrine/annotations' diff --git a/src/Resources/config/makers.xml b/src/Resources/config/makers.xml index 626c317a0..e47c880eb 100644 --- a/src/Resources/config/makers.xml +++ b/src/Resources/config/makers.xml @@ -22,6 +22,7 @@ + diff --git a/tests/Maker/MakeControllerTest.php b/tests/Maker/MakeControllerTest.php index 49cd40da4..984131bce 100644 --- a/tests/Maker/MakeControllerTest.php +++ b/tests/Maker/MakeControllerTest.php @@ -13,6 +13,7 @@ use Symfony\Bundle\MakerBundle\Maker\MakeController; use Symfony\Bundle\MakerBundle\Test\MakerTestCase; +use Symfony\Bundle\MakerBundle\Test\MakerTestDetails; use Symfony\Bundle\MakerBundle\Test\MakerTestRunner; class MakeControllerTest extends MakerTestCase @@ -22,9 +23,23 @@ protected function getMakerClass(): string return MakeController::class; } - public function getTestDetails() + // @legacy Remove when Symfony 5.4 is no longer supported + private function getControllerTest(): MakerTestDetails { - yield 'it_generates_a_controller' => [$this->createMakerTest() + return $this + ->createMakerTest() + ->preRun(function (MakerTestRunner $runner) { + if ($runner->getSymfonyVersion() < 60000) { + // Because MakeController::configureDependencies() is executed in the main thread, + // we need to manually add in `doctrine/annotations` for Symfony 5.4 tests. + $runner->runProcess('composer require doctrine/annotations'); + } + }); + } + + public function getTestDetails(): \Generator + { + yield 'it_generates_a_controller' => [$this->getControllerTest() ->run(function (MakerTestRunner $runner) { $output = $runner->runMaker([ // controller class name @@ -37,7 +52,7 @@ public function getTestDetails() }), ]; - yield 'it_generates_a_controller_with_twig' => [$this->createMakerTest() + yield 'it_generates_a_controller_with_twig' => [$this->getControllerTest() ->addExtraDependencies('twig') ->run(function (MakerTestRunner $runner) { $output = $runner->runMaker([ @@ -49,7 +64,7 @@ public function getTestDetails() }), ]; - yield 'it_generates_a_controller_with_twig_no_base_template' => [$this->createMakerTest() + yield 'it_generates_a_controller_with_twig_no_base_template' => [$this->getControllerTest() ->addExtraDependencies('twig') ->run(function (MakerTestRunner $runner) { $runner->deleteFile('templates/base.html.twig'); @@ -63,7 +78,7 @@ public function getTestDetails() }), ]; - yield 'it_generates_a_controller_with_without_template' => [$this->createMakerTest() + yield 'it_generates_a_controller_with_without_template' => [$this->getControllerTest() ->addExtraDependencies('twig') ->run(function (MakerTestRunner $runner) { $runner->deleteFile('templates/base.html.twig'); @@ -80,7 +95,7 @@ public function getTestDetails() }), ]; - yield 'it_generates_a_controller_in_sub_namespace' => [$this->createMakerTest() + yield 'it_generates_a_controller_in_sub_namespace' => [$this->getControllerTest() ->run(function (MakerTestRunner $runner) { $output = $runner->runMaker([ // controller class name @@ -92,7 +107,7 @@ public function getTestDetails() }), ]; - yield 'it_generates_a_controller_in_sub_namespace_with_template' => [$this->createMakerTest() + yield 'it_generates_a_controller_in_sub_namespace_with_template' => [$this->getControllerTest() ->addExtraDependencies('twig') ->run(function (MakerTestRunner $runner) { $output = $runner->runMaker([ @@ -104,7 +119,7 @@ public function getTestDetails() }), ]; - yield 'it_generates_a_controller_with_full_custom_namespace' => [$this->createMakerTest() + yield 'it_generates_a_controller_with_full_custom_namespace' => [$this->getControllerTest() ->addExtraDependencies('twig') ->run(function (MakerTestRunner $runner) { $output = $runner->runMaker([ @@ -118,7 +133,7 @@ public function getTestDetails() ]; } - private function runControllerTest(MakerTestRunner $runner, string $filename) + private function runControllerTest(MakerTestRunner $runner, string $filename): void { $runner->copy( 'make-controller/tests/'.$filename,