Skip to content

Generate a different controller when Twig is not installed #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Command/MakeControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ protected function getParameters(): array

protected function getFiles(array $params): array
{
$skeletonFile = $this->isTwigInstalled() ? 'ControllerWithTwig.php.txt' : 'Controller.php.txt';

return [
__DIR__.'/../Resources/skeleton/controller/Controller.php.txt' => 'src/Controller/'.$params['controller_class_name'].'.php'
__DIR__.'/../Resources/skeleton/controller/'.$skeletonFile => 'src/Controller/'.$params['controller_class_name'].'.php'
];
}

Expand All @@ -89,4 +91,9 @@ protected function configureDependencies(DependencyBuilder $dependencies)
'annotations'
);
}

private function isTwigInstalled()
{
return class_exists(Symfony\Bundle\TwigBundle\TwigBundle::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a use Symfony\Bundle\TwigBundle\TwigBundle statement and then use TwigBundle::class here.

}
}
3 changes: 1 addition & 2 deletions src/Resources/skeleton/controller/Controller.php.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class {{ controller_class_name }} extends Controller
*/
public function index()
{
// replace this line with your own code!
return $this->render('@Maker/demoPage.html.twig', [ 'path' => str_replace($this->getParameter('kernel.project_dir').'/', '', __FILE__) ]);
return new Response('Hello World');
}
}
19 changes: 19 additions & 0 deletions src/Resources/skeleton/controller/ControllerWithTwig.php.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class {{ controller_class_name }} extends Controller
{
/**
* @Route("{{ route_path }}", name="{{ route_name }}")
*/
public function index()
{
// replace this line with your own code!
return $this->render('@Maker/demoPage.html.twig', [ 'path' => str_replace($this->getParameter('kernel.project_dir').'/', '', __FILE__) ]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use demo.html.twig as a template name instead. @Maker won't work anyway and we don't recommend using @App.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a separate PR I'm going to propose to remove this code and the demo template entirely. We don't need it in a generator bundle (it's useful only after creating a new project, but we already have that covered).

}
}