/** * undocumented function * * @param Compiler * @return void **/ public function __construct(Compiler $compiler) { $this->compiler = $compiler; foreach ($this->functions as $volt => $func) { if (is_string($func)) { $compiler->addFunction($volt, $func); } else { $compiler->addFunction($volt, $this->{$func}); } } }
/** * @param VoltCompiler $compiler */ public static function install($compiler) { foreach (get_class_methods(get_called_class()) as $method) { if ($method != __METHOD__) { $compiler->addFunction(Text::uncamelize($method), function ($resolvedArgs, $exprArgs) use($method) { return get_called_class() . '::' . $method . '(' . $resolvedArgs . ')'; }); } } }
public function testVoltUsersFunctions() { $this->specify("Custom functions should work", function () { $volt = new Compiler(); //Single string function $volt->addFunction('random', 'mt_rand'); //Function with closure $volt->addFunction('shuffle', function ($arguments, $exprArguments) { return 'str_shuffle(' . $arguments . ')'; }); $volt->addFunction('strtotime', 'strtotime'); $compilation = $volt->compileString('{{ random() }}'); expect($compilation)->equals('<?= mt_rand() ?>'); $compilation = $volt->compileString('{{ shuffle("hello") }}'); expect($compilation)->equals('<?= str_shuffle(\'hello\') ?>'); $compilation = $volt->compileString('{{ strtotime("now") }}'); expect($compilation)->equals("<?= strtotime('now') ?>"); }); }
public function testVoltUsersFunctions() { $volt = new Compiler(); //Single string function $volt->addFunction('random', 'mt_rand'); //Function with closure $volt->addFunction('shuffle', function ($arguments, $exprArguments) { return 'str_shuffle(' . $arguments . ')'; }); $compilation = $volt->compileString('{{ random() }}'); $this->assertEquals($compilation, '<?php echo mt_rand(); ?>'); $compilation = $volt->compileString('{{ shuffle("hello") }}'); $this->assertEquals($compilation, '<?php echo str_shuffle(\'hello\'); ?>'); }
public function getVoltCompiler() { $volt = new VoltCompiler(); $volt->setOptions(array('stat' => true, 'compileAlways' => true)); require CORE_PATH . 'config/volt.functions.php'; foreach ($voltFunctions as $macro => $function) { $volt->addFunction($macro, $function); } return $volt; }