Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed PHP namespaces usage #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/MtHaml/Support/Php/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,23 @@ private function compileFile($file)

private function wrapCompiledCode($code, $funName)
{
// Parses all used namespaces and puts them into header of the file.
$namespaces = array();
if (preg_match_all('/<\?php\s+(use\s+[^;\?]+);?(?:\s+)?\?>\n?/S', $code, $matches, PREG_OFFSET_CAPTURE)) {
for ($i = count($matches[0])-1; $i >= 0; $i--) {
$code = substr($code, 0, $matches[0][$i][1]) .
substr($code, $matches[0][$i][1] + strlen($matches[0][$i][0]));
array_unshift($namespaces, $matches[1][$i][0].';');
}
}
$namespaces = implode(PHP_EOL, $namespaces);

// The code is wrapped in a function so that it can be parsed
// once, and executed multiple times. This is faster than repeatedly
// including the same PHP file.
return <<<PHP
<?php

$namespaces
function $funName(\$__variables)
{
extract(\$__variables);
Expand Down