Skip to content

Commit 20c3704

Browse files
committed
Finishing Twig template with make:controller
* Tweaks to how the template looks * Some internal technical changes and fixes
1 parent 995b534 commit 20c3704

File tree

9 files changed

+93
-32
lines changed

9 files changed

+93
-32
lines changed

src/FileManager.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ public function fileExists($path): bool
5858
return file_exists($this->absolutizePath($path));
5959
}
6060

61+
public function relativizePath($absolutePath): string
62+
{
63+
$relativePath = str_replace($this->rootDirectory, '.', $absolutePath);
64+
65+
return is_dir($absolutePath) ? rtrim($relativePath, '/').'/' : $relativePath;
66+
}
67+
6168
private function absolutizePath($path): string
6269
{
6370
if (0 === strpos($path, '/')) {
@@ -66,11 +73,4 @@ private function absolutizePath($path): string
6673

6774
return sprintf('%s/%s', $this->rootDirectory, $path);
6875
}
69-
70-
private function relativizePath($absolutePath): string
71-
{
72-
$relativePath = str_replace($this->rootDirectory, '.', $absolutePath);
73-
74-
return is_dir($absolutePath) ? rtrim($relativePath, '/').'/' : $relativePath;
75-
}
7676
}

src/Generator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public function generate(array $parameters, array $files)
4646
}
4747

4848
foreach ($files as $fileTemplatePath => $targetPath) {
49-
$fileContents = $this->fileManager->parseTemplate($fileTemplatePath, $parameters);
49+
$templateParameters = array_merge($parameters, [
50+
'relative_path' => $this->fileManager->relativizePath($targetPath)
51+
]);
52+
53+
$fileContents = $this->fileManager->parseTemplate($fileTemplatePath, $templateParameters);
5054
$this->fileManager->dumpFile($targetPath, $fileContents);
5155
}
5256
}

src/Maker/MakeController.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\Common\Annotations\Annotation;
1515
use Symfony\Bundle\MakerBundle\ConsoleStyle;
1616
use Symfony\Bundle\MakerBundle\DependencyBuilder;
17+
use Symfony\Bundle\MakerBundle\FileManager;
1718
use Symfony\Bundle\MakerBundle\InputConfiguration;
1819
use Symfony\Bundle\MakerBundle\Str;
1920
use Symfony\Bundle\MakerBundle\Validator;
@@ -30,12 +31,12 @@
3031
final class MakeController extends AbstractMaker
3132
{
3233
private $router;
33-
private $projectDir;
34+
private $fileManager;
3435

35-
public function __construct(RouterInterface $router, string $projectDir)
36+
public function __construct(RouterInterface $router, FileManager $fileManager)
3637
{
3738
$this->router = $router;
38-
$this->projectDir = $projectDir;
39+
$this->fileManager = $fileManager;
3940
}
4041

4142
public static function getCommandName(): string
@@ -56,32 +57,27 @@ public function getParameters(InputInterface $input): array
5657
{
5758
$controllerClassName = Str::asClassName($input->getArgument('controller-class'), 'Controller');
5859
Validator::validateClassName($controllerClassName);
59-
60-
if (file_exists($this->projectDir.'/templates/base.html.twig')) {
61-
$twigFirstLine = "{% extends 'base.html.twig' %}\n\n{% block title %}Hello {{ controller_name }}!{% endblock %}\n";
62-
} else {
63-
$twigFirstLine = "<!DOCTYPE html>\n\n<title>Hello {{ controller_name }}!</title>\n";
64-
}
60+
$baseLayoutExists = $this->fileManager->fileExists('templates/base.html.twig');
6561

6662
return [
6763
'controller_class_name' => $controllerClassName,
6864
'controller_class_file' => 'src/Controller/'.$controllerClassName.'.php',
6965
'route_path' => Str::asRoutePath(str_replace('Controller', '', $controllerClassName)),
7066
'route_name' => Str::asRouteName(str_replace('Controller', '', $controllerClassName)),
7167
'twig_file' => Str::asFilePath(str_replace('Controller', '', $controllerClassName)).'.html.twig',
72-
'twig_first_line' => $twigFirstLine,
68+
'base_layout_exists' => $baseLayoutExists,
7369
'twig_installed' => $this->isTwigInstalled(),
7470
];
7571
}
7672

7773
public function getFiles(array $params): array
7874
{
79-
$dir = __DIR__.'/../Resources/skeleton/controller/';
75+
$dir = __DIR__.'/../Resources/skeleton/controller';
8076

81-
$paths = [$dir.'Controller.tpl.php' => $params['controller_class_file']];
77+
$paths = [$dir.'/Controller.tpl.php' => $params['controller_class_file']];
8278

8379
if ($params['twig_installed']) {
84-
$paths[$dir.'Controller.twig.php'] = 'templates/'.$params['twig_file'];
80+
$paths[$dir.'/twig_template.tpl.php'] = 'templates/'.$params['twig_file'];
8581
}
8682

8783
return $paths;

src/Resources/config/makers.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<service id="maker.maker.make_controller" class="Symfony\Bundle\MakerBundle\Maker\MakeController">
1919
<argument type="service" id="router" />
20-
<argument>%kernel.project_dir%</argument>
20+
<argument type="service" id="maker.file_manager" />
2121
<tag name="maker.command" />
2222
</service>
2323

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
use Symfony\Component\Routing\Annotation\Route;
66
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7-
use Symfony\Component\HttpFoundation\Response;
87

9-
class <?= $controller_class_name ?> extends AbstractController
8+
class <?= $controller_class_name ?> extends Controller
109
{
1110
/**
1211
* @Route("<?= $route_path ?>", name="<?= $route_name ?>")
@@ -15,10 +14,13 @@ public function index()
1514
{
1615
<?php if ($twig_installed) { ?>
1716
return $this->render('<?= $twig_file ?>', [
18-
'controller_name' => '<?= $controller_name ?>',
17+
'controller_name' => '<?= $controller_class_name ?>',
1918
]);
2019
<?php } else { ?>
21-
return new Response('Welcome to your new controller!');
20+
return $this->json([
21+
'message' => 'Welcome to your new controller!',
22+
'path' => '<?= $relative_path; ?>',
23+
]);
2224
<?php } ?>
2325
}
2426
}

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

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php if ($base_layout_exists): ?>
2+
{% extends 'base.html.twig' %}
3+
4+
{% block title %}Hello {{ controller_name }}!{% endblock %}
5+
<?php else: ?>
6+
<!DOCTYPE html>
7+
8+
<title>Hello {{ controller_name }}!</title>
9+
<?php endif; ?>
10+
11+
{% block body %}
12+
<style>
13+
#example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
14+
#example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
15+
</style>
16+
17+
<div id="example-wrapper">
18+
<h1>Hello {{ controller_name }}! ✅</h1>
19+
20+
This friendly message is coming from:
21+
<ul>
22+
<li>Your controller at <code><?= $controller_class_file; ?></code></li>
23+
<li>Your template at <code><?= $relative_path ?></code></li>
24+
</ul>
25+
</div>
26+
{% endblock %}

tests/Maker/FunctionalTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@ public function getCommandTests()
6868
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeController')
6969
];
7070

71+
yield 'controller_with_template' => [MakerTestDetails::createTest(
72+
$this->getMakerInstance(MakeController::class),
73+
[
74+
// controller class name
75+
'FooTwig',
76+
])
77+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeControllerTwig')
78+
->addExtraDependencies('twig')
79+
];
80+
81+
yield 'controller_with_template_no_base' => [MakerTestDetails::createTest(
82+
$this->getMakerInstance(MakeController::class),
83+
[
84+
// controller class name
85+
'FooTwig',
86+
])
87+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeControllerTwig')
88+
->addExtraDependencies('twig')
89+
->addPreMakeCommand('rm templates/base.html.twig')
90+
];
91+
7192
yield 'entity' => [MakerTestDetails::createTest(
7293
$this->getMakerInstance(MakeEntity::class),
7394
[
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Tests;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
7+
class GeneratedControllerWithTwigTest extends WebTestCase
8+
{
9+
public function testController()
10+
{
11+
$client = self::createClient();
12+
$client->request('GET', '/foo/twig');
13+
14+
$this->assertEquals(200, $client->getResponse()->getStatusCode());
15+
$this->assertContains('<!DOCTYPE html>', $client->getResponse()->getContent());
16+
$this->assertContains('Hello FooTwigController', $client->getResponse()->getContent());
17+
}
18+
}

0 commit comments

Comments
 (0)