Exemplo n.º 1
0
 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");
 }
Exemplo n.º 2
0
 public function compile(Apishka_Templater_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)');
     }
 }
Exemplo n.º 3
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->raw('(')->subcompile($this->getNode('node'))->raw(' === constant(');
     if ($this->getNode('arguments')->hasNode(1)) {
         $compiler->raw('get_class(')->subcompile($this->getNode('arguments')->getNode(1))->raw(')."::".');
     }
     $compiler->subcompile($this->getNode('arguments')->getNode(0))->raw('))');
 }
Exemplo n.º 4
0
 /**
  * Compile
  *
  * @param Apishka_Templater_Compiler $compiler
  */
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $name = $this->getAttribute('name');
     $test = $compiler->getEnvironment()->getTest($name);
     $this->setAttribute('name', $name);
     $this->setAttribute('type', 'test');
     $this->setAttribute('callable', 'twig_test_empty');
     $this->setAttribute('is_variadic', $test->isVariadic());
     $this->compileCallable($compiler);
 }
Exemplo n.º 5
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('), ');
 }
Exemplo n.º 6
0
 /**
  * Compile
  *
  * @param Apishka_Templater_Compiler $compiler
  */
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $name = $this->getAttribute('name');
     $compiler->addDebugInfo($this);
     if ($this->isSpecial()) {
         $compiler->raw($this->specialVars[$name]);
     } elseif ($this->getAttribute('always_defined')) {
         $compiler->raw('$context[')->string($name)->raw(']');
     } else {
         $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : null)');
     }
 }
Exemplo n.º 7
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");
         }
     }
 }
Exemplo n.º 8
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $name = $this->getAttribute('name');
     $function = $compiler->getEnvironment()->getFunction($name);
     $this->setAttribute('name', $name);
     $this->setAttribute('type', 'function');
     $this->setAttribute('needs_environment', $function->needsEnvironment());
     $this->setAttribute('needs_context', $function->needsContext());
     $this->setAttribute('arguments', $function->getArguments());
     $this->setAttribute('callable', $function->getCallable());
     $this->setAttribute('is_variadic', $function->isVariadic());
     $this->compileCallable($compiler);
 }
Exemplo n.º 9
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->raw('array(');
     $first = true;
     foreach ($this->getKeyValuePairs() as $pair) {
         if (!$first) {
             $compiler->raw(', ');
         }
         $first = false;
         $compiler->subcompile($pair['key'])->raw(' => ')->subcompile($pair['value']);
     }
     $compiler->raw(')');
 }
Exemplo n.º 10
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->subcompile($this->getNode('node'))->raw('->')->raw($this->getAttribute('method'))->raw('(');
     $first = true;
     foreach ($this->getNode('arguments')->getKeyValuePairs() as $pair) {
         if (!$first) {
             $compiler->raw(', ');
         }
         $first = false;
         $compiler->subcompile($pair['value']);
     }
     $compiler->raw(')');
 }
Exemplo n.º 11
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");
 }
Exemplo n.º 12
0
 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");
 }
Exemplo n.º 13
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     if (function_exists('twig_template_get_attributes') && !$this->getAttribute('disable_c_ext')) {
         $compiler->raw('twig_template_get_attributes($this, ');
     } else {
         $compiler->raw('$this->getAttribute(');
     }
     $compiler->subcompile($this->getNode('node'));
     $compiler->raw(', ')->subcompile($this->getNode('attribute'));
     // only generate optional arguments when needed (to make generated code more readable)
     $needSecond = Apishka_Templater_TemplateAbstract::ANY_CALL !== $this->getAttribute('type');
     $needFirst = $needSecond || null !== $this->getNode('arguments');
     if ($needFirst) {
         if (null !== $this->getNode('arguments')) {
             $compiler->raw(', ')->subcompile($this->getNode('arguments'));
         } else {
             $compiler->raw(', array()');
         }
     }
     if ($needSecond) {
         $compiler->raw(', ')->repr($this->getAttribute('type'));
     }
     $compiler->raw(')');
 }
Exemplo n.º 14
0
 /**
  * Compile
  *
  * @param Apishka_Templater_Compiler $compiler
  */
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->raw('(')->subcompile($this->getNode('left'))->raw(' ');
     $this->operator($compiler);
     $compiler->raw(' ')->subcompile($this->getNode('right'))->raw(')');
 }
Exemplo n.º 15
0
 /**
  * Compile arguments
  *
  * @param Apishka_Templater_Compiler $compiler
  */
 protected function compileArguments(Apishka_Templater_Compiler $compiler)
 {
     $compiler->raw('(');
     $first = true;
     if ($this->hasAttribute('needs_environment') && $this->getAttribute('needs_environment')) {
         $compiler->raw('$this->env');
         $first = false;
     }
     if ($this->hasAttribute('needs_context') && $this->getAttribute('needs_context')) {
         if (!$first) {
             $compiler->raw(', ');
         }
         $compiler->raw('$context');
         $first = false;
     }
     if ($this->hasAttribute('arguments')) {
         foreach ($this->getAttribute('arguments') as $argument) {
             if (!$first) {
                 $compiler->raw(', ');
             }
             $compiler->string($argument);
             $first = false;
         }
     }
     if ($this->hasNode('node')) {
         if (!$first) {
             $compiler->raw(', ');
         }
         $compiler->subcompile($this->getNode('node'));
         $first = false;
     }
     if ($this->hasNode('arguments') && null !== $this->getNode('arguments')) {
         $callable = $this->getAttribute('callable');
         $arguments = $this->getArguments($callable, $this->getNode('arguments'));
         foreach ($arguments as $node) {
             if (!$first) {
                 $compiler->raw(', ');
             }
             $compiler->subcompile($node);
             $first = false;
         }
     }
     $compiler->raw(')');
 }
Exemplo n.º 16
0
 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");
 }
Exemplo n.º 17
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->raw('$_')->raw($this->getAttribute('name'))->raw('_');
 }
Exemplo n.º 18
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write('')->subcompile($this->getNode('expr'))->raw(";\n");
 }
Exemplo n.º 19
0
 /**
  * Compile
  *
  * @param Apishka_Templater_Compiler $compiler
  */
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->raw(' ');
     $this->operator($compiler);
     $compiler->subcompile($this->getNode('node'));
 }
Exemplo n.º 20
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'));
 }
Exemplo n.º 21
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");
 }
Exemplo n.º 22
0
 /**
  * Operator
  *
  * @param Apishka_Templater_Compiler $compiler
  */
 public function operator(Apishka_Templater_Compiler $compiler)
 {
     $compiler->raw('!');
 }
Exemplo n.º 23
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->raw('((')->subcompile($this->getNode('expr1'))->raw(') ? (')->subcompile($this->getNode('expr2'))->raw(') : (')->subcompile($this->getNode('expr3'))->raw('))');
 }
Exemplo n.º 24
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->raw('(0 == ')->subcompile($this->getNode('node'))->raw(' % ')->subcompile($this->getNode('arguments')->getNode(0))->raw(')');
 }
Exemplo n.º 25
0
 /**
  * Operator
  *
  * @param Apishka_Templater_Compiler $compiler
  *
  * @return Apishka_Templater_Compiler
  */
 public function operator(Apishka_Templater_Compiler $compiler)
 {
     return $compiler->raw('!==');
 }
Exemplo n.º 26
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->raw('(null === ')->subcompile($this->getNode('node'))->raw(')');
 }
Exemplo n.º 27
0
 public function compile(Apishka_Templater_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");
 }
Exemplo n.º 28
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->subcompile($this->getNode('body'));
 }
Exemplo n.º 29
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");
 }
Exemplo n.º 30
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write('echo ')->string($this->getAttribute('data'))->raw(";\n");
 }