コード例 #1
0
ファイル: For.php プロジェクト: n0way/Twig
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write("\$context['_parent'] = (array) \$context;\n")->write("\$context['_seq'] = twig_ensure_traversable(")->subcompile($this->getNode('seq'))->raw(");\n");
     if (null !== $this->getNode('else')) {
         $compiler->write("\$context['_iterated'] = false;\n");
     }
     if ($this->getAttribute('with_loop')) {
         $compiler->write("\$context['loop'] = array(\n")->write("  'parent' => \$context['_parent'],\n")->write("  'index0' => 0,\n")->write("  'index'  => 1,\n")->write("  'first'  => true,\n")->write(");\n")->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {\n")->indent()->write("\$length = count(\$context['_seq']);\n")->write("\$context['loop']['revindex0'] = \$length - 1;\n")->write("\$context['loop']['revindex'] = \$length;\n")->write("\$context['loop']['length'] = \$length;\n")->write("\$context['loop']['last'] = 1 === \$length;\n")->outdent()->write("}\n");
     }
     $compiler->write("foreach (\$context['_seq'] as ")->subcompile($this->getNode('key_target'))->raw(" => ")->subcompile($this->getNode('value_target'))->raw(") {\n")->indent();
     if (null !== $this->getNode('ifexpr')) {
         $compiler->write("if (")->subcompile($this->getNode('ifexpr'))->raw(") {\n")->indent();
     }
     $compiler->subcompile($this->getNode('body'));
     if (null !== $this->getNode('else')) {
         $compiler->write("\$context['_iterated'] = true;\n");
     }
     if ($this->getAttribute('with_loop')) {
         $compiler->write("++\$context['loop']['index0'];\n")->write("++\$context['loop']['index'];\n")->write("\$context['loop']['first'] = false;\n")->write("if (isset(\$context['loop']['length'])) {\n")->indent()->write("--\$context['loop']['revindex0'];\n")->write("--\$context['loop']['revindex'];\n")->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n")->outdent()->write("}\n");
     }
     if (null !== $this->getNode('ifexpr')) {
         $compiler->outdent()->write("}\n");
     }
     $compiler->outdent()->write("}\n");
     if (null !== $this->getNode('else')) {
         $compiler->write("if (!\$context['_iterated']) {\n")->indent()->subcompile($this->getNode('else'))->outdent()->write("}\n");
     }
     $compiler->write("\$_parent = \$context['_parent'];\n");
     // remove some "private" loop variables (needed for nested loops)
     $compiler->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\'' . $this->getNode('key_target')->getAttribute('name') . '\'], $context[\'' . $this->getNode('value_target')->getAttribute('name') . '\'], $context[\'_parent\'], $context[\'loop\']);' . "\n");
     // keep the values set in the inner context for variables defined in the outer context
     $compiler->write("\$context = array_merge(\$_parent, array_intersect_key(\$context, \$_parent));\n");
 }
コード例 #2
0
ファイル: ToggleNode.php プロジェクト: smithandre/toggler
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler $compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write('if (')->raw('toggle(')->subcompile($this->getNode('feature'))->raw(')')->raw(") {\n")->indent()->subcompile($this->getNode('body'));
     if ($this->hasNode('else') && null !== $this->getNode('else')) {
         $compiler->outdent()->write("} else {\n")->indent()->subcompile($this->getNode('else'));
     }
     $compiler->outdent()->write("}\n");
 }
コード例 #3
0
ファイル: TwigNodeTry.php プロジェクト: JozefAB/neoacu
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler $compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write('try {');
     $compiler->indent()->subcompile($this->getNode('try'));
     if ($this->hasNode('catch') && null !== $this->getNode('catch')) {
         $compiler->outdent()->write('} catch (\\Exception $e) {' . "\n")->indent()->write('if ($context[\'gantry\']->debug()) throw $e;' . "\n")->write('$context[\'e\'] = $e;' . "\n")->subcompile($this->getNode('catch'));
     }
     $compiler->outdent()->write("}\n");
 }
コード例 #4
0
ファイル: Pagination.php プロジェクト: carew/carew
 private function compileMaxPerPage(\Twig_Compiler $compiler)
 {
     $compiler->write("public function getMaxesPerPage()\n", "{\n")->indent();
     $compiler->write("return array(\n");
     $compiler->indent();
     foreach ($this->getAttribute('maxesPerPage') as $maxPerPage) {
         $compiler->write($maxPerPage . ",\n");
     }
     $compiler->outdent();
     $compiler->write(");\n");
     $compiler->outdent()->write("}\n\n");
 }
コード例 #5
0
ファイル: Lambda.php プロジェクト: dpolac/twig-lambda
 protected function compileWithArguments(\Twig_Compiler $compiler, $expressionNode, array $arguments)
 {
     $compiler->raw("\n");
     $compiler->indent();
     $compiler->addIndentation();
     $compiler->raw("function() use(&\$context) {\n");
     $compiler->indent();
     // copy of arguments and __ from context
     foreach ($arguments as $arg) {
         $compiler->addIndentation();
         $compiler->raw("if (isset(\$context['{$arg}'])) \$outer{$arg} = \$context['{$arg}'];\n");
     }
     $compiler->addIndentation();
     $compiler->raw("if (isset(\$context['__'])) \$outer__ = \$context['__'];\n");
     // adding closure's arguments to context
     $compiler->addIndentation();
     $compiler->raw("\$context['__'] = func_get_args();\n");
     foreach ($arguments as $i => $arg) {
         $compiler->addIndentation();
         $compiler->raw("if (func_num_args()>{$i}) \$context['{$arg}'] = func_get_arg({$i});\n");
         $compiler->addIndentation();
         $compiler->raw("else unset(\$context['{$arg}']);\n");
     }
     // getting call result
     $compiler->addIndentation();
     $compiler->raw("\$result = ");
     $compiler->subcompile($this->getNode($expressionNode));
     $compiler->raw(";\n");
     // recreating original context
     foreach ($arguments as $arg) {
         $compiler->addIndentation();
         $compiler->raw("if (isset(\$outer{$arg})) \$context['{$arg}'] = \$outer{$arg} ;\n");
         $compiler->addIndentation();
         $compiler->raw("else unset(\$context['{$arg}']);\n");
     }
     $compiler->addIndentation();
     $compiler->raw("if (isset(\$outer__)) \$context['__'] = \$outer__ ;\n");
     $compiler->addIndentation();
     $compiler->raw("else unset(\$context['__']);\n");
     // return statement
     $compiler->addIndentation();
     $compiler->raw("return \$result;\n");
     $compiler->outdent();
     $compiler->addIndentation();
     $compiler->raw("}\n");
     $compiler->outdent();
     $compiler->addIndentation();
 }
コード例 #6
0
ファイル: Include.php プロジェクト: 3dw1np/wf3
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     if ($this->getAttribute('ignore_missing')) {
         $compiler->write("try {\n")->indent();
     }
     if ($this->getNode('expr') instanceof Twig_Node_Expression_Constant) {
         $compiler->write("\$this->env->loadTemplate(")->subcompile($this->getNode('expr'))->raw(")->display(");
     } else {
         $compiler->write("\$template = \$this->env->resolveTemplate(")->subcompile($this->getNode('expr'))->raw(");\n")->write('$template->display(');
     }
     if (false === $this->getAttribute('only')) {
         if (null === $this->getNode('variables')) {
             $compiler->raw('$context');
         } else {
             $compiler->raw('array_merge($context, ')->subcompile($this->getNode('variables'))->raw(')');
         }
     } else {
         if (null === $this->getNode('variables')) {
             $compiler->raw('array()');
         } else {
             $compiler->subcompile($this->getNode('variables'));
         }
     }
     $compiler->raw(");\n");
     if ($this->getAttribute('ignore_missing')) {
         $compiler->outdent()->write("} catch (Twig_Error_Loader \$e) {\n")->indent()->write("// ignore missing template\n")->outdent()->write("}\n\n");
     }
 }
コード例 #7
0
ファイル: If.php プロジェクト: jesusmarket/jesusmarket
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     for ($i = 0, $count = count($this->getNode('tests')); $i < $count; $i += 2) {
         if ($i > 0) {
             $compiler->outdent()->write("} elseif (");
         } else {
             $compiler->write('if (');
         }
         $compiler->subcompile($this->getNode('tests')->getNode($i))->raw(") {\n")->indent()->subcompile($this->getNode('tests')->getNode($i + 1));
     }
     if ($this->hasNode('else') && null !== $this->getNode('else')) {
         $compiler->outdent()->write("} else {\n")->indent()->subcompile($this->getNode('else'));
     }
     $compiler->outdent()->write("}\n");
 }
コード例 #8
0
ファイル: Macro.php プロジェクト: dunglas/Twig
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write(sprintf('public function macro_%s(', $this->getAttribute('name')));
     $count = count($this->getNode('arguments'));
     $pos = 0;
     foreach ($this->getNode('arguments') as $name => $default) {
         $compiler->raw('$__' . $name . '__ = ')->subcompile($default);
         if (++$pos < $count) {
             $compiler->raw(', ');
         }
     }
     if (PHP_VERSION_ID >= 50600) {
         if ($count) {
             $compiler->raw(', ');
         }
         $compiler->raw('...$__varargs__');
     }
     $compiler->raw(")\n")->write("{\n")->indent();
     $compiler->write("\$context = \$this->env->mergeGlobals(array(\n")->indent();
     foreach ($this->getNode('arguments') as $name => $default) {
         $compiler->addIndentation()->string($name)->raw(' => $__' . $name . '__')->raw(",\n");
     }
     $compiler->addIndentation()->string(self::VARARGS_NAME)->raw(' => ');
     if (PHP_VERSION_ID >= 50600) {
         $compiler->raw("\$__varargs__,\n");
     } else {
         $compiler->raw('func_num_args() > ')->repr($count)->raw(' ? array_slice(func_get_args(), ')->repr($count)->raw(") : array(),\n");
     }
     $compiler->outdent()->write("));\n\n")->write("\$blocks = array();\n\n")->write("ob_start();\n")->write("try {\n")->indent()->subcompile($this->getNode('body'))->raw("\n")->write("return ('' === \$tmp = ob_get_contents()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset());\n")->outdent()->write("} finally {\n")->indent()->write("ob_end_clean();\n")->outdent()->write("}\n")->outdent()->write("}\n\n");
 }
コード例 #9
0
ファイル: Macro.php プロジェクト: nizsheanez/PolymorphCMS
    /**
     * Compiles the node to PHP.
     *
     * @param Twig_Compiler A Twig_Compiler instance
     */
    public function compile(Twig_Compiler $compiler)
    {
        $arguments = array();
        foreach ($this->getNode('arguments') as $argument) {
            $arguments[] = '$'.$argument->getAttribute('name').' = null';
        }

        $compiler
            ->addDebugInfo($this)
            ->write(sprintf("public function get%s(%s)\n", $this->getAttribute('name'), implode(', ', $arguments)), "{\n")
            ->indent()
            ->write("\$context = array_merge(\$this->env->getGlobals(), array(\n")
            ->indent()
        ;

        foreach ($this->getNode('arguments') as $argument) {
            $compiler
                ->write('')
                ->string($argument->getAttribute('name'))
                ->raw(' => $'.$argument->getAttribute('name'))
                ->raw(",\n")
            ;
        }

        $compiler
            ->outdent()
            ->write("));\n\n")
            ->write("ob_start();\n")
            ->subcompile($this->getNode('body'))
            ->raw("\n")
            ->write("return ob_get_clean();\n")
            ->outdent()
            ->write("}\n\n")
        ;
    }
コード例 #10
0
ファイル: tags.php プロジェクト: newsapps/wordpress-mtv
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write('while (');
     for ($i = 0; $i < count($this->getNode('tests')); $i += 2) {
         $compiler->subcompile($this->getNode('tests')->getNode($i))->raw(") {\n")->indent()->subcompile($this->getNode('tests')->getNode($i + 1));
     }
     $compiler->outdent()->write("}\n");
 }
コード例 #11
0
ファイル: Switch_Node.php プロジェクト: jmstan/craft-website
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler $compiler
  *
  * @return null
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write("switch (")->subcompile($this->getNode('value'))->raw(") {\n")->indent();
     foreach ($this->_cases as $case) {
         $compiler->write('case ')->subcompile($case['expr'])->raw(":\n")->write("{\n")->indent()->subcompile($case['body'])->write("break;\n")->outdent()->write("}\n");
     }
     if ($this->hasNode('default') && $this->getNode('default') !== null) {
         $compiler->write("default:\n")->write("{\n")->indent()->subcompile($this->getNode('default'))->outdent()->write("}\n");
     }
     $compiler->outdent()->write("}\n");
 }
コード例 #12
0
ファイル: SwitchNode.php プロジェクト: Silwereth/core
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write('switch (')->subcompile($this->getNode('expression'))->raw(") {\n")->indent();
     /* @var $case \Twig_Node */
     foreach ($this->getNode('cases')->getIterator() as $key => $case) {
         $compiler->write('case ')->subcompile($case->getNode('expression'))->raw(":\n");
         if ($case->hasNode('body')) {
             $compiler->indent()->subcompile($case->getNode('body'));
         }
         if ($case->hasAttribute('break') && $case->getAttribute('break') == true) {
             $compiler->write("break;\n");
         }
         $compiler->outdent();
     }
     if ($this->hasNode('default') && $this->getNode('default') !== null) {
         $compiler->write('default')->raw(":\n")->indent()->subcompile($this->getNode('default'))->outdent();
     }
     $compiler->outdent()->write("}\n");
 }
コード例 #13
0
ファイル: WhileNode.php プロジェクト: free2er/twig-extensions
 /**
  * Собирает PHP-выражение
  *
  * @param TwigCompiler $compiler Компилятор Twig.
  *
  * @return void
  */
 public function compile(TwigCompiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write('while (');
     $compiler->subcompile($this->getNode('condition'));
     $compiler->write(") {\n");
     $compiler->indent();
     $compiler->subcompile($this->getNode('body'));
     $compiler->outdent();
     $compiler->write("}\n");
 }
コード例 #14
0
ファイル: Debug.php プロジェクト: rafaelgou/PHPBol
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write("if (\$this->env->isDebug()) {\n")->indent();
     if (null === $this->getNode('expr')) {
         // remove embedded templates (macros) from the context
         $compiler->write("echo '<div class=\"twig_debug\">';\n")->write("echo '<h6>DATA DEBUG</h6>';\n")->write("foreach (\$context as \$key => \$data) {\n")->write("if (!\$data instanceof Twig_Template) {\n")->write("echo '<table><caption>'.\$key.'</caption>';\n")->write("if (is_array(\$data)) {\n")->write("foreach (\$data as \$datakey => \$datavalue) {\n")->write("if     (is_array(\$datavalue)) { \$datavalue = print_r(\$datavalue,true); }\n")->write("elseif (is_bool(\$datavalue)) { \$datavalue ? 'True' : 'False'; }\n")->write("elseif (\$datavalue instanceof DateTime) { \$datavalue = \$datavalue->format('Y-d-m H:is'); }\n")->write("echo '<tr><th>'.\$datakey.'</th><td>'.\$datavalue.'</td></tr>';\n")->write("}\n")->write("} else {\n")->write("if     (is_array(\$data)) { \$data = print_r(\$data,true); }\n")->write("elseif (is_bool(\$data)) { \$data ? 'True' : 'False'; }\n")->write("elseif (\$data instanceof DateTime) { \$data = \$data->format('Y-d-m H:is'); }\n")->write("echo '<tr><td>'.\$data.'</td></tr>';\n")->write("}\n")->write("echo '</table>';\n")->write("}\n")->write("}\n")->write("echo '</div>';\n");
     } else {
         $compiler->write("var_dump(")->subcompile($this->getNode('expr'))->raw(");\n");
     }
     $compiler->outdent()->write("}\n");
 }
コード例 #15
0
ファイル: Debug.php プロジェクト: D4rk4/Kusaba-z
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write("if (\$this->env->isDebug()) {\n")->indent();
     if (null === $this->getNode('expr')) {
         // remove embedded templates (macros) from the context
         $compiler->write("\$vars = array();\n")->write("foreach (\$context as \$key => \$value) {\n")->indent()->write("if (!\$value instanceof Twig_Template) {\n")->indent()->write("\$vars[\$key] = \$value;\n")->outdent()->write("}\n")->outdent()->write("}\n")->write("print_r(\$vars);\n");
     } else {
         $compiler->write("print_r(")->subcompile($this->getNode('expr'))->raw(");\n");
     }
     $compiler->outdent()->write("}\n");
 }
コード例 #16
0
ファイル: DumpNode.php プロジェクト: ayoah/symfony
 /**
  * {@inheritdoc}
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->write("if (\$this->env->isDebug()) {\n")->indent();
     if (!$this->hasNode('values')) {
         // remove embedded templates (macros) from the context
         $compiler->write(sprintf('$%svars = array();' . "\n", $this->varPrefix))->write(sprintf('foreach ($context as $%1$skey => $%1$sval) {' . "\n", $this->varPrefix))->indent()->write(sprintf('if (!$%sval instanceof \\Twig_Template) {' . "\n", $this->varPrefix))->indent()->write(sprintf('$%1$svars[$%1$skey] = $%1$sval;' . "\n", $this->varPrefix))->outdent()->write("}\n")->outdent()->write("}\n")->addDebugInfo($this)->write(sprintf('\\Symfony\\Component\\VarDumper\\VarDumper::dump($%svars);' . "\n", $this->varPrefix));
     } elseif (($values = $this->getNode('values')) && 1 === $values->count()) {
         $compiler->addDebugInfo($this)->write('\\Symfony\\Component\\VarDumper\\VarDumper::dump(')->subcompile($values->getNode(0))->raw(");\n");
     } else {
         $compiler->addDebugInfo($this)->write('\\Symfony\\Component\\VarDumper\\VarDumper::dump(array(' . "\n")->indent();
         foreach ($values as $node) {
             $compiler->addIndentation();
             if ($node->hasAttribute('name')) {
                 $compiler->string($node->getAttribute('name'))->raw(' => ');
             }
             $compiler->subcompile($node)->raw(",\n");
         }
         $compiler->outdent()->write("));\n");
     }
     $compiler->outdent()->write("}\n");
 }
コード例 #17
0
ファイル: SwitchNode.php プロジェクト: bolt/bolt
 /**
  * {@inheritdoc}
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write('switch (')->subcompile($this->getNode('value'))->raw(") {\n")->indent();
     for ($i = 0; $i < count($this->getNode('cases')); $i += 2) {
         $compiler->write('case ')->subcompile($this->getNode('cases')->getNode($i))->raw(":\n")->indent()->subcompile($this->getNode('cases')->getNode($i + 1))->write('')->raw("break;\n");
     }
     if ($this->hasNode('default') && null !== $this->getNode('default')) {
         $compiler->write("default:\n")->indent()->subcompile($this->getNode('default'))->write('')->raw("break;\n");
     }
     $compiler->outdent()->write("}\n");
 }
コード例 #18
0
ファイル: TreeNode.php プロジェクト: ninsuo/jordan-tree
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     // $tree_treeA = function($data, $level = 0) use (&$context, &$tree_treeA) {
     $compiler->write("\$tree_")->raw($this->getAttribute('as'))->raw(" = ")->raw("function(\$data, \$level = 0) use (&\$context, &\$tree_")->raw($this->getAttribute('as'))->raw(") {\n")->indent();
     // backuping local scope context
     $compiler->write("\$context['_parent'][\$level] = \$context;\n")->write("\$context['_seq'] = twig_ensure_traversable(\$data);\n");
     // initializing treeloop variable
     $compiler->write("\$context['treeloop'] = array(\n")->write("  'parent' => \$context['_parent'][\$level],\n")->write("  'index0' => 0,\n")->write("  'index'  => 1,\n")->write("  'first'  => true,\n")->write(");\n")->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {\n")->indent()->write("\$length = count(\$context['_seq']);\n")->write("\$context['treeloop']['revindex0'] = \$length - 1;\n")->write("\$context['treeloop']['revindex'] = \$length;\n")->write("\$context['treeloop']['length'] = \$length;\n")->write("\$context['treeloop']['last'] = 1 === \$length;\n")->outdent()->write("}\n");
     // starting loop
     $compiler->write("foreach (\$context['_seq'] as ")->subcompile($this->getNode('key_target'))->raw(' => ')->subcompile($this->getNode('value_target'))->raw(") {\n")->indent();
     // tag's body
     foreach ($this->getAttribute('data') as $data) {
         switch ($data['type']) {
             // case #1: a simple Twig_Node_Body
             case 'body':
                 $compiler->subcompile($data['node']);
                 break;
                 // case #2: recursive call to $tree_treeA
             // case #2: recursive call to $tree_treeA
             case 'subtree':
                 $compiler->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Traversable)) {\n")->indent()->write("\$tree_")->raw($data['with'])->raw("(")->subcompile($data['child'])->raw(", \$level + 1);\n")->outdent()->write("}");
                 break;
             default:
                 break;
         }
     }
     // updating treeloop context
     $compiler->write("++\$context['treeloop']['index0'];\n")->write("++\$context['treeloop']['index'];\n")->write("\$context['treeloop']['first'] = false;\n")->write("if (isset(\$context['treeloop']['length'])) {\n")->indent()->write("--\$context['treeloop']['revindex0'];\n")->write("--\$context['treeloop']['revindex'];\n")->write("\$context['treeloop']['last'] = 0 === \$context['treeloop']['revindex0'];\n")->outdent()->write("}\n");
     // ending loop
     $compiler->outdent()->write("}\n");
     // recovering local scope context and cleaning up
     $compiler->write("\$_parent = \$context['_parent'][\$level];\n")->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\'' . $this->getNode('key_target')->getAttribute('name') . '\'], $context[\'' . $this->getNode('value_target')->getAttribute('name') . '\'], $context[\'_parent\'][$level], $context[\'treeloop\']);' . "\n")->write("\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n");
     // };
     // $tree_treeA($context["items"]);
     $compiler->outdent()->write("};\n")->write("\$tree_")->raw($this->getAttribute('as'))->raw("(")->subcompile($this->getNode('seq'))->raw(");\n")->write("unset(\$context['_parent']);\n");
 }
コード例 #19
0
ファイル: Switch.php プロジェクト: davyrolink/TFD7
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write('switch(')->subcompile($this->getNode('expression'))->raw(") {\n")->indent();
     $total = count($this->getNode('cases'));
     for ($i = 0; $i < $total; $i++) {
         $expr = $this->getNode('cases')->getNode($i)->getAttribute('expression');
         $body = $this->getNode('cases')->getNode($i)->getNode('body');
         if (is_null($expr)) {
             $compiler->write('default')->raw(":\n");
         } else {
             foreach ($expr as $subExpr) {
                 $compiler->write('case ')->subcompile($subExpr)->raw(":\n");
             }
         }
         $compiler->indent();
         $compiler->subcompile($body);
         if ($i + 1 >= $total || !$this->getNode('cases')->getNode($i + 1)->getAttribute('fallthrough')) {
             $compiler->write("break;\n");
         }
         $compiler->outdent();
     }
     $compiler->outdent()->write('}');
 }
コード例 #20
0
ファイル: Switch_Node.php プロジェクト: amite/arc-va
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler $compiler
  *
  * @return null
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write("switch (")->subcompile($this->getNode('value'))->raw(") {\n")->indent();
     foreach ($this->getNode('cases') as $case) {
         // The 'body' node may have been removed by Twig if it was an empty text node in a sub-template,
         // outside of any blocks
         if (!$case->hasNode('body')) {
             continue;
         }
         $compiler->write('case ')->subcompile($case->getNode('expr'))->raw(":\n")->write("{\n")->indent()->subcompile($case->getNode('body'))->write("break;\n")->outdent()->write("}\n");
     }
     if ($this->hasNode('default') && $this->getNode('default') !== null) {
         $compiler->write("default:\n")->write("{\n")->indent()->subcompile($this->getNode('default'))->outdent()->write("}\n");
     }
     $compiler->outdent()->write("}\n");
 }
コード例 #21
0
ファイル: Node.php プロジェクト: enyosolutions/AutoAB
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write("{\n")->indent();
     // Generate the variants
     $compiler->write("\$ab_variants = [\n");
     $compiler->indent();
     foreach ($this->getNode('variants') as $k => $v) {
         $compiler->write("[\n");
         $compiler->indent()->write("'name' => '{$k}',\n");
         $compiler->write("'value' => function(){\n");
         $compiler->subcompile($v);
         $compiler->write("}\n");
         $compiler->outdent()->write("],\n");
     }
     $compiler->outdent()->write("];\n\n");
     $compiler->write("\$ab_selected_test = null;\n");
     // Generate the selector method
     $compiler->write("if (isset(\$_GET['__ab_" . $this->test_name . "'])) {\n");
     $compiler->indent()->write("foreach (\$ab_variants as \$ab_variant_iteration) {\n");
     $compiler->indent()->write("if (\$ab_variant_iteration['name'] === \$_GET['__ab_" . $this->test_name . "']) {\n");
     $compiler->indent()->write("\$ab_selected_test = \$ab_variant_iteration;\n");
     $compiler->write("break;\n");
     $compiler->outdent()->write("}\n");
     $compiler->outdent()->write("}\n");
     $compiler->outdent()->write("}\n\n");
     $compiler->write("if (!isset(\$ab_selected_test)) {\n");
     $compiler->write("\$ip = isset(\$_SERVER['HTTP_X_REAL_IP']) ? \$_SERVER['HTTP_X_REAL_IP'] : \$_SERVER['REMOTE_ADDR'];");
     $compiler->indent()->write("\$ab_random_seed = \$ip . count(\$ab_variants) . '" . $this->test_name . "';\n");
     $compiler->write("\$ab_hash_seed = substr(md5(\$ab_random_seed), 0, 6);\n");
     $compiler->write("\$ab_int_seed = intval(hexdec(\$ab_hash_seed));\n");
     $compiler->write("mt_srand(\$ab_int_seed);\n");
     $compiler->write("\$ab_selected_test_id = mt_rand(0, count(\$ab_variants) - 1);\n");
     $compiler->write("\$ab_selected_test = \$ab_variants[\$ab_selected_test_id];\n");
     $compiler->outdent()->write("}\n\n");
     // Set the global list of enrolled tests
     $compiler->write("global \$ab_enrolled_tests;\n");
     $compiler->write("if (!isset(\$ab_enrolled_tests)) {\n");
     $compiler->indent()->write("\$ab_enrolled_tests = [];\n");
     $compiler->outdent()->write("}\n");
     $compiler->write("\$ab_enrolled_tests['" . $this->test_name . "'] = \$ab_selected_test['name'];\n\n");
     // Execute the variant
     $compiler->write("\$ab_selected_test['value']();\n");
     $compiler->outdent()->write("}");
 }
コード例 #22
0
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $config = $this->environment->get_phpbb_config();
     if (!$config['tpl_allow_php']) {
         $compiler->write("// INCLUDEPHP Disabled\n");
         return;
     }
     if ($this->getAttribute('ignore_missing')) {
         $compiler->write("try {\n")->indent();
     }
     $compiler->write("\$location = ")->subcompile($this->getNode('expr'))->raw(";\n")->write("if (phpbb_is_absolute(\$location)) {\n")->indent()->write("require(\$location);\n")->outdent()->write("} else if (file_exists(\$this->getEnvironment()->get_phpbb_root_path() . \$location)) {\n")->indent()->write("require(\$this->getEnvironment()->get_phpbb_root_path() . \$location);\n")->outdent()->write("} else {\n")->indent()->write("require(\$this->getEnvironment()->getLoader()->getCacheKey(\$location));\n")->outdent()->write("}\n");
     if ($this->getAttribute('ignore_missing')) {
         $compiler->outdent()->write("} catch (\\Twig_Error_Loader \$e) {\n")->indent()->write("// ignore missing template\n")->outdent()->write("}\n\n");
     }
 }
コード例 #23
0
 /**
  * {@inheritdoc}
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     if ($this->getAttribute('ignoreErrors')) {
         $compiler->write('try {' . PHP_EOL)->indent();
     }
     $compiler->write('ob_start();', PHP_EOL)->subcompile($this->getNode('body'))->write('$content = ob_get_clean();', PHP_EOL)->write('$highlightEngine = $this->env->getTokenParsers()->getTokenParser(\'highlight\')->getEngine();', PHP_EOL)->write('echo $highlightEngine->getOutputFor($content, ')->subcompile($this->getNode('language'))->write(', ');
     if ($this->hasNode('variables')) {
         $compiler->subcompile($this->getNode('variables'));
     } else {
         $compiler->write('[]');
     }
     $compiler->write(');')->write(PHP_EOL);
     if ($this->getAttribute('ignoreErrors')) {
         $compiler->outdent()->write('} catch (\\Exception $e) {}' . PHP_EOL);
     }
 }
コード例 #24
0
ファイル: Macro.php プロジェクト: ceroberoz/kurs
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $arguments = array();
     foreach ($this->getNode('arguments') as $argument) {
         $arguments[] = '$' . $argument->getAttribute('name') . ' = null';
     }
     $compiler->addDebugInfo($this)->write(sprintf("public function get%s(%s)\n", $this->getAttribute('name'), implode(', ', $arguments)), "{\n")->indent();
     if (!count($this->getNode('arguments'))) {
         $compiler->write("\$context = \$this->env->getGlobals();\n\n");
     } else {
         $compiler->write("\$context = \$this->env->mergeGlobals(array(\n")->indent();
         foreach ($this->getNode('arguments') as $argument) {
             $compiler->write('')->string($argument->getAttribute('name'))->raw(' => $' . $argument->getAttribute('name'))->raw(",\n");
         }
         $compiler->outdent()->write("));\n\n");
     }
     $compiler->write("\$blocks = array();\n\n")->write("ob_start();\n")->write("try {\n")->indent()->subcompile($this->getNode('body'))->outdent()->write("} catch(Exception \$e) {\n")->indent()->write("ob_end_clean();\n\n")->write("throw \$e;\n")->outdent()->write("}\n\n")->write("return ob_get_clean();\n")->outdent()->write("}\n\n");
 }
コード例 #25
0
ファイル: event.php プロジェクト: Tarendai/spring-website
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $location = $this->listener_directory . $this->getNode('expr')->getAttribute('name');
     foreach ($this->environment->get_phpbb_extensions() as $ext_namespace => $ext_path) {
         $ext_namespace = str_replace('/', '_', $ext_namespace);
         if (defined('DEBUG')) {
             // If debug mode is enabled, lets check for new/removed EVENT
             //  templates on page load rather than at compile. This is
             //  slower, but makes developing extensions easier (no need to
             //  purge the cache when a new event template file is added)
             $compiler->write("if (\$this->env->getLoader()->exists('@{$ext_namespace}/{$location}.html')) {\n")->indent();
         }
         if (defined('DEBUG') || $this->environment->getLoader()->exists('@' . $ext_namespace . '/' . $location . '.html')) {
             $compiler->write("\$previous_look_up_order = \$this->env->getNamespaceLookUpOrder();\n")->write("\$this->env->setNamespaceLookUpOrder(array('{$ext_namespace}', '__main__'));\n")->write("\$this->env->loadTemplate('@{$ext_namespace}/{$location}.html')->display(\$context);\n")->write("\$this->env->setNamespaceLookUpOrder(\$previous_look_up_order);\n");
         }
         if (defined('DEBUG')) {
             $compiler->outdent()->write("}\n\n");
         }
     }
 }
コード例 #26
0
ファイル: Macro.php プロジェクト: DeDoOozZz/brighterycms
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler $compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write(sprintf("public function get%s(", $this->getAttribute('name')));
     $count = count($this->getNode('arguments'));
     $pos = 0;
     foreach ($this->getNode('arguments') as $name => $default) {
         $compiler->raw('$__' . $name . '__ = ')->subcompile($default);
         if (++$pos < $count) {
             $compiler->raw(', ');
         }
     }
     $compiler->raw(")\n")->write("{\n")->indent();
     if (!count($this->getNode('arguments'))) {
         $compiler->write("\$context = \$this->env->getGlobals();\n\n");
     } else {
         $compiler->write("\$context = \$this->env->mergeGlobals(array(\n")->indent();
         foreach ($this->getNode('arguments') as $name => $default) {
             $compiler->write('')->string($name)->raw(' => $__' . $name . '__')->raw(",\n");
         }
         $compiler->outdent()->write("));\n\n");
     }
     $compiler->write("\$blocks = array();\n\n")->write("ob_start();\n")->write("try {\n")->indent()->subcompile($this->getNode('body'))->outdent()->write("} catch (Exception \$e) {\n")->indent()->write("ob_end_clean();\n\n")->write("throw \$e;\n")->outdent()->write("}\n\n")->write("return ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset());\n")->outdent()->write("}\n\n");
 }
コード例 #27
0
 /**
  * @param \Twig_Compiler $compiler
  */
 protected function compileContentRegionFunction(\Twig_Compiler $compiler)
 {
     //  Prepare the functions head.
     $compiler->write(PHP_EOL, sprintf('/* Added by %s */', TwigExtension::class), PHP_EOL, sprintf('public function %s()', self::COMPILE_FUNCTION_NAME), PHP_EOL, '{')->indent()->write(PHP_EOL);
     //  For debug its nice to be able to understand the code in the IDE.
     //  These lines will add IDE specific tags and shortcuts for later on.
     $compiler->write('/** @var \\Twig_Environment $environment */', PHP_EOL, '$environment = $this->env;', PHP_EOL, '$regions = [];', PHP_EOL, PHP_EOL, '//  Handle local regions', PHP_EOL);
     //  Compile the local regions as region context.
     $regions = $this->getAttribute(ContentRegionNodeVisitor::CONTEXT_REGIONS);
     foreach ($regions as $region) {
         $compiler->write(sprintf('$regions["%2$s"] = new \\%1$s("%2$s", "%3$s", "%4$s");', RegionContext::class, $region[RegionNode::ATTRIBUTE_IDENTITY], $region[RegionNode::ATTRIBUTE_RENDERER], $region[RegionNode::ATTRIBUTE_LOADER]), PHP_EOL);
     }
     //  Break in code block.
     //        $compiler->write(
     //            PHP_EOL,
     //            '//  Handle region contexts within the template context.',
     //            PHP_EOL
     //        );
     //
     //        //  Compile the context regions as region contexts too.
     //        $compiler
     //            ->write(sprintf('if (isset($context["%s"])) {', ContentRegionNodeVisitor::CONTEXT_REGIONS), PHP_EOL)
     //            ->indent()
     //            ->write(sprintf('foreach ($context["%s"] as $region) {', ContentRegionNodeVisitor::CONTEXT_REGIONS), PHP_EOL)
     //            ->indent()
     //            ->write(
     //                sprintf(
     //                    '$regions[$region["%2$s"]] = new \%1$s($region["%2$s"]);',
     //                    RegionContext::class,
     //                    RegionNode::ATTRIBUTE_IDENTITY
     //                ),
     //                PHP_EOL
     //            )
     //            ->outdent()
     //            ->write('}', PHP_EOL) // end for
     //            ->outdent()
     //            ->write('}', PHP_EOL); // end if
     //  Break in code block.
     $compiler->write(PHP_EOL, '//  Handle region contexts across included templates.', PHP_EOL);
     //  Now merge in the region contexts from the template includes.
     $includes = $this->getAttribute(ContentRegionNodeVisitor::CONTEXT_INCLUDES);
     foreach ($includes as $include) {
         $compiler->write(PHP_EOL, sprintf('/** @var \\%s $template */', AbstractTwigTemplate::class), PHP_EOL, sprintf('$template = $environment->loadTemplate("%s");', $include), PHP_EOL, sprintf('foreach ($template->%s() as $regionContext) {', self::COMPILE_FUNCTION_NAME), PHP_EOL)->indent()->write(sprintf('/** @var \\%s $regionContext */', RegionContext::class), PHP_EOL, '$regions[$regionContext->getIdentity()] = $regionContext;', PHP_EOL)->outdent()->write('}', PHP_EOL);
     }
     //  Return the regions from the function.
     $compiler->write(PHP_EOL, 'return $regions;', PHP_EOL);
     //  Break in code block.
     $compiler->write(PHP_EOL);
     //  Close the body of the function.
     $compiler->outdent()->write('}', PHP_EOL);
 }
コード例 #28
0
ファイル: Module.php プロジェクト: jesusmarket/jesusmarket
 protected function compileClassFooter(Twig_Compiler $compiler)
 {
     $compiler->outdent()->write("}\n");
 }
コード例 #29
0
ファイル: Module.php プロジェクト: freeztime/ignitedcms-pro
 protected function compileConstructor(Twig_Compiler $compiler)
 {
     $compiler->write("public function __construct(Twig_Environment \$env)\n", "{\n")->indent()->subcompile($this->getNode('constructor_start'))->write("parent::__construct(\$env);\n\n");
     // parent
     if (null === ($parent = $this->getNode('parent'))) {
         $compiler->write("\$this->parent = false;\n\n");
     } elseif ($parent instanceof Twig_Node_Expression_Constant) {
         $compiler->addDebugInfo($parent)->write('$this->parent = $this->loadTemplate(')->subcompile($parent)->raw(', ')->repr($compiler->getFilename())->raw(', ')->repr($this->getNode('parent')->getLine())->raw(");\n");
     }
     $countTraits = count($this->getNode('traits'));
     if ($countTraits) {
         // traits
         foreach ($this->getNode('traits') as $i => $trait) {
             $node = $trait->getNode('template');
             $compiler->write(sprintf('$_trait_%s = $this->loadTemplate(', $i))->subcompile($node)->raw(', ')->repr($compiler->getFilename())->raw(', ')->repr($node->getLine())->raw(");\n");
             $compiler->addDebugInfo($trait->getNode('template'))->write(sprintf("if (!\$_trait_%s->isTraitable()) {\n", $i))->indent()->write("throw new Twig_Error_Runtime('Template \"'.")->subcompile($trait->getNode('template'))->raw(".'\" cannot be used as a trait.');\n")->outdent()->write("}\n")->write(sprintf("\$_trait_%s_blocks = \$_trait_%s->getBlocks();\n\n", $i, $i));
             foreach ($trait->getNode('targets') as $key => $value) {
                 $compiler->write(sprintf('if (!isset($_trait_%s_blocks[', $i))->string($key)->raw("])) {\n")->indent()->write("throw new Twig_Error_Runtime(sprintf('Block ")->string($key)->raw(' is not defined in trait ')->subcompile($trait->getNode('template'))->raw(".'));\n")->outdent()->write("}\n\n")->write(sprintf('$_trait_%s_blocks[', $i))->subcompile($value)->raw(sprintf('] = $_trait_%s_blocks[', $i))->string($key)->raw(sprintf(']; unset($_trait_%s_blocks[', $i))->string($key)->raw("]);\n\n");
             }
         }
         if ($countTraits > 1) {
             $compiler->write("\$this->traits = array_merge(\n")->indent();
             for ($i = 0; $i < $countTraits; ++$i) {
                 $compiler->write(sprintf('$_trait_%s_blocks' . ($i == $countTraits - 1 ? '' : ',') . "\n", $i));
             }
             $compiler->outdent()->write(");\n\n");
         } else {
             $compiler->write("\$this->traits = \$_trait_0_blocks;\n\n");
         }
         $compiler->write("\$this->blocks = array_merge(\n")->indent()->write("\$this->traits,\n")->write("array(\n");
     } else {
         $compiler->write("\$this->blocks = array(\n");
     }
     // blocks
     $compiler->indent();
     foreach ($this->getNode('blocks') as $name => $node) {
         $compiler->write(sprintf("'%s' => array(\$this, 'block_%s'),\n", $name, $name));
     }
     if ($countTraits) {
         $compiler->outdent()->write(")\n");
     }
     $compiler->outdent()->write(");\n")->outdent()->subcompile($this->getNode('constructor_end'))->write("}\n\n");
 }