Skip to content

Commit 8bd3a87

Browse files
committed
Implement functionsNamespace option
1 parent ca93300 commit 8bd3a87

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/JsPhpize/Compiler/Compiler.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@ protected function visitFunctionCall(FunctionCall $functionCall, $indent)
224224
$dynamicCall = '(function_exists(' . var_export($name, true) . ') ? ' .
225225
$staticCall . ' : ' .
226226
$dynamicCall . ')';
227+
228+
$functionNamespace = $this->engine->getOption('functionsNamespace');
229+
230+
if ($functionNamespace) {
231+
$dynamicCall = '(function_exists(' . var_export($functionNamespace . '\\' . $name, true) . ') ? ' .
232+
'\\' . $functionNamespace . '\\' . $staticCall . ' : ' .
233+
$dynamicCall . ')';
234+
}
227235
}
228236

229237
return $this->handleVariableChildren($functionCall, $indent, $dynamicCall);

tests/functionInNamespace.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace myNameSpace;
4+
5+
function fooBar()
6+
{
7+
return 'Hello';
8+
}

tests/options.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,30 @@ public function testBooleanLogicalOperators()
169169
$this->assertSame(false, $jsPhpize->render('return null && false'));
170170
$this->assertSame(false, $jsPhpize->render('return 0 || null'));
171171
}
172+
173+
/**
174+
* @throws \JsPhpize\Compiler\Exception
175+
* @throws \JsPhpize\Lexer\Exception
176+
* @throws \JsPhpize\Parser\Exception
177+
*/
178+
public function testFunctionNamespace()
179+
{
180+
include_once __DIR__ . '/functionInNamespace.php';
181+
182+
$jsPhpize = new JsPhpize();
183+
$message = null;
184+
185+
try {
186+
$jsPhpize->render('return fooBar()');
187+
} catch (\JsPhpize\Compiler\Exception $exception) {
188+
$message = $exception->getMessage();
189+
}
190+
191+
$this->assertRegExp('/Undefined variable: fooBar/', $message);
192+
193+
$jsPhpize = new JsPhpize([
194+
'functionsNamespace' => 'myNameSpace',
195+
]);
196+
$this->assertSame('Hello', $jsPhpize->render('return fooBar()'));
197+
}
172198
}

0 commit comments

Comments
 (0)