Skip to content

Commit 7d95b8d

Browse files
committed
feature #280 Add --no-template option for make:controller command (welcoMattic)
This PR was squashed before being merged into the 1.0-dev branch (closes #280). Discussion ---------- Add --no-template option for make:controller command I had several times the case in which I did not necessarily need a template when I generated a controller. So I'm trying to add an option `--no-template` to force avoid template generation. I think that I will need help on tests writing ;) Commits ------- 99680bc Fix tests (passed on local env) d6ec857 Fix internal project tests 052734c Try to fix tests 860d340 WIP Add --no-template option for make:controller command
2 parents b5de434 + 99680bc commit 7d95b8d

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

src/Maker/MakeController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Console\Command\Command;
2222
use Symfony\Component\Console\Input\InputArgument;
2323
use Symfony\Component\Console\Input\InputInterface;
24+
use Symfony\Component\Console\Input\InputOption;
2425

2526
/**
2627
* @author Javier Eguiluz <[email protected]>
@@ -38,6 +39,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
3839
$command
3940
->setDescription('Creates a new controller class')
4041
->addArgument('controller-class', InputArgument::OPTIONAL, sprintf('Choose a name for your controller class (e.g. <fg=yellow>%sController</>)', Str::asClassName(Str::getRandomTerm())))
42+
->addOption('no-template', null, InputOption::VALUE_NONE, 'Use this option to disable template generation')
4143
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeController.txt'))
4244
;
4345
}
@@ -50,19 +52,20 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
5052
'Controller'
5153
);
5254

55+
$noTemplate = $input->getOption('no-template');
5356
$templateName = Str::asFilePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()).'/index.html.twig';
5457
$controllerPath = $generator->generateController(
5558
$controllerClassNameDetails->getFullName(),
5659
'controller/Controller.tpl.php',
5760
[
5861
'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
5962
'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
60-
'twig_installed' => $this->isTwigInstalled(),
63+
'with_template' => $this->isTwigInstalled() && !$noTemplate,
6164
'template_name' => $templateName,
6265
]
6366
);
6467

65-
if ($this->isTwigInstalled()) {
68+
if ($this->isTwigInstalled() && !$noTemplate) {
6669
$generator->generateFile(
6770
'templates/'.$templateName,
6871
'controller/twig_template.tpl.php',

src/Resources/help/MakeController.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ The <info>%command.name%</info> command generates a new controller class.
33
<info>php %command.full_name% CoolStuffController</info>
44

55
If the argument is missing, the command will ask for the controller class name interactively.
6+
7+
You can also generate the controller alone, without template with this option:
8+
9+
<info>php %command.full_name% --no-template</info>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class <?= $class_name; ?> extends <?= $parent_class_name; ?><?= "\n" ?>
1212
*/
1313
public function index()
1414
{
15-
<?php if ($twig_installed) { ?>
15+
<?php if ($with_template) { ?>
1616
return $this->render('<?= $template_name ?>', [
1717
'controller_name' => '<?= $class_name ?>',
1818
]);

tests/Maker/FunctionalTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,22 @@ public function getCommandTests()
121121
->deleteFile('templates/base.html.twig')
122122
];
123123

124+
yield 'controller_without_template' => [MakerTestDetails::createTest(
125+
$this->getMakerInstance(MakeController::class),
126+
[
127+
// controller class name
128+
'FooNoTemplate',
129+
])
130+
->setArgumentsString('--no-template')
131+
->addExtraDependencies('twig')
132+
->assert(function (string $output, string $directory) {
133+
// make sure the template was not configured
134+
$this->assertContainsCount('created: ', $output, 1);
135+
$this->assertContains('created: src/Controller/FooNoTemplateController.php', $output);
136+
$this->assertNotContains('created: templates/foo_no_template/index.html.twig', $output);
137+
})
138+
];
139+
124140
yield 'controller_sub_namespace' => [MakerTestDetails::createTest(
125141
$this->getMakerInstance(MakeController::class),
126142
[

0 commit comments

Comments
 (0)