Skip to content

Commit 995b534

Browse files
nicolas-grekasweaverryan
authored andcommitted
Replace demo page by twig file
1 parent 0cd9d72 commit 995b534

File tree

6 files changed

+45
-24
lines changed

6 files changed

+45
-24
lines changed

src/Maker/MakeController.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
final class MakeController extends AbstractMaker
3131
{
3232
private $router;
33+
private $projectDir;
3334

34-
public function __construct(RouterInterface $router)
35+
public function __construct(RouterInterface $router, string $projectDir)
3536
{
3637
$this->router = $router;
38+
$this->projectDir = $projectDir;
3739
}
3840

3941
public static function getCommandName(): string
@@ -55,20 +57,34 @@ public function getParameters(InputInterface $input): array
5557
$controllerClassName = Str::asClassName($input->getArgument('controller-class'), 'Controller');
5658
Validator::validateClassName($controllerClassName);
5759

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+
}
65+
5866
return [
5967
'controller_class_name' => $controllerClassName,
68+
'controller_class_file' => 'src/Controller/'.$controllerClassName.'.php',
6069
'route_path' => Str::asRoutePath(str_replace('Controller', '', $controllerClassName)),
6170
'route_name' => Str::asRouteName(str_replace('Controller', '', $controllerClassName)),
71+
'twig_file' => Str::asFilePath(str_replace('Controller', '', $controllerClassName)).'.html.twig',
72+
'twig_first_line' => $twigFirstLine,
73+
'twig_installed' => $this->isTwigInstalled(),
6274
];
6375
}
6476

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

69-
return [
70-
__DIR__.'/../Resources/skeleton/controller/'.$skeletonFile => 'src/Controller/'.$params['controller_class_name'].'.php',
71-
];
81+
$paths = [$dir.'Controller.tpl.php' => $params['controller_class_file']];
82+
83+
if ($params['twig_installed']) {
84+
$paths[$dir.'Controller.twig.php'] = 'templates/'.$params['twig_file'];
85+
}
86+
87+
return $paths;
7288
}
7389

7490
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>%kernel.project_dir%</argument>
2021
<tag name="maker.command" />
2122
</service>
2223

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
77
use Symfony\Component\HttpFoundation\Response;
88

9-
class <?= $controller_class_name ?> extends Controller
9+
class <?= $controller_class_name ?> extends AbstractController
1010
{
1111
/**
1212
* @Route("<?= $route_path ?>", name="<?= $route_name ?>")
1313
*/
1414
public function index()
1515
{
16+
<?php if ($twig_installed) { ?>
17+
return $this->render('<?= $twig_file ?>', [
18+
'controller_name' => '<?= $controller_name ?>',
19+
]);
20+
<?php } else { ?>
1621
return new Response('Welcome to your new controller!');
22+
<?php } ?>
1723
}
1824
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?= $twig_first_line ?>
2+
3+
{% block body %}
4+
<h1>Hello {{ controller_name }}!</h1>
5+
<p>From <code>templates/<?= $twig_file ?></code></p>
6+
{% endblock %}

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

Lines changed: 0 additions & 18 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)