Skip to content

Commit

Permalink
feature #60 Php templates (weaverryan)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 1.0-dev branch (closes #60).

Discussion
----------

Php templates

This depends on #59. So sorry for the messy diff until that is merged:

tl;dr; This renders the templates as PHP, instead of simple `{{ foo }}` find and replace. We do not want to make the templates super dynamic and fancy, but we're already running into a few spots where we're hitting this limitation (case: `make:controller` which has *two* possible templates, because the current replace is too limiting).

The only ugly part is that the `<?php` in the templates needs to be written as `//PHP_OPEN` to avoid syntax errors (we then replace this with `<?php` after including). Here is an example: https://github.com/symfony/maker-bundle/compare/master...weaverryan:php-templates?expand=1#diff-3ecc33834a0f1ec4f8e5516d71e4ad35

Btw, if you're wondering "Why not use Twig!?". We don't want to create a dependency on Twig to use the maker :).

Cheers!

Commits
-------

8fe5462 Php templates
  • Loading branch information
weaverryan committed Nov 29, 2017
2 parents 3defef8 + 8fe5462 commit d9db2dd
Show file tree
Hide file tree
Showing 33 changed files with 120 additions and 118 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"symfony/http-kernel": "^3.4|^4.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.8",
"symfony/phpunit-bridge": "^3.4|^4.0",
"symfony/process": "^3.4|^4.0"
},
Expand Down
10 changes: 4 additions & 6 deletions src/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ public function setIO(SymfonyStyle $io): void

public function parseTemplate(string $templatePath, array $parameters): string
{
$keys = array_keys($parameters);
$values = array_values($parameters);
$placeholders = array_map(function ($name) {
return "{{ $name }}";
}, $keys);
ob_start();
extract($parameters, EXTR_SKIP);
include $templatePath;

return str_replace($placeholders, $values, file_get_contents($templatePath));
return ob_get_clean();
}

public function dumpFile(string $filename, string $content): void
Expand Down
2 changes: 1 addition & 1 deletion src/Maker/MakeAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function getParameters(InputInterface $input): array
public function getFiles(array $params): array
{
return [
__DIR__.'/../Resources/skeleton/authenticator/Empty.php.txt' => 'src/Security/'.$params['class_name'].'.php',
__DIR__.'/../Resources/skeleton/authenticator/Empty.tpl.php' => 'src/Security/'.$params['class_name'].'.php',
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Maker/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function getParameters(InputInterface $input): array
public function getFiles(array $params): array
{
return [
__DIR__.'/../Resources/skeleton/command/Command.php.txt' => 'src/Command/'.$params['command_class_name'].'.php',
__DIR__.'/../Resources/skeleton/command/Command.tpl.php' => 'src/Command/'.$params['command_class_name'].'.php',
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Maker/MakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getParameters(InputInterface $input): array

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

return [
__DIR__.'/../Resources/skeleton/controller/'.$skeletonFile => 'src/Controller/'.$params['controller_class_name'].'.php',
Expand Down
4 changes: 2 additions & 2 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public function getParameters(InputInterface $input): array
public function getFiles(array $params): array
{
return [
__DIR__.'/../Resources/skeleton/doctrine/Entity.php.txt' => 'src/Entity/'.$params['entity_class_name'].'.php',
__DIR__.'/../Resources/skeleton/doctrine/Repository.php.txt' => 'src/Repository/'.$params['repository_class_name'].'.php',
__DIR__.'/../Resources/skeleton/doctrine/Entity.tpl.php' => 'src/Entity/'.$params['entity_class_name'].'.php',
__DIR__.'/../Resources/skeleton/doctrine/Repository.tpl.php' => 'src/Repository/'.$params['repository_class_name'].'.php',
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Maker/MakeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getParameters(InputInterface $input): array
public function getFiles(array $params): array
{
return [
__DIR__.'/../Resources/skeleton/form/Type.php.txt' => 'src/Form/'.$params['form_class_name'].'.php',
__DIR__.'/../Resources/skeleton/form/Type.tpl.php' => 'src/Form/'.$params['form_class_name'].'.php',
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Maker/MakeFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function getParameters(InputInterface $input): array
public function getFiles(array $params): array
{
return [
__DIR__.'/../Resources/skeleton/test/Functional.php.txt' => 'tests/'.$params['test_class_name'].'.php',
__DIR__.'/../Resources/skeleton/test/Functional.tpl.php' => 'tests/'.$params['test_class_name'].'.php',
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Maker/MakeSerializerEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getParameters(InputInterface $input): array
public function getFiles(array $params): array
{
return [
__DIR__.'/../Resources/skeleton/serializer/Encoder.php.txt' => 'src/Serializer/'.$params['encoder_class_name'].'.php',
__DIR__.'/../Resources/skeleton/serializer/Encoder.tpl.php' => 'src/Serializer/'.$params['encoder_class_name'].'.php',
];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Maker/MakeSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ public function getParameters(InputInterface $input): array
'event' => $event,
'eventArg' => $eventShortName ? sprintf('%s $event', $eventShortName) : '$event',
'methodName' => Str::asEventMethod($event),
'eventUseStatement' => $eventClass ? sprintf("use $eventClass;\n") : '',
'eventClass' => $eventClass,
];
}

public function getFiles(array $params): array
{
return [
__DIR__.'/../Resources/skeleton/event/Subscriber.php.txt' => 'src/EventSubscriber/'.$params['subscriber_class_name'].'.php',
__DIR__.'/../Resources/skeleton/event/Subscriber.tpl.php' => 'src/EventSubscriber/'.$params['subscriber_class_name'].'.php',
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Maker/MakeTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function getParameters(InputInterface $input): array
public function getFiles(array $params): array
{
return [
__DIR__.'/../Resources/skeleton/twig/Extension.php.txt' => 'src/Twig/'.$params['extension_class_name'].'.php',
__DIR__.'/../Resources/skeleton/twig/Extension.tpl.php' => 'src/Twig/'.$params['extension_class_name'].'.php',
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Maker/MakeUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function getParameters(InputInterface $input): array
public function getFiles(array $params): array
{
return [
__DIR__.'/../Resources/skeleton/test/Unit.php.txt' => 'tests/'.$params['test_class_name'].'.php',
__DIR__.'/../Resources/skeleton/test/Unit.tpl.php' => 'tests/'.$params['test_class_name'].'.php',
];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Maker/MakeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public function getParameters(InputInterface $input): array
public function getFiles(array $params): array
{
return [
__DIR__.'/../Resources/skeleton/validator/Validator.php.txt' => 'src/Validator/Constraints/'.$params['validator_class_name'].'.php',
__DIR__.'/../Resources/skeleton/validator/Constraint.php.txt' => 'src/Validator/Constraints/'.$params['constraint_class_name'].'.php',
__DIR__.'/../Resources/skeleton/validator/Validator.tpl.php' => 'src/Validator/Constraints/'.$params['validator_class_name'].'.php',
__DIR__.'/../Resources/skeleton/validator/Constraint.tpl.php' => 'src/Validator/Constraints/'.$params['constraint_class_name'].'.php',
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Maker/MakeVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function getParameters(InputInterface $input): array
public function getFiles(array $params): array
{
return [
__DIR__.'/../Resources/skeleton/security/Voter.php.txt' => 'src/Security/Voter/'.$params['voter_class_name'].'.php',
__DIR__.'/../Resources/skeleton/security/Voter.tpl.php' => 'src/Security/Voter/'.$params['voter_class_name'].'.php',
];
}

Expand Down
4 changes: 3 additions & 1 deletion src/MakerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ public function getParameters(InputInterface $input): array;
* For example:
*
* return array(
* __DIR__.'/../Resources/skeleton/command/Command.php.txt' => 'src/Command/'.$params['command_class_name'].'.php',
* __DIR__.'/../Resources/skeleton/command/Command.tpl.php' => 'src/Command/'.$params['command_class_name'].'.php',
* );
*
* These files are parsed as PHP.
*
* @param array $params The parameters returned from getParameters()
*
* @return array
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?= "<?php\n" ?>

namespace App\Security;

Expand All @@ -9,45 +9,45 @@
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\GuardAuthenticator;

class {{ class_name }} extends GuardAuthenticator
class <?= $class_name ?> extends GuardAuthenticator
{
public function supports(Request $request)
{

// todo
}

public function getCredentials(Request $request)
{

// todo
}

public function getUser($credentials, UserProviderInterface $userProvider)
{

// todo
}

public function checkCredentials($credentials, UserInterface $user)
{

// todo
}

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{

// todo
}

public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{

// todo
}

public function supportsRememberMe()
public function start(Request $request, AuthenticationException $authException = null)
{

// todo
}

public function start(Request $request, AuthenticationException $authException = null)
public function supportsRememberMe()
{

// todo
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?= "<?php\n" ?>

namespace App\Command;

Expand All @@ -9,9 +9,9 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class {{ command_class_name }} extends Command
class <?= $command_class_name ?> extends Command
{
protected static $defaultName = '{{ command_name }}';
protected static $defaultName = '<?= $command_name ?>';

protected function configure()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
<?= "<?php\n" ?>

namespace App\Controller;

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

class {{ controller_class_name }} extends AbstractController
class <?= $controller_class_name ?> extends AbstractController
{
/**
* @Route("{{ route_path }}", name="{{ route_name }}")
* @Route("<?= $route_path ?>", name="<?= $route_name ?>")
*/
public function index()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
<?= "<?php\n" ?>

namespace App\Controller;

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

class {{ controller_class_name }} extends AbstractController
class <?= $controller_class_name ?> extends AbstractController
{
/**
* @Route("{{ route_path }}", name="{{ route_name }}")
* @Route("<?= $route_path ?>", name="<?= $route_name ?>")
*/
public function index()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
<?= "<?php\n" ?>

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="App\Repository\{{ repository_class_name }}")
* @ORM\Entity(repositoryClass="App\Repository\<?= $repository_class_name ?>")
*/
class {{ entity_class_name }}
class <?= $entity_class_name."\n" ?>
{
/**
* @ORM\Id
Expand Down
28 changes: 0 additions & 28 deletions src/Resources/skeleton/doctrine/Repository.php.txt

This file was deleted.

28 changes: 28 additions & 0 deletions src/Resources/skeleton/doctrine/Repository.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?= "<?php\n" ?>

namespace App\Repository;

use App\Entity\<?= $entity_class_name ?>;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

class <?= $repository_class_name ?> extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, <?= $entity_class_name ?>::class);
}

/*
public function findBySomething($value)
{
return $this->createQueryBuilder('<?= $entity_alias ?>')
->where('<?= $entity_alias ?>.something = :value')->setParameter('value', $value)
->orderBy('<?= $entity_alias ?>.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
}
20 changes: 0 additions & 20 deletions src/Resources/skeleton/event/Subscriber.php.txt

This file was deleted.

21 changes: 21 additions & 0 deletions src/Resources/skeleton/event/Subscriber.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?= "<?php\n" ?>

namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
<?= $eventClass ? "use $eventClass;\n" : '' ?>

class <?= $subscriber_class_name ?> implements EventSubscriberInterface
{
public function <?= $methodName ?>(<?= $eventArg ?>)
{
// ...
}

public static function getSubscribedEvents()
{
return [
'<?= $event ?>' => '<?= $methodName ?>',
];
}
}
Loading

0 comments on commit d9db2dd

Please sign in to comment.