コード例 #1
0
ファイル: For.php プロジェクト: jasmun/Noco100
 /**
  * Compiles the node to PHP.
  *
  * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance
  */
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write("\$context['_parent'] = (array) \$context;\n")->write("\$context['_seq'] = ifwpsn_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");
         if (!$this->getAttribute('ifexpr')) {
             $compiler->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");
         }
     }
     $this->loop->setAttribute('else', null !== $this->getNode('else'));
     $this->loop->setAttribute('with_loop', $this->getAttribute('with_loop'));
     $this->loop->setAttribute('ifexpr', $this->getAttribute('ifexpr'));
     $compiler->write("foreach (\$context['_seq'] as ")->subcompile($this->getNode('key_target'))->raw(" => ")->subcompile($this->getNode('value_target'))->raw(") {\n")->indent()->subcompile($this->getNode('body'))->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_intersect_key(\$context, \$_parent) + \$_parent;\n");
 }
コード例 #2
0
ファイル: Parent.php プロジェクト: jasmun/Noco100
 /**
  * Compiles the node to PHP.
  *
  * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance
  */
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     if ($this->getAttribute('output')) {
         $compiler->addDebugInfo($this)->write("\$this->displayParentBlock(")->string($this->getAttribute('name'))->raw(", \$context, \$blocks);\n");
     } else {
         $compiler->raw("\$this->renderParentBlock(")->string($this->getAttribute('name'))->raw(", \$context, \$blocks)");
     }
 }
コード例 #3
0
ファイル: Import.php プロジェクト: jasmun/Noco100
 /**
  * Compiles the node to PHP.
  *
  * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance
  */
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write('')->subcompile($this->getNode('var'))->raw(' = ');
     if ($this->getNode('expr') instanceof IfwPsn_Vendor_Twig_Node_Expression_Name && '_self' === $this->getNode('expr')->getAttribute('name')) {
         $compiler->raw("\$this");
     } else {
         $compiler->raw('$this->env->loadTemplate(')->subcompile($this->getNode('expr'))->raw(")");
     }
     $compiler->raw(";\n");
 }
コード例 #4
0
ファイル: Include.php プロジェクト: jasmun/Noco100
 /**
  * Compiles the node to PHP.
  *
  * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance
  */
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     if ($this->getAttribute('ignore_missing')) {
         $compiler->write("try {\n")->indent();
     }
     $this->addGetTemplate($compiler);
     $compiler->raw('->display(');
     $this->addTemplateArguments($compiler);
     $compiler->raw(");\n");
     if ($this->getAttribute('ignore_missing')) {
         $compiler->outdent()->write("} catch (IfwPsn_Vendor_Twig_Error_Loader \$e) {\n")->indent()->write("// ignore missing template\n")->outdent()->write("}\n\n");
     }
 }
コード例 #5
0
ファイル: If.php プロジェクト: jasmun/Noco100
 /**
  * Compiles the node to PHP.
  *
  * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance
  */
 public function compile(IfwPsn_Vendor_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");
 }
コード例 #6
0
ファイル: Set.php プロジェクト: jasmun/Noco100
 /**
  * Compiles the node to PHP.
  *
  * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance
  */
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     if (count($this->getNode('names')) > 1) {
         $compiler->write('list(');
         foreach ($this->getNode('names') as $idx => $node) {
             if ($idx) {
                 $compiler->raw(', ');
             }
             $compiler->subcompile($node);
         }
         $compiler->raw(')');
     } else {
         if ($this->getAttribute('capture')) {
             $compiler->write("ob_start();\n")->subcompile($this->getNode('values'));
         }
         $compiler->subcompile($this->getNode('names'), false);
         if ($this->getAttribute('capture')) {
             $compiler->raw(" = ('' === \$tmp = ob_get_clean()) ? '' : new IfwPsn_Vendor_Twig_Markup(\$tmp, \$this->env->getCharset())");
         }
     }
     if (!$this->getAttribute('capture')) {
         $compiler->raw(' = ');
         if (count($this->getNode('names')) > 1) {
             $compiler->write('array(');
             foreach ($this->getNode('values') as $idx => $value) {
                 if ($idx) {
                     $compiler->raw(', ');
                 }
                 $compiler->subcompile($value);
             }
             $compiler->raw(')');
         } else {
             if ($this->getAttribute('safe')) {
                 $compiler->raw("('' === \$tmp = ")->subcompile($this->getNode('values'))->raw(") ? '' : new IfwPsn_Vendor_Twig_Markup(\$tmp, \$this->env->getCharset())");
             } else {
                 $compiler->subcompile($this->getNode('values'));
             }
         }
     }
     $compiler->raw(";\n");
 }
コード例 #7
0
ファイル: Macro.php プロジェクト: jasmun/Noco100
 /**
  * Compiles the node to PHP.
  *
  * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance
  */
 public function compile(IfwPsn_Vendor_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 IfwPsn_Vendor_Twig_Markup(\$tmp, \$this->env->getCharset());\n")->outdent()->write("}\n\n");
 }
コード例 #8
0
ファイル: SetTemp.php プロジェクト: jasmun/Noco100
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $name = $this->getAttribute('name');
     $compiler->addDebugInfo($this)->write('if (isset($context[')->string($name)->raw('])) { $_')->raw($name)->raw('_ = $context[')->repr($name)->raw(']; } else { $_')->raw($name)->raw("_ = null; }\n");
 }
コード例 #9
0
ファイル: Block.php プロジェクト: jasmun/Noco100
 /**
  * Compiles the node to PHP.
  *
  * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance
  */
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write(sprintf("public function block_%s(\$context, array \$blocks = array())\n", $this->getAttribute('name')), "{\n")->indent();
     $compiler->subcompile($this->getNode('body'))->outdent()->write("}\n\n");
 }
コード例 #10
0
ファイル: Spaceless.php プロジェクト: jasmun/Noco100
 /**
  * Compiles the node to PHP.
  *
  * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance
  */
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write("ob_start();\n")->subcompile($this->getNode('body'))->write("echo trim(preg_replace('/>\\s+</', '><', ob_get_clean()));\n");
 }
コード例 #11
0
ファイル: BlockReference.php プロジェクト: jasmun/Noco100
 /**
  * Compiles the node to PHP.
  *
  * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance
  */
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write(sprintf("\$this->displayBlock('%s', \$context, \$blocks);\n", $this->getAttribute('name')));
 }
コード例 #12
0
ファイル: Print.php プロジェクト: jasmun/Noco100
 /**
  * Compiles the node to PHP.
  *
  * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance
  */
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write('echo ')->subcompile($this->getNode('expr'))->raw(";\n");
 }
コード例 #13
0
ファイル: Flush.php プロジェクト: jasmun/Noco100
 /**
  * Compiles the node to PHP.
  *
  * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance
  */
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write("flush();\n");
 }
コード例 #14
0
ファイル: Text.php プロジェクト: jasmun/Noco100
 /**
  * Compiles the node to PHP.
  *
  * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance
  */
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write('echo ')->string($this->getAttribute('data'))->raw(";\n");
 }
コード例 #15
0
ファイル: SandboxedPrint.php プロジェクト: jasmun/Noco100
 /**
  * Compiles the node to PHP.
  *
  * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance
  */
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write('echo $this->env->getExtension(\'sandbox\')->ensureToStringAllowed(')->subcompile($this->getNode('expr'))->raw(");\n");
 }
コード例 #16
0
ファイル: Sandbox.php プロジェクト: jasmun/Noco100
 /**
  * Compiles the node to PHP.
  *
  * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance
  */
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write("\$sandbox = \$this->env->getExtension('sandbox');\n")->write("if (!\$alreadySandboxed = \$sandbox->isSandboxed()) {\n")->indent()->write("\$sandbox->enableSandbox();\n")->outdent()->write("}\n")->subcompile($this->getNode('body'))->write("if (!\$alreadySandboxed) {\n")->indent()->write("\$sandbox->disableSandbox();\n")->outdent()->write("}\n");
 }