Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Maker/MakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
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;
use Symfony\Component\Console\Input\InputArgument;
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;

/**
Expand All @@ -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';
Expand Down Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/makers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</service>

<service id="maker.maker.make_controller" class="Symfony\Bundle\MakerBundle\Maker\MakeController">
<argument type="service" id="maker.php_compat_util" />
<tag name="maker.command" />
</service>

Expand Down
33 changes: 24 additions & 9 deletions tests/Maker/MakeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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([
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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
Expand All @@ -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([
Expand All @@ -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([
Expand All @@ -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,
Expand Down