コード例 #1
0
ファイル: Filter.php プロジェクト: mdzzohrabi/azera-fry
 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(')');
 }
コード例 #2
0
ファイル: FryTest.php プロジェクト: mdzzohrabi/azera-fry
 /**
  * @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));
 }
コード例 #3
0
ファイル: SetNode.php プロジェクト: mdzzohrabi/azera-fry
 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");
 }
コード例 #4
0
ファイル: Arguments.php プロジェクト: mdzzohrabi/azera-fry
 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(']');
 }
コード例 #5
0
ファイル: Call.php プロジェクト: mdzzohrabi/azera-fry
 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(')');
 }
コード例 #6
0
ファイル: Macro.php プロジェクト: mdzzohrabi/azera-fry
 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('}');
 }
コード例 #7
0
ファイル: ArrayNode.php プロジェクト: mdzzohrabi/azera-fry
 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(')');
 }
コード例 #8
0
ファイル: IfSection.php プロジェクト: mdzzohrabi/azera-fry
 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();
 }
コード例 #9
0
ファイル: Range.php プロジェクト: mdzzohrabi/azera-fry
 public function compile(Compiler $compiler)
 {
     $compiler->raw('range(')->subcompile($this->getNode(self::NODE_1))->raw(', ')->subcompile($this->getNode(self::NODE_2))->raw(')');
 }
コード例 #10
0
ファイル: LoopNode.php プロジェクト: mdzzohrabi/azera-fry
 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"];');
 }
コード例 #11
0
ファイル: Template.php プロジェクト: mdzzohrabi/azera-fry
 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();
     }
 }
コード例 #12
0
ファイル: Name.php プロジェクト: mdzzohrabi/azera-fry
 public function compile(Compiler $compiler)
 {
     $name = $this->getAttribute(self::ATTR_NAME);
     $route = $this->convertToRoute($name);
     $compiler->raw('$this->getValue( $context , ["' . implode('","', $route) . '"] )');
 }
コード例 #13
0
ファイル: Constant.php プロジェクト: mdzzohrabi/azera-fry
 public function compile(Compiler $compiler)
 {
     $compiler->raw($this->getAttribute('constant'));
 }
コード例 #14
0
ファイル: RenderBlock.php プロジェクト: mdzzohrabi/azera-fry
 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('))');
 }
コード例 #15
0
ファイル: Text.php プロジェクト: mdzzohrabi/azera-fry
 public function compile(Compiler $compiler)
 {
     $compiler->write('echo ')->string($this->getAttribute('text'))->raw(";\n");
 }
コード例 #16
0
ファイル: PrintNode.php プロジェクト: mdzzohrabi/azera-fry
 public function compile(Compiler $compiler)
 {
     $compiler->write('echo ');
     $this->getNode(self::NODE_EXPRESSION)->compile($compiler);
     $compiler->raw(';')->line();
 }
コード例 #17
0
ファイル: CommentNode.php プロジェクト: mdzzohrabi/azera-fry
 /**
  * @param Compiler $compiler
  */
 public function compile(Compiler $compiler)
 {
     $compiler->writeln('/*')->writeln($this->getAttribute(self::ATTR_COMMENT))->writeln('*/');
 }
コード例 #18
0
ファイル: Environment.php プロジェクト: mdzzohrabi/azera-fry
 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;
 }
コード例 #19
0
ファイル: SandboxNode.php プロジェクト: mdzzohrabi/azera-fry
 function compile(Compiler $compiler)
 {
     $compiler->writeln('$_sandbox = $context;');
     $compiler->subcompile($this->body);
     $compiler->writeln('$context = $_sandbox;');
 }
コード例 #20
0
ファイル: Block.php プロジェクト: mdzzohrabi/azera-fry
 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('}');
 }