Skip to content

Commit eb8da45

Browse files
javiereguiluzfabpot
authored andcommitted
Generate a different controller when Twig is not installed
1 parent cb5a8de commit eb8da45

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/Command/MakeControllerCommand.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111

1212
namespace Symfony\Bundle\MakerBundle\Command;
1313

14+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
1415
use Symfony\Bundle\MakerBundle\ConsoleStyle;
1516
use Symfony\Bundle\MakerBundle\DependencyBuilder;
1617
use Symfony\Bundle\MakerBundle\Generator;
1718
use Symfony\Bundle\MakerBundle\Str;
1819
use Symfony\Bundle\MakerBundle\Validator;
20+
use Symfony\Bundle\TwigBundle\TwigBundle;
1921
use Symfony\Component\Console\Input\InputArgument;
2022
use Symfony\Component\Console\Input\InputInterface;
2123
use Symfony\Component\Console\Output\OutputInterface;
2224
use Symfony\Component\Routing\RouterInterface;
23-
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
2425

2526
/**
2627
* @author Javier Eguiluz <[email protected]>
@@ -62,8 +63,10 @@ protected function getParameters(): array
6263

6364
protected function getFiles(array $params): array
6465
{
66+
$skeletonFile = $this->isTwigInstalled() ? 'ControllerWithTwig.php.txt' : 'Controller.php.txt';
67+
6568
return [
66-
__DIR__.'/../Resources/skeleton/controller/Controller.php.txt' => 'src/Controller/'.$params['controller_class_name'].'.php'
69+
__DIR__.'/../Resources/skeleton/controller/'.$skeletonFile => 'src/Controller/'.$params['controller_class_name'].'.php'
6770
];
6871
}
6972

@@ -89,4 +92,9 @@ protected function configureDependencies(DependencyBuilder $dependencies)
8992
'annotations'
9093
);
9194
}
95+
96+
private function isTwigInstalled()
97+
{
98+
return class_exists(TwigBundle::class);
99+
}
92100
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class {{ controller_class_name }} extends Controller
1313
*/
1414
public function index()
1515
{
16-
// replace this line with your own code!
17-
return $this->render('@Maker/demoPage.html.twig', [ 'path' => str_replace($this->getParameter('kernel.project_dir').'/', '', __FILE__) ]);
16+
return new Response('Welcome to your new controller!');
1817
}
1918
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7+
use Symfony\Component\HttpFoundation\Response;
8+
9+
class {{ controller_class_name }} extends Controller
10+
{
11+
/**
12+
* @Route("{{ route_path }}", name="{{ route_name }}")
13+
*/
14+
public function index()
15+
{
16+
// replace this line with your own code!
17+
return $this->render('@Maker/demoPage.html.twig', [ 'path' => str_replace($this->getParameter('kernel.project_dir').'/', '', __FILE__) ]);
18+
}
19+
}

0 commit comments

Comments
 (0)