Skip to content

Commit 4028f85

Browse files
Jibbarthjrushlow
andauthored
[make:controller] avoid require doctrine/annotation when can use attributes (#858)
Co-authored-by: Jesse Rushlow <[email protected]>
1 parent 6036523 commit 4028f85

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

src/Maker/MakeController.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
use Symfony\Bundle\MakerBundle\Generator;
1919
use Symfony\Bundle\MakerBundle\InputConfiguration;
2020
use Symfony\Bundle\MakerBundle\Str;
21+
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;
2122
use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator;
2223
use Symfony\Bundle\TwigBundle\TwigBundle;
2324
use Symfony\Component\Console\Command\Command;
2425
use Symfony\Component\Console\Input\InputArgument;
2526
use Symfony\Component\Console\Input\InputInterface;
2627
use Symfony\Component\Console\Input\InputOption;
2728
use Symfony\Component\HttpFoundation\Response;
29+
use Symfony\Component\HttpKernel\Kernel;
2830
use Symfony\Component\Routing\Annotation\Route;
2931

3032
/**
@@ -33,6 +35,17 @@
3335
*/
3436
final class MakeController extends AbstractMaker
3537
{
38+
private $phpCompatUtil;
39+
40+
public function __construct(PhpCompatUtil $phpCompatUtil = null)
41+
{
42+
if (null === $phpCompatUtil) {
43+
@trigger_error(sprintf('Passing a "%s" instance is mandatory since version 1.42.0', PhpCompatUtil::class), \E_USER_DEPRECATED);
44+
}
45+
46+
$this->phpCompatUtil = $phpCompatUtil;
47+
}
48+
3649
public static function getCommandName(): string
3750
{
3851
return 'make:controller';
@@ -100,6 +113,11 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
100113

101114
public function configureDependencies(DependencyBuilder $dependencies): void
102115
{
116+
// @legacy - Remove method when support for Symfony 5.4 is dropped
117+
if (null !== $this->phpCompatUtil && 60000 <= Kernel::VERSION_ID && $this->phpCompatUtil->canUseAttributes()) {
118+
return;
119+
}
120+
103121
$dependencies->addClassDependency(
104122
Annotation::class,
105123
'doctrine/annotations'

src/Resources/config/makers.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
</service>
2323

2424
<service id="maker.maker.make_controller" class="Symfony\Bundle\MakerBundle\Maker\MakeController">
25+
<argument type="service" id="maker.php_compat_util" />
2526
<tag name="maker.command" />
2627
</service>
2728

tests/Maker/MakeControllerTest.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bundle\MakerBundle\Maker\MakeController;
1515
use Symfony\Bundle\MakerBundle\Test\MakerTestCase;
16+
use Symfony\Bundle\MakerBundle\Test\MakerTestDetails;
1617
use Symfony\Bundle\MakerBundle\Test\MakerTestRunner;
1718

1819
class MakeControllerTest extends MakerTestCase
@@ -22,9 +23,23 @@ protected function getMakerClass(): string
2223
return MakeController::class;
2324
}
2425

25-
public function getTestDetails()
26+
// @legacy Remove when Symfony 5.4 is no longer supported
27+
private function getControllerTest(): MakerTestDetails
2628
{
27-
yield 'it_generates_a_controller' => [$this->createMakerTest()
29+
return $this
30+
->createMakerTest()
31+
->preRun(function (MakerTestRunner $runner) {
32+
if ($runner->getSymfonyVersion() < 60000) {
33+
// Because MakeController::configureDependencies() is executed in the main thread,
34+
// we need to manually add in `doctrine/annotations` for Symfony 5.4 tests.
35+
$runner->runProcess('composer require doctrine/annotations');
36+
}
37+
});
38+
}
39+
40+
public function getTestDetails(): \Generator
41+
{
42+
yield 'it_generates_a_controller' => [$this->getControllerTest()
2843
->run(function (MakerTestRunner $runner) {
2944
$output = $runner->runMaker([
3045
// controller class name
@@ -37,7 +52,7 @@ public function getTestDetails()
3752
}),
3853
];
3954

40-
yield 'it_generates_a_controller_with_twig' => [$this->createMakerTest()
55+
yield 'it_generates_a_controller_with_twig' => [$this->getControllerTest()
4156
->addExtraDependencies('twig')
4257
->run(function (MakerTestRunner $runner) {
4358
$output = $runner->runMaker([
@@ -49,7 +64,7 @@ public function getTestDetails()
4964
}),
5065
];
5166

52-
yield 'it_generates_a_controller_with_twig_no_base_template' => [$this->createMakerTest()
67+
yield 'it_generates_a_controller_with_twig_no_base_template' => [$this->getControllerTest()
5368
->addExtraDependencies('twig')
5469
->run(function (MakerTestRunner $runner) {
5570
$runner->deleteFile('templates/base.html.twig');
@@ -63,7 +78,7 @@ public function getTestDetails()
6378
}),
6479
];
6580

66-
yield 'it_generates_a_controller_with_without_template' => [$this->createMakerTest()
81+
yield 'it_generates_a_controller_with_without_template' => [$this->getControllerTest()
6782
->addExtraDependencies('twig')
6883
->run(function (MakerTestRunner $runner) {
6984
$runner->deleteFile('templates/base.html.twig');
@@ -80,7 +95,7 @@ public function getTestDetails()
8095
}),
8196
];
8297

83-
yield 'it_generates_a_controller_in_sub_namespace' => [$this->createMakerTest()
98+
yield 'it_generates_a_controller_in_sub_namespace' => [$this->getControllerTest()
8499
->run(function (MakerTestRunner $runner) {
85100
$output = $runner->runMaker([
86101
// controller class name
@@ -92,7 +107,7 @@ public function getTestDetails()
92107
}),
93108
];
94109

95-
yield 'it_generates_a_controller_in_sub_namespace_with_template' => [$this->createMakerTest()
110+
yield 'it_generates_a_controller_in_sub_namespace_with_template' => [$this->getControllerTest()
96111
->addExtraDependencies('twig')
97112
->run(function (MakerTestRunner $runner) {
98113
$output = $runner->runMaker([
@@ -104,7 +119,7 @@ public function getTestDetails()
104119
}),
105120
];
106121

107-
yield 'it_generates_a_controller_with_full_custom_namespace' => [$this->createMakerTest()
122+
yield 'it_generates_a_controller_with_full_custom_namespace' => [$this->getControllerTest()
108123
->addExtraDependencies('twig')
109124
->run(function (MakerTestRunner $runner) {
110125
$output = $runner->runMaker([
@@ -118,7 +133,7 @@ public function getTestDetails()
118133
];
119134
}
120135

121-
private function runControllerTest(MakerTestRunner $runner, string $filename)
136+
private function runControllerTest(MakerTestRunner $runner, string $filename): void
122137
{
123138
$runner->copy(
124139
'make-controller/tests/'.$filename,

0 commit comments

Comments
 (0)