Skip to content

Commit 8bd8596

Browse files
committed
Handle usage in namespaced templates
1 parent d657544 commit 8bd8596

3 files changed

Lines changed: 44 additions & 27 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"prefer-stable": true,
2323
"require": {
2424
"php": ">=7.2",
25-
"phug/phug": "^1.6"
25+
"phug/phug": "^1.7.2"
2626
},
2727
"require-dev": {
2828
"phpunit/phpunit": "^8.5",

src/Phug/Component/ComponentExtension.php

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function getKeywords(): array
7676
return [
7777
'slot' => static function (string $name): array {
7878
return [
79-
'begin' => '<?php if ('.static::class.'::slot('.var_export($name, true).', get_defined_vars())) { ?>',
79+
'begin' => '<?php if (\\'.static::class.'::slot('.var_export($name, true).', get_defined_vars())) { ?>',
8080
'end' => '<?php } ?>',
8181
];
8282
},
@@ -120,7 +120,17 @@ public function handleNodeEvent(NodeEvent $event): void
120120

121121
public function handleOutputEvent(OutputEvent $event): void
122122
{
123-
$event->setOutput($this->parseOutput($event->getOutput()));
123+
$event->prependCode(implode("\n", [
124+
'$firstMixin = function (string ...$names) use (&$__pug_mixins) {',
125+
' foreach ($names as $name) {',
126+
' if (isset($__pug_mixins[$name])) {',
127+
' return $name;',
128+
' }',
129+
' }',
130+
' throw new \\InvalidArgumentException("No defined mixin/component in [".implode(", ", $names)."]");',
131+
'};',
132+
'$firstComponent = $firstMixin;',
133+
]));
124134
}
125135

126136
public function attachEvents(): void
@@ -136,27 +146,4 @@ public function detachEvents(): void
136146
$compiler->detach(CompilerEvent::NODE, [$this, 'handleNodeEvent']);
137147
$compiler->detach(CompilerEvent::OUTPUT, [$this, 'handleOutputEvent']);
138148
}
139-
140-
private function parseOutput(string $output): string
141-
{
142-
$mixinFunctionsCode = implode("\n", [
143-
'',
144-
'$firstMixin = function (string ...$names) use (&$__pug_mixins) {',
145-
' foreach ($names as $name) {',
146-
' if (isset($__pug_mixins[$name])) {',
147-
' return $name;',
148-
' }',
149-
' }',
150-
' throw new \\InvalidArgumentException("No defined mixin/component in [".implode(", ", $names)."]");',
151-
'};',
152-
'$firstComponent = $firstMixin;',
153-
'',
154-
]);
155-
156-
if (preg_match('/^(<\?(?:php)?\s+namespace\s\S.*)((\n[\s\S]*)?)$/U', $output, $matches)) {
157-
return $matches[1].$mixinFunctionsCode.$matches[2];
158-
}
159-
160-
return "<?php$mixinFunctionsCode?>$output";
161-
}
162149
}

tests/Phug/Component/ComponentExtensionTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use PHPUnit\Framework\TestCase;
77
use Phug\Compiler\Event\NodeEvent;
8+
use Phug\Compiler\Event\OutputEvent;
89
use Phug\CompilerEvent;
910
use Phug\Component\ComponentExtension;
1011
use Phug\Formatter\Element\KeywordElement;
@@ -261,7 +262,7 @@ public function testSlotKeyword()
261262
$keyword = new KeywordElement('slot', '', null, null, [(new TextElement)->setValue('Hello')]);
262263

263264
$this->assertSame([
264-
'begin' => '<?php if (Phug\Component\ComponentExtension::slot(\'foobar\', get_defined_vars())) { ?>',
265+
'begin' => '<?php if (\Phug\Component\ComponentExtension::slot(\'foobar\', get_defined_vars())) { ?>',
265266
'end' => '<?php } ?>',
266267
], $slot('foobar', $keyword));
267268
}
@@ -370,6 +371,35 @@ public function testWithPug()
370371
]))));
371372
}
372373

374+
/**
375+
* @throws PhugException
376+
*/
377+
public function testNamespace()
378+
{
379+
$pug = new Pug([
380+
'on_output' => function (OutputEvent $event) {
381+
$event->prependCode('namespace pug;');
382+
},
383+
]);
384+
ComponentExtension::enable($pug);
385+
386+
$this->assertSame(implode("\n", [
387+
'Title',
388+
'<article data-attr="5">',
389+
' Content',
390+
'</article>',
391+
]), $this->format($pug->render(implode("\n", [
392+
'mixin foobar(obj)',
393+
' slot title',
394+
' article(data-attr=obj.a): slot',
395+
'+foobar({a: 5})',
396+
' slot title',
397+
' | Title',
398+
' slot __main__',
399+
' | Content',
400+
]))));
401+
}
402+
373403
public function getPugPhpTestsTemplates(): array
374404
{
375405
return array_map(function ($file) {

0 commit comments

Comments
 (0)