Skip to content

Commit d9db2dd

Browse files
committed
feature #60 Php templates (weaverryan)
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
2 parents 3defef8 + 8fe5462 commit d9db2dd

33 files changed

+120
-118
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"symfony/http-kernel": "^3.4|^4.0"
2121
},
2222
"require-dev": {
23+
"friendsofphp/php-cs-fixer": "^2.8",
2324
"symfony/phpunit-bridge": "^3.4|^4.0",
2425
"symfony/process": "^3.4|^4.0"
2526
},

src/FileManager.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ public function setIO(SymfonyStyle $io): void
4040

4141
public function parseTemplate(string $templatePath, array $parameters): string
4242
{
43-
$keys = array_keys($parameters);
44-
$values = array_values($parameters);
45-
$placeholders = array_map(function ($name) {
46-
return "{{ $name }}";
47-
}, $keys);
43+
ob_start();
44+
extract($parameters, EXTR_SKIP);
45+
include $templatePath;
4846

49-
return str_replace($placeholders, $values, file_get_contents($templatePath));
47+
return ob_get_clean();
5048
}
5149

5250
public function dumpFile(string $filename, string $content): void

src/Maker/MakeAuthenticator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function getParameters(InputInterface $input): array
5858
public function getFiles(array $params): array
5959
{
6060
return [
61-
__DIR__.'/../Resources/skeleton/authenticator/Empty.php.txt' => 'src/Security/'.$params['class_name'].'.php',
61+
__DIR__.'/../Resources/skeleton/authenticator/Empty.tpl.php' => 'src/Security/'.$params['class_name'].'.php',
6262
];
6363
}
6464

src/Maker/MakeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function getParameters(InputInterface $input): array
6060
public function getFiles(array $params): array
6161
{
6262
return [
63-
__DIR__.'/../Resources/skeleton/command/Command.php.txt' => 'src/Command/'.$params['command_class_name'].'.php',
63+
__DIR__.'/../Resources/skeleton/command/Command.tpl.php' => 'src/Command/'.$params['command_class_name'].'.php',
6464
];
6565
}
6666

src/Maker/MakeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function getParameters(InputInterface $input): array
6969

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

7474
return [
7575
__DIR__.'/../Resources/skeleton/controller/'.$skeletonFile => 'src/Controller/'.$params['controller_class_name'].'.php',

src/Maker/MakeEntity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public function getParameters(InputInterface $input): array
6363
public function getFiles(array $params): array
6464
{
6565
return [
66-
__DIR__.'/../Resources/skeleton/doctrine/Entity.php.txt' => 'src/Entity/'.$params['entity_class_name'].'.php',
67-
__DIR__.'/../Resources/skeleton/doctrine/Repository.php.txt' => 'src/Repository/'.$params['repository_class_name'].'.php',
66+
__DIR__.'/../Resources/skeleton/doctrine/Entity.tpl.php' => 'src/Entity/'.$params['entity_class_name'].'.php',
67+
__DIR__.'/../Resources/skeleton/doctrine/Repository.tpl.php' => 'src/Repository/'.$params['repository_class_name'].'.php',
6868
];
6969
}
7070

src/Maker/MakeForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function getParameters(InputInterface $input): array
6262
public function getFiles(array $params): array
6363
{
6464
return [
65-
__DIR__.'/../Resources/skeleton/form/Type.php.txt' => 'src/Form/'.$params['form_class_name'].'.php',
65+
__DIR__.'/../Resources/skeleton/form/Type.tpl.php' => 'src/Form/'.$params['form_class_name'].'.php',
6666
];
6767
}
6868

src/Maker/MakeFunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function getParameters(InputInterface $input): array
5858
public function getFiles(array $params): array
5959
{
6060
return [
61-
__DIR__.'/../Resources/skeleton/test/Functional.php.txt' => 'tests/'.$params['test_class_name'].'.php',
61+
__DIR__.'/../Resources/skeleton/test/Functional.tpl.php' => 'tests/'.$params['test_class_name'].'.php',
6262
];
6363
}
6464

src/Maker/MakeSerializerEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getParameters(InputInterface $input): array
6161
public function getFiles(array $params): array
6262
{
6363
return [
64-
__DIR__.'/../Resources/skeleton/serializer/Encoder.php.txt' => 'src/Serializer/'.$params['encoder_class_name'].'.php',
64+
__DIR__.'/../Resources/skeleton/serializer/Encoder.tpl.php' => 'src/Serializer/'.$params['encoder_class_name'].'.php',
6565
];
6666
}
6767

src/Maker/MakeSubscriber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ public function getParameters(InputInterface $input): array
8585
'event' => $event,
8686
'eventArg' => $eventShortName ? sprintf('%s $event', $eventShortName) : '$event',
8787
'methodName' => Str::asEventMethod($event),
88-
'eventUseStatement' => $eventClass ? sprintf("use $eventClass;\n") : '',
88+
'eventClass' => $eventClass,
8989
];
9090
}
9191

9292
public function getFiles(array $params): array
9393
{
9494
return [
95-
__DIR__.'/../Resources/skeleton/event/Subscriber.php.txt' => 'src/EventSubscriber/'.$params['subscriber_class_name'].'.php',
95+
__DIR__.'/../Resources/skeleton/event/Subscriber.tpl.php' => 'src/EventSubscriber/'.$params['subscriber_class_name'].'.php',
9696
];
9797
}
9898

0 commit comments

Comments
 (0)