Skip to content

Commit 4162f12

Browse files
committed
feature #64 Replace demo page by twig file (javiereguiluz, nicolas-grekas, weaverryan)
This PR was merged into the 1.0-dev branch. Discussion ---------- Replace demo page by twig file Follow up of #46 Seriously, this will make demoing Symfony a no brainer. Wow effect. Newbies <3 And for others, that's still >50% likelly OK. <img width="773" alt="screen shot 2018-02-08 at 10 58 59 pm" src="https://user-images.githubusercontent.com/121003/36011345-74da6026-0d24-11e8-96a5-55c818b27e56.png"> Commits ------- 42665c8 using a class 27634d6 phpcs tweaks 20c3704 Finishing Twig template with make:controller 995b534 Replace demo page by twig file 0cd9d72 Remove the demo page for new controllers
2 parents 039bb24 + 42665c8 commit 4162f12

File tree

12 files changed

+118
-83
lines changed

12 files changed

+118
-83
lines changed

.php_cs.dist

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ EOF
2525
->setFinder(
2626
PhpCsFixer\Finder::create()
2727
->in(__DIR__.'/src')
28+
// the PHP template files are a bit special
29+
->notName('*.tpl.php')
2830
)
29-
;
31+
;

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: 17 additions & 5 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,10 +31,12 @@
3031
final class MakeController extends AbstractMaker
3132
{
3233
private $router;
34+
private $fileManager;
3335

34-
public function __construct(RouterInterface $router)
36+
public function __construct(RouterInterface $router, FileManager $fileManager)
3537
{
3638
$this->router = $router;
39+
$this->fileManager = $fileManager;
3740
}
3841

3942
public static function getCommandName(): string
@@ -54,21 +57,30 @@ public function getParameters(InputInterface $input): array
5457
{
5558
$controllerClassName = Str::asClassName($input->getArgument('controller-class'), 'Controller');
5659
Validator::validateClassName($controllerClassName);
60+
$baseLayoutExists = $this->fileManager->fileExists('templates/base.html.twig');
5761

5862
return [
5963
'controller_class_name' => $controllerClassName,
64+
'controller_class_file' => 'src/Controller/'.$controllerClassName.'.php',
6065
'route_path' => Str::asRoutePath(str_replace('Controller', '', $controllerClassName)),
6166
'route_name' => Str::asRouteName(str_replace('Controller', '', $controllerClassName)),
67+
'twig_file' => Str::asFilePath(str_replace('Controller', '', $controllerClassName)).'.html.twig',
68+
'base_layout_exists' => $baseLayoutExists,
69+
'twig_installed' => $this->isTwigInstalled(),
6270
];
6371
}
6472

6573
public function getFiles(array $params): array
6674
{
67-
$skeletonFile = $this->isTwigInstalled() ? 'ControllerWithTwig.tpl.php' : 'Controller.tpl.php';
75+
$dir = __DIR__.'/../Resources/skeleton/controller';
6876

69-
return [
70-
__DIR__.'/../Resources/skeleton/controller/'.$skeletonFile => 'src/Controller/'.$params['controller_class_name'].'.php',
71-
];
77+
$paths = [$dir.'/Controller.tpl.php' => $params['controller_class_file']];
78+
79+
if ($params['twig_installed']) {
80+
$paths[$dir.'/twig_template.tpl.php'] = 'templates/'.$params['twig_file'];
81+
}
82+
83+
return $paths;
7284
}
7385

7486
public function writeSuccessMessage(array $params, ConsoleStyle $io)

src/Resources/config/makers.xml

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

1818
<service id="maker.maker.make_controller" class="Symfony\Bundle\MakerBundle\Maker\MakeController">
1919
<argument type="service" id="router" />
20+
<argument type="service" id="maker.file_manager" />
2021
<tag name="maker.command" />
2122
</service>
2223

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

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

98
class <?= $controller_class_name ?> extends Controller
109
{
@@ -13,6 +12,15 @@ class <?= $controller_class_name ?> extends Controller
1312
*/
1413
public function index()
1514
{
16-
return new Response('Welcome to your new controller!');
15+
<?php if ($twig_installed) { ?>
16+
return $this->render('<?= $twig_file ?>', [
17+
'controller_name' => '<?= $controller_class_name ?>',
18+
]);
19+
<?php } else { ?>
20+
return $this->json([
21+
'message' => 'Welcome to your new controller!',
22+
'path' => '<?= $relative_path; ?>',
23+
]);
24+
<?php } ?>
1725
}
1826
}

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

Lines changed: 0 additions & 19 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 class="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 %}

src/Resources/views/demoPage.html.twig

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/Str.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Bundle\MakerBundle;
1313

14+
use Symfony\Component\DependencyInjection\Container;
15+
1416
/**
1517
* @author Javier Eguiluz <[email protected]>
1618
* @author Ryan Weaver <[email protected]>
@@ -97,6 +99,14 @@ public static function asEventMethod(string $eventName): string
9799
return sprintf('on%s', self::asClassName($eventName));
98100
}
99101

102+
public static function asFilePath(string $value): string
103+
{
104+
$value = Container::underscore(trim($value));
105+
$value = str_replace('\\', '/', $value);
106+
107+
return $value;
108+
}
109+
100110
public static function getRandomTerm(): string
101111
{
102112
$adjectives = [

0 commit comments

Comments
 (0)