Skip to content

Commit 74cb81a

Browse files
adrienlucasweaverryan
authored andcommitted
Add --invokable option to make:controller
1 parent a1733f8 commit 74cb81a

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

src/Maker/MakeController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
6060
$command
6161
->addArgument('controller-class', InputArgument::OPTIONAL, sprintf('Choose a name for your controller class (e.g. <fg=yellow>%sController</>)', Str::asClassName(Str::getRandomTerm())))
6262
->addOption('no-template', null, InputOption::VALUE_NONE, 'Use this option to disable template generation')
63+
->addOption('invokable', 'i', InputOption::VALUE_NONE, 'Use this option to create an invokable controller')
6364
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeController.txt'))
6465
;
6566
}
@@ -73,21 +74,25 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
7374
);
7475

7576
$withTemplate = $this->isTwigInstalled() && !$input->getOption('no-template');
77+
$isInvokable = (bool) $input->getOption('invokable');
7678

7779
$useStatements = new UseStatementGenerator([
7880
AbstractController::class,
7981
$withTemplate ? Response::class : JsonResponse::class,
8082
Route::class,
8183
]);
8284

83-
$templateName = Str::asFilePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()).'/index.html.twig';
85+
$templateName = Str::asFilePath($controllerClassNameDetails->getRelativeNameWithoutSuffix())
86+
.($isInvokable ? '.html.twig' : '/index.html.twig');
87+
8488
$controllerPath = $generator->generateController(
8589
$controllerClassNameDetails->getFullName(),
8690
'controller/Controller.tpl.php',
8791
[
8892
'use_statements' => $useStatements,
8993
'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
9094
'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
95+
'method_name' => $isInvokable ? '__invoke' : 'index',
9196
'with_template' => $withTemplate,
9297
'template_name' => $templateName,
9398
]

src/Resources/skeleton/controller/Controller.tpl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class <?= $class_name; ?> extends AbstractController
88
{
99
<?= $generator->generateRouteForControllerMethod($route_path, $route_name); ?>
10-
public function index(): <?php if ($with_template) { ?>Response<?php } else { ?>JsonResponse<?php } ?>
10+
public function <?= $method_name ?>(): <?php if ($with_template) { ?>Response<?php } else { ?>JsonResponse<?php } ?>
1111

1212
{
1313
<?php if ($with_template) { ?>

tests/Maker/MakeControllerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,33 @@ public function getTestDetails(): \Generator
116116
$this->assertStringContainsString('templates/foo/bar/cool/index.html.twig', $output);
117117
}),
118118
];
119+
120+
yield 'it_generates_a_controller_with_invoke' => [$this->createMakerTest()
121+
->addExtraDependencies('twig')
122+
->run(function (MakerTestRunner $runner) {
123+
$output = $runner->runMaker([
124+
// controller class name
125+
'FooInvokable',
126+
], '--invokable');
127+
128+
$this->assertStringContainsString('src/Controller/FooInvokableController.php', $output);
129+
$this->assertStringContainsString('templates/foo_invokable.html.twig', $output);
130+
$this->runControllerTest($runner, 'it_generates_an_invokable_controller.php');
131+
}),
132+
];
133+
134+
yield 'it_generates_a_controller_with_invoke_in_sub_namespace' => [$this->createMakerTest()
135+
->addExtraDependencies('twig')
136+
->run(function (MakerTestRunner $runner) {
137+
$output = $runner->runMaker([
138+
// controller class name
139+
'Admin\\FooInvokable',
140+
], '--invokable');
141+
142+
$this->assertStringContainsString('src/Controller/Admin/FooInvokableController.php', $output);
143+
$this->assertStringContainsString('templates/admin/foo_invokable.html.twig', $output);
144+
}),
145+
];
119146
}
120147

121148
private function runControllerTest(MakerTestRunner $runner, string $filename): void
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Tests;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
7+
class GeneratedControllerTest extends WebTestCase
8+
{
9+
public function testControllerValidity()
10+
{
11+
$client = self::createClient();
12+
$client->request('GET', '/foo/invokable');
13+
14+
$this->assertEquals(200, $client->getResponse()->getStatusCode());
15+
}
16+
17+
public function testControllerInvokability()
18+
{
19+
$kernel = self::bootKernel();
20+
$controller = $kernel->getContainer()->get('App\Controller\FooInvokableController');
21+
$this->assertIsCallable($controller);
22+
23+
$response = $controller();
24+
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
25+
}
26+
}

0 commit comments

Comments
 (0)