예제 #1
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     if ($this->getAttribute('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");
         if (!$this->getAttribute('ifexpr')) {
             $compiler->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");
         }
     }
 }
예제 #2
0
파일: For.php 프로젝트: apishka/templater
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write("\$context['_parent'] = \$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");
         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");
 }
예제 #3
0
파일: Set.php 프로젝트: apishka/templater
 public function compile(Apishka_Templater_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 Apishka_Templater_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 Apishka_Templater_Markup(\$tmp, \$this->env->getCharset())");
             } else {
                 $compiler->subcompile($this->getNode('values'));
             }
         }
     }
     $compiler->raw(";\n");
 }
예제 #4
0
 /**
  * Compile args
  *
  * @param Apishka_Templater_Compiler     $compiler
  * @param Apishka_Templater_NodeAbstract $args
  */
 protected function compileContext(Apishka_Templater_Compiler $compiler, Apishka_Templater_NodeAbstract $args)
 {
     if (!count($args)) {
         $compiler->raw('$context, ');
         return;
     }
     $compiler->raw('array_replace(')->raw('$context, ')->raw('array(');
     foreach ($args as $name => $arg) {
         $compiler->write('')->string($name)->raw(' => ')->subcompile($arg)->raw(',');
     }
     $compiler->raw(')')->raw('), ');
 }
예제 #5
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $tags = $filters = $functions = array();
     foreach (array('tags', 'filters', 'functions') as $type) {
         foreach ($this->{'used' . ucfirst($type)} as $name => $node) {
             if ($node instanceof Apishka_Templater_NodeAbstract) {
                 ${$type}[$name] = $node->getLine();
             } else {
                 ${$type}[$node] = null;
             }
         }
     }
     $compiler->write('$tags = ')->repr(array_filter($tags))->raw(";\n")->write('$filters = ')->repr(array_filter($filters))->raw(";\n")->write('$functions = ')->repr(array_filter($functions))->raw(";\n\n")->write("try {\n")->indent()->write("\$this->env->getExtension('sandbox')->checkSecurity(\n")->indent()->write(!$tags ? "array(),\n" : "array('" . implode("', '", array_keys($tags)) . "'),\n")->write(!$filters ? "array(),\n" : "array('" . implode("', '", array_keys($filters)) . "'),\n")->write(!$functions ? "array()\n" : "array('" . implode("', '", array_keys($functions)) . "')\n")->outdent()->write(");\n")->outdent()->write("} catch (Apishka_Templater_Sandbox_SecurityError \$e) {\n")->indent()->write("\$e->setTemplateFile(\$this->getTemplateName());\n\n")->write("if (\$e instanceof Apishka_Templater_Sandbox_SecurityNotAllowedTagError && isset(\$tags[\$e->getTagName()])) {\n")->indent()->write("\$e->setTemplateLine(\$tags[\$e->getTagName()]);\n")->outdent()->write("} elseif (\$e instanceof Apishka_Templater_Sandbox_SecurityNotAllowedFilterError && isset(\$filters[\$e->getFilterName()])) {\n")->indent()->write("\$e->setTemplateLine(\$filters[\$e->getFilterName()]);\n")->outdent()->write("} elseif (\$e instanceof Apishka_Templater_Sandbox_SecurityNotAllowedFunctionError && isset(\$functions[\$e->getFunctionName()])) {\n")->indent()->write("\$e->setTemplateLine(\$functions[\$e->getFunctionName()]);\n")->outdent()->write("}\n\n")->write("throw \$e;\n")->outdent()->write("}\n\n");
 }
예제 #6
0
파일: If.php 프로젝트: apishka/templater
 public function compile(Apishka_Templater_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");
 }
예제 #7
0
 protected function compileDebugInfo(Apishka_Templater_Compiler $compiler)
 {
     $compiler->write("public function getDebugInfo()\n", "{\n")->indent()->write(sprintf("return %s;\n", str_replace("\n", '', var_export(array_reverse($compiler->getDebugInfo(), true), true))))->outdent()->write("}\n");
 }
예제 #8
0
 /**
  * {@inheritdoc}
  */
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->write(sprintf('$%s = $this->env->getExtension(', $this->getAttribute('var_name')))->repr($this->getAttribute('extension_name'))->raw(");\n")->write(sprintf('$%s->enter($%s = new Apishka_Templater_Profiler_Profile($this->getTemplateName(), ', $this->getAttribute('var_name'), $this->getAttribute('var_name') . '_prof'))->repr($this->getAttribute('type'))->raw(', ')->repr($this->getAttribute('name'))->raw("));\n\n");
 }
예제 #9
0
 /**
  * {@inheritdoc}
  */
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->write("\n")->write(sprintf("\$%s->leave(\$%s);\n\n", $this->getAttribute('var_name'), $this->getAttribute('var_name') . '_prof'));
 }