Skip to content

Commit fcc59b3

Browse files
authored
Merge pull request #9 from pug-php/namespace-statement
Keep namespace statements at file beginning
2 parents 086536a + 1fcc479 commit fcc59b3

File tree

4 files changed

+55
-15
lines changed

4 files changed

+55
-15
lines changed

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
],
1212
"require": {
1313
"php": ">=7.0",
14-
"js-phpize/js-phpize": "^2.0.0",
15-
"phug/compiler": "^0.5.0 || ^1.0@dev",
16-
"phug/formatter": "^0.5.43 || ^1.0@dev"
14+
"js-phpize/js-phpize": "^2.8.3",
15+
"phug/compiler": "^1.7.2",
16+
"phug/formatter": "^1.7.2"
1717
},
1818
"require-dev": {
1919
"phpunit/phpunit": ">=4.8.35 <6.0",

src/JsPhpize/Traits/Compilation.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,12 @@ public function compile(JsPhpize $jsPhpize, $code, $fileName)
6565
}
6666

6767
/**
68-
* @param CompilerInterface $compiler
69-
* @param string $output
68+
* @param string $output
7069
*
7170
* @return string
7271
*/
73-
protected function parseOutput($compiler, $output)
72+
protected function parseOutput($output)
7473
{
75-
$jsPhpize = $this->getJsPhpizeEngine($compiler);
7674
$output = preg_replace(
7775
'/\{\s*\?><\?(?:php)?\s*\}/',
7876
'{}',
@@ -86,13 +84,6 @@ protected function parseOutput($compiler, $output)
8684
$output
8785
);
8886

89-
$dependencies = $jsPhpize->compileDependencies();
90-
if ($dependencies !== '') {
91-
$output = $compiler->getFormatter()->handleCode($dependencies) . $output;
92-
}
93-
94-
$jsPhpize->flushDependencies();
95-
9687
return $output;
9788
}
9889

@@ -101,8 +92,17 @@ public function handleOutputEvent(Compiler\Event\OutputEvent $event)
10192
/** @var CompilerInterface $compiler */
10293
$compiler = $event->getTarget();
10394

104-
$event->setOutput($this->parseOutput($compiler, $event->getOutput()));
95+
$event->setOutput($this->parseOutput($event->getOutput()));
10596

97+
$jsPhpize = $this->getJsPhpizeEngine($compiler);
98+
$dependencies = $jsPhpize->compileDependencies();
99+
100+
if ($dependencies !== '') {
101+
$dependencies = $compiler->getFormatter()->handleCode($dependencies);
102+
$event->prependOutput($dependencies);
103+
}
104+
105+
$jsPhpize->flushDependencies();
106106
$compiler->unsetOption('jsphpize_engine');
107107
}
108108

tests/JsPhpize/Example/Example.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Tests\JsPhpize\Example;
4+
5+
class Example
6+
{
7+
public static function foo()
8+
{
9+
return 'bar';
10+
}
11+
}

tests/JsPhpize/JsPhpizePhugTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use JsPhpize\JsPhpizePhug;
77
use PHPUnit\Framework\TestCase;
88
use Phug\Compiler;
9+
use Phug\CompilerEvent;
910
use Phug\Renderer;
1011
use Tests\Thrower;
1112

@@ -102,6 +103,33 @@ public function testPlug()
102103
);
103104
}
104105

106+
public function testNamespaceInsertion()
107+
{
108+
include_once __DIR__ . '/Example/Example.php';
109+
110+
$compiler = new Compiler([
111+
'compiler_modules' => [JsPhpizePhug::class],
112+
]);
113+
$compiler->attach(CompilerEvent::OUTPUT, function (Compiler\Event\OutputEvent $outputEvent) {
114+
$outputEvent->prependCode('namespace Tests\JsPhpize\Example;');
115+
});
116+
117+
$user = [
118+
'name' => 'Bob',
119+
];
120+
121+
ob_start();
122+
$php = $compiler->compile('a(title=user.name foo=Example::foo())');
123+
eval('?>' . $php);
124+
$html = ob_get_contents();
125+
ob_end_clean();
126+
127+
self::assertSame(
128+
'<a title="Bob" foo="bar"></a>',
129+
$html
130+
);
131+
}
132+
105133
public function testTruncatedCode()
106134
{
107135
$compiler = new Compiler([
@@ -123,6 +151,7 @@ public function testTruncatedCode()
123151
$php5Syntax = 'call_user_func(call_user_func($GLOBALS[\'__jpv_dotWithArrayPrototype\'], $items, ' .
124152
'\'forEach\'), function ($item) {';
125153
$actual = $jsPhpize('items.forEach(function (item) {');
154+
126155
self::assertContains(
127156
$jsPhpize('items.forEach(function (item) {'),
128157
[$php5Syntax, $php7Syntax],

0 commit comments

Comments
 (0)