Skip to content

Commit feb5ca9

Browse files
authored
Merge pull request #31 from pug-php/keep-extend
Fix #30 Keep extends as first statement
2 parents 5a7c267 + a1e88e7 commit feb5ca9

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed

src/Jade/JadeSymfonyEngine.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ public function __construct($kernel)
101101
$app->setDebug($kernel->isDebug());
102102
$app->setEnvironment($environment);
103103
$app->setRequestStack($container->get('request_stack'));
104+
// @codeCoverageIgnoreStart
104105
if ($container->has('security.token_storage')) {
105106
$app->setTokenStorage($container->get('security.token_storage'));
106107
}
108+
// @codeCoverageIgnoreEnd
107109
$this->share('app', $app);
108110
}
109111

@@ -155,6 +157,22 @@ protected function getFileFromName($name)
155157
return $directory . DIRECTORY_SEPARATOR . $name;
156158
}
157159

160+
protected function getPugCodeLayoutStructure($pugCode)
161+
{
162+
$parts = preg_split('/^extend(?=s?\s)/m', $pugCode, 2);
163+
164+
if (count($parts) === 1) {
165+
return ['', $parts[0]];
166+
}
167+
168+
$parts[1] .= "\n";
169+
$parts[1] = explode("\n", $parts[1], 2);
170+
$parts[0] .= 'extend' . $parts[1][0] . "\n";
171+
$parts[1] = substr($parts[1][1], 0, -1);
172+
173+
return $parts;
174+
}
175+
158176
/**
159177
* Share variables (local templates parameters) with all future templates rendered.
160178
*
@@ -182,18 +200,18 @@ public function share($variables, $value = null)
182200
*/
183201
public function preRender($pugCode)
184202
{
185-
$preCode = '';
203+
$parts = $this->getPugCodeLayoutStructure($pugCode);
186204
$className = get_class($this);
187205
foreach ($this->replacements as $name => $callable) {
188-
$preCode .= ":php\n" .
206+
$parts[0] .= ":php\n" .
189207
" if (!function_exists('$name')) {\n" .
190208
" function $name() {\n" .
191209
" return call_user_func_array($className::getGlobalHelper('$name'), func_get_args());\n" .
192210
" }\n" .
193211
" }\n";
194212
}
195213

196-
return $preCode . $pugCode;
214+
return implode('', $parts);
197215
}
198216

199217
/**
@@ -302,9 +320,11 @@ public function exists($name)
302320
*/
303321
public function supports($name)
304322
{
323+
// @codeCoverageIgnoreStart
305324
$extensions = method_exists($this->jade, 'getExtensions')
306325
? $this->jade->getExtensions()
307326
: $this->jade->getOption('extensions');
327+
// @codeCoverageIgnoreEnd
308328
foreach ($extensions as $extension) {
309329
if (substr($name, -strlen($extension)) === $extension) {
310330
return true;

src/Jade/Symfony/Traits/Options.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ trait Options
2121
*/
2222
public function getOptionDefault($name, $default = null)
2323
{
24+
// @codeCoverageIgnoreStart
2425
try {
2526
return $this->getOption($name, $default);
2627
} catch (\InvalidArgumentException $exception) {
2728
return $default;
2829
}
30+
// @codeCoverageIgnoreEnd
2931
}
3032

3133
/**

tests/Pug/PugSymfonyBundle/Command/AssetsPublishCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testCommand()
5252
file_put_contents($customHelperFile, $customHelper);
5353
file_put_contents($stylePhpFile, $stylePhp);
5454

55-
$this->assertContains('9 templates cached', $output, 'All templates can be cached except filter.pug as the upper filter does not exists.');
55+
$this->assertContains('11 templates cached', $output, 'All templates can be cached except filter.pug as the upper filter does not exists.');
5656
$this->assertContains('1 templates failed to be cached', $output, 'filter.pug fails as the upper filter does not exists.');
5757
$this->assertRegExp('/(Unknown\sfilter\supper|upper:\sFilter\sdoes\s?n[\'o]t\sexists)/', $output, 'filter.pug fails as the upper filter does not exists.');
5858
$this->assertContains('filter.pug', $output, 'filter.pug fails as the upper filter does not exists.');

tests/Pug/PugSymfonyEngineTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,25 @@ public function testPreRender()
217217
$pugSymfony = new PugSymfonyEngine($kernel);
218218

219219
self::assertSame('<p>/foo</p>', $pugSymfony->renderString('p=asset("foo")'));
220+
221+
$kernel = new TestKernel(function (Container $container) {
222+
$container->setParameter('pug', [
223+
'expressionLanguage' => 'js',
224+
]);
225+
});
226+
$kernel->boot();
227+
$pugSymfony = new PugSymfonyEngine($kernel);
228+
229+
self::assertSame('<p>/foo</p>', $pugSymfony->renderString('p=asset("foo")'));
230+
231+
self::assertSame(implode('', [
232+
'<html>',
233+
'<head><title>My Site</title></head>',
234+
'<body><h1>Welcome Bob</h1><p>42</p><footer><p></p>Some footer text</footer></body>',
235+
'</html>',
236+
]), $pugSymfony->render('layout/welcome.pug', [
237+
'name' => 'Bob',
238+
]));
220239
}
221240

222241
/**
@@ -810,6 +829,32 @@ public function testInstallSymfony4()
810829
self::assertContains('Engine service added in config/packages/framework.yaml', $io->getLastOutput());
811830
clearstatcache();
812831
self::assertFileExists($installedFile);
832+
833+
$fs->remove($installedFile);
834+
file_put_contents($dir . '/config/services.yaml', str_replace(
835+
['services', 'pug'],
836+
['x', 'x'],
837+
file_get_contents($dir . '/config/services.yaml')
838+
));
839+
file_put_contents($dir . '/config/packages/framework.yaml', str_replace(
840+
['pug', 'twig', 'framework'],
841+
['x', 'x', 'x'],
842+
file_get_contents($dir . '/config/packages/framework.yaml')
843+
));
844+
file_put_contents($dir . '/config/bundles.php', preg_replace(
845+
'/\[\s*\n\s*/',
846+
'[',
847+
file_get_contents($dir . '/config/bundles.php')
848+
));
849+
$io->reset();
850+
851+
self::assertTrue(PugSymfonyEngine::install(new Event('install', $composer, $io), $dir));
852+
self::assertSame([
853+
'framework entry not found in config/packages/framework.yaml.',
854+
'services entry not found in config/services.yaml.',
855+
'Sorry, config/bundles.php has a format we can\'t handle automatically.',
856+
], $io->getLastOutput());
857+
$fs->remove($installedFile);
813858
}
814859

815860
public function testOptionDefaultingOnException()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
html
2+
head
3+
title My Site
4+
body
5+
block content
6+
footer
7+
p
8+
| Some footer text
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extend layout
2+
3+
block content
4+
h1 Welcome #{name}
5+
6+
p=random(42, 42)

0 commit comments

Comments
 (0)