public function compile(Compiler $compiler) { $compiler->raw('$this->filter(')->string($this->getAttribute(self::ATTR_NAME))->raw(', '); $this->getArguments()->compile($compiler); $compiler->raw(', function() use ( $context ){ return '); $this->getNode(self::NODE_CONTENT)->compile($compiler); $compiler->raw(' ;}'); $compiler->raw(')'); }
/** * @dataProvider dataCompile * @param $template * @param $compiled * @throws \Azera\Fry\Exception\Exception * @throws \Azera\Fry\Exception\LexerException */ public function testCompiles($template, $compiled) { $env = $this->getEnvironment()->addExtension(new Core()); $lexer = new Lexer(new Reader($template), $env); $parser = new Parser($lexer->tokenize()->getStream(), $env); $bodyNode = $parser->parse()->getBody(); $compiler = new Compiler($this->getEnvironment()); $output = $compiler->compile($bodyNode); $this->assertEquals(trim($compiled), trim($output)); }
public function compile(Compiler $compiler) { $compiler->write('$this->set( $context , ')->raw('["' . implode('","', $this->convertToRoute($this->getAttribute(self::ATTR_VARIABLE))) . '"]')->raw(' , function() use ( $context ) {' . "\n")->indent(); if ($this->getAttribute(self::ATTR_IS_BLOCK)) { $compiler->writeln('ob_start();')->subcompile($this->getNode(self::NODE_VALUE))->writeln('return ob_get_clean();'); } else { $compiler->write('return ')->subcompile($this->getNode(self::NODE_VALUE))->raw(';' . "\n"); } $compiler->outdent()->writeln('});' . "\n"); }
private function compileArguments(Compiler $compiler) { $compiler->raw('['); $first = true; foreach ($this->nodes as $name => $node) { if (!$first) { $compiler->raw(','); } if (is_string($name)) { $compiler->string($name)->raw(' => '); } $node->compile($compiler); $first = false; } $compiler->raw(']'); }
public function compile(Compiler $compiler) { $env = $compiler->getEnvironment(); $functionName = $this->getAttribute(self::ATTR_NAME); /** @var Arguments $args */ $args = $this->getNode(self::NODE_ARGUMENTS); if ($env->hasFunction($functionName) && ($func = $env->getFunction($functionName)) instanceof NativeFunction) { $func->compile($compiler, $args); return; } if ($functionName == 'parent' && $compiler->isBlock()) { $compiler->raw('$this->parent->renderBlock(')->string($compiler->scopeName())->raw(' , $context )'); return; } if ($functionName == 'renderBlock') { if ($args->size() > 2) { throw new \LogicException(sprintf('Invalid renderBlock arguments count , %d , %d expected', $args->size(), 2)); } if ($args->size() < 1) { throw new \LogicException(sprintf('Invalid renderBlock, block name not defined')); } if ($args->size() == 2 && !$args->getNode(1) instanceof ArrayNode) { throw new \LogicException(sprintf('Invalid renderBlock, context must be array')); } $compiler->raw('$this->renderBlock(')->subcompile($args->getArgument(0))->raw(','); if ($args->size() == 2) { $compiler->raw('array_merge( $context , ')->subcompile($args->getArgument(1))->raw(')'); } else { $compiler->raw(' $context '); } if ($args) { $compiler->raw(')'); } return; } $compiler->raw('$this->getValue( $context , ')->raw('["' . implode('","', $this->convertToRoute($this->getAttribute(self::ATTR_NAME))) . '"]')->raw(' , self::METHOD_CALL , ')->subcompile($this->getNode(self::NODE_ARGUMENTS))->raw(')'); }
public function compile(Compiler $compiler) { $args = $this->getNode(self::NODE_ARGS)->getNodes(); $macroName = $this->getAttribute(self::ATTR_NAME); $compiler->writeln('// Line ' . $this->getLineNo())->write('public function macro_' . $macroName)->scopeIn(Compiler::SCOPE_MACRO, $macroName)->raw('( $context = [] , $arguments = [] ) {')->line()->indent()->writeln('$args = [')->indent(); foreach ($args as $name => $value) { $compiler->write('')->string($name)->raw(' => '); if ($value) { $compiler->subcompile($value); } else { $compiler->raw('null'); } $compiler->raw(',')->line(); } $compiler->outdent()->writeln('];')->writeln('$context = array_merge( $context , $this->prepareArgs( $args , $arguments ) );')->subcompile($this->getNode(self::NODE_BODY))->scopeOut()->outdent()->writeln('}'); }
public function compile(Compiler $compiler) { $compiler->raw('array('); $elements = $this->getNode(self::NODE_ELEMENTS); $first = true; foreach ($elements as $element) { if (!$first) { $compiler->raw(','); } list($key, $value) = $element; if ($key) { if ($key instanceof Constant) { $compiler->subcompile($key); } elseif ($key instanceof Name) { $compiler->string($key->getName()); } $compiler->raw(' => '); } $compiler->subcompile($value); $first = false; } $compiler->raw(')'); }
public function compile(Compiler $compiler) { /** @var Node[] $tests */ $tests = $this->getNode(self::NODE_TESTS); for ($i = 0; $i < count($tests); $i += 2) { $test = $tests[$i]; $true = $tests[$i + 1]; if ($i == 0) { $compiler->write('if ( '); } else { $compiler->raw(' elseif ( '); } $compiler->subcompile($test)->raw(' ) { ' . "\n")->indent()->subcompile($true)->outdent()->write('}'); } if ($this->hasNode(self::NODE_ELSE)) { $compiler->raw(' else { ' . "\n")->indent()->subcompile($this->getNode(self::NODE_ELSE))->outdent()->write('}'); } $compiler->line(); }
public function compile(Compiler $compiler) { $compiler->raw('range(')->subcompile($this->getNode(self::NODE_1))->raw(', ')->subcompile($this->getNode(self::NODE_2))->raw(')'); }
public function compile(Compiler $compiler) { $key = $this->hasAttribute(self::ATTR_KEY) ? $this->getAttribute(self::ATTR_KEY) : 'key'; $value = $this->getAttribute(self::ATTR_VALUE); $compiler->writeln('$context["_parent"] = $context;')->writeln('$context["loop"] = [')->indent()->write('"items" => $_seq = $this->ensureTraversable(')->subcompile($this->getNode(self::NODE_REPO))->raw("),\n")->writeln('"index" => 0,')->writeln('"count" => count( $_seq ),')->outdent()->writeln('];')->write('foreach ( $context["loop"]["items"] as ')->raw("\${$key}")->raw(' => ')->raw("\${$value}")->raw(" ) { \n")->indent()->writeln(sprintf('$context["%s"] = $%s;', $key, $key))->writeln(sprintf('$context["%s"] = $%s;', $value, $value))->subcompile($this->getNode(self::NODE_BODY))->writeln('$context["loop"]["index"]++;')->outdent()->writeln('}')->writeln('$context = $context["_parent"];'); }
private function renderParentFunction(Compiler $compiler) { if ($this->hasParent()) { $parent = $this->getNode(self::NODE_PARENT); $compiler->writeln('// Line ' . $parent->getLineNo())->writeln('protected function getParent() {')->indent()->write('return ')->subcompile($parent)->raw(';')->line()->outdent()->writeln('}')->line(); } }
public function compile(Compiler $compiler) { $name = $this->getAttribute(self::ATTR_NAME); $route = $this->convertToRoute($name); $compiler->raw('$this->getValue( $context , ["' . implode('","', $route) . '"] )'); }
public function compile(Compiler $compiler) { $compiler->raw($this->getAttribute('constant')); }
public function compile(Compiler $compiler) { $compiler->raw(sprintf('$this->renderBlock("%s", ', $this->getAttribute(self::ATTR_NAME)))->raw('array_merge( $context , ')->subcompile($this->getNode(self::NODE_CONTEXT))->raw('))'); }
public function compile(Compiler $compiler) { $compiler->write('echo ')->string($this->getAttribute('text'))->raw(";\n"); }
public function compile(Compiler $compiler) { $compiler->write('echo '); $this->getNode(self::NODE_EXPRESSION)->compile($compiler); $compiler->raw(';')->line(); }
/** * @param Compiler $compiler */ public function compile(Compiler $compiler) { $compiler->writeln('/*')->writeln($this->getAttribute(self::ATTR_COMMENT))->writeln('*/'); }
public function compileSource($source, $fileName) { $lexer = new Lexer(new Reader($source, $fileName), $this); $parser = new Parser($lexer->tokenize()->getStream(), $this); $compiler = new Compiler($this); $compiled = $compiler->compile($parser->parse()); return $compiled; }
function compile(Compiler $compiler) { $compiler->writeln('$_sandbox = $context;'); $compiler->subcompile($this->body); $compiler->writeln('$context = $_sandbox;'); }
public function compile(Compiler $compiler) { $blockName = $this->getAttribute(self::ATTR_NAME); $compiler->writeln('// Line ' . $this->getLineNo())->writeln(sprintf('public function %s( array $context = array() ) {', $this->getMethodName()))->indent()->scopeIn(Compiler::SCOPE_BLOCK, $blockName)->writeln('ob_start();')->subcompile($this->getNode(self::NODE_BODY))->writeln('return ob_get_clean();')->scopeOut()->outdent()->writeln('}'); }