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
7 changes: 6 additions & 1 deletion src/Maker/MakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
$command
->addArgument('controller-class', InputArgument::OPTIONAL, sprintf('Choose a name for your controller class (e.g. <fg=yellow>%sController</>)', Str::asClassName(Str::getRandomTerm())))
->addOption('no-template', null, InputOption::VALUE_NONE, 'Use this option to disable template generation')
->addOption('invokable', 'i', InputOption::VALUE_NONE, 'Use this option to create an invokable controller')
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeController.txt'))
;
}
Expand All @@ -73,21 +74,25 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
);

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

$useStatements = new UseStatementGenerator([
AbstractController::class,
$withTemplate ? Response::class : JsonResponse::class,
Route::class,
]);

$templateName = Str::asFilePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()).'/index.html.twig';
$templateName = Str::asFilePath($controllerClassNameDetails->getRelativeNameWithoutSuffix())
.($isInvokable ? '.html.twig' : '/index.html.twig');

$controllerPath = $generator->generateController(
$controllerClassNameDetails->getFullName(),
'controller/Controller.tpl.php',
[
'use_statements' => $useStatements,
'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'method_name' => $isInvokable ? '__invoke' : 'index',
'with_template' => $withTemplate,
'template_name' => $templateName,
]
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/skeleton/controller/Controller.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class <?= $class_name; ?> extends AbstractController
{
<?= $generator->generateRouteForControllerMethod($route_path, $route_name); ?>
public function index(): <?php if ($with_template) { ?>Response<?php } else { ?>JsonResponse<?php } ?>
public function <?= $method_name ?>(): <?php if ($with_template) { ?>Response<?php } else { ?>JsonResponse<?php } ?>

{
<?php if ($with_template) { ?>
Expand Down
27 changes: 27 additions & 0 deletions tests/Maker/MakeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,33 @@ public function getTestDetails(): \Generator
$this->assertStringContainsString('templates/foo/bar/cool/index.html.twig', $output);
}),
];

yield 'it_generates_a_controller_with_invoke' => [$this->createMakerTest()
->addExtraDependencies('twig')
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker([
// controller class name
'FooInvokable',
], '--invokable');

$this->assertStringContainsString('src/Controller/FooInvokableController.php', $output);
$this->assertStringContainsString('templates/foo_invokable.html.twig', $output);
$this->runControllerTest($runner, 'it_generates_an_invokable_controller.php');
}),
];

yield 'it_generates_a_controller_with_invoke_in_sub_namespace' => [$this->createMakerTest()
->addExtraDependencies('twig')
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker([
// controller class name
'Admin\\FooInvokable',
], '--invokable');

$this->assertStringContainsString('src/Controller/Admin/FooInvokableController.php', $output);
$this->assertStringContainsString('templates/admin/foo_invokable.html.twig', $output);
}),
];
}

private function runControllerTest(MakerTestRunner $runner, string $filename): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Tests;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class GeneratedControllerTest extends WebTestCase
{
public function testControllerValidity()
{
$client = self::createClient();
$client->request('GET', '/foo/invokable');

$this->assertEquals(200, $client->getResponse()->getStatusCode());
}

public function testControllerInvokability()
{
$kernel = self::bootKernel();
$controller = $kernel->getContainer()->get('App\Controller\FooInvokableController');
$this->assertIsCallable($controller);

$response = $controller();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
}
}