Skip to content

Commit 952fd36

Browse files
committed
Keep namespace statements at file beginning
1 parent 05e9d7e commit 952fd36

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"require": {
1313
"php": ">=7.0",
1414
"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"
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-15
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,16 +84,6 @@ protected function parseOutput($compiler, $output)
8684
$output
8785
);
8886

89-
$dependencies = $jsPhpize->compileDependencies();
90-
if ($dependencies !== '') {
91-
$dependencies = $compiler->getFormatter()->handleCode($dependencies);
92-
$output = preg_match('/^(<\?(?:php)?\s+namespace\s\S.*)(((?:;|\n|\?>)[\s\S]*)?)$/U', $output, $matches)
93-
? $matches[1] . $dependencies . $matches[2]
94-
: $dependencies . $output;
95-
}
96-
97-
$jsPhpize->flushDependencies();
98-
9987
return $output;
10088
}
10189

@@ -104,8 +92,17 @@ public function handleOutputEvent(Compiler\Event\OutputEvent $event)
10492
/** @var CompilerInterface $compiler */
10593
$compiler = $event->getTarget();
10694

107-
$event->setOutput($this->parseOutput($compiler, $event->getOutput()));
95+
$event->setOutput($this->parseOutput($event->getOutput()));
10896

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();
109106
$compiler->unsetOption('jsphpize_engine');
110107
}
111108

0 commit comments

Comments
 (0)