Example #1
0
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     if (function_exists('ifwpsn_twig_template_get_attributes') && !$this->getAttribute('disable_c_ext')) {
         $compiler->raw('ifwpsn_twig_template_get_attributes($this, ');
     } else {
         $compiler->raw('$this->getAttribute(');
     }
     if ($this->getAttribute('ignore_strict_check')) {
         $this->getNode('node')->setAttribute('ignore_strict_check', true);
     }
     $compiler->subcompile($this->getNode('node'));
     $compiler->raw(', ')->subcompile($this->getNode('attribute'));
     if (count($this->getNode('arguments')) || IfwPsn_Vendor_Twig_Template::ANY_CALL !== $this->getAttribute('type') || $this->getAttribute('is_defined_test') || $this->getAttribute('ignore_strict_check')) {
         $compiler->raw(', ')->subcompile($this->getNode('arguments'));
         if (IfwPsn_Vendor_Twig_Template::ANY_CALL !== $this->getAttribute('type') || $this->getAttribute('is_defined_test') || $this->getAttribute('ignore_strict_check')) {
             $compiler->raw(', ')->repr($this->getAttribute('type'));
         }
         if ($this->getAttribute('is_defined_test') || $this->getAttribute('ignore_strict_check')) {
             $compiler->raw(', ' . ($this->getAttribute('is_defined_test') ? 'true' : 'false'));
         }
         if ($this->getAttribute('ignore_strict_check')) {
             $compiler->raw(', ' . ($this->getAttribute('ignore_strict_check') ? 'true' : 'false'));
         }
     }
     $compiler->raw(')');
 }
Example #2
0
 /**
  * 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");
 }
Example #3
0
 /**
  * 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)");
     }
 }
Example #4
0
 public function compile(IfwPsn_Vendor_Twig_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('))');
 }
Example #5
0
 /**
  * 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");
 }
Example #6
0
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $name = $this->getAttribute('name');
     $test = $compiler->getEnvironment()->getTest($name);
     $this->setAttribute('name', $name);
     $this->setAttribute('type', 'test');
     $this->setAttribute('thing', $test);
     if ($test instanceof IfwPsn_Vendor_Twig_TestCallableInterface || $test instanceof IfwPsn_Vendor_Twig_SimpleTest) {
         $this->setAttribute('callable', $test->getCallable());
     }
     $this->compileCallable($compiler);
 }
Example #7
0
 /**
  * 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('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");
         }
     }
 }
Example #8
0
 public function compile(IfwPsn_Vendor_Twig_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(')');
 }
Example #9
0
 /**
  * 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->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(')');
 }
Example #10
0
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $name = $this->getNode('filter')->getAttribute('value');
     $filter = $compiler->getEnvironment()->getFilter($name);
     $this->setAttribute('name', $name);
     $this->setAttribute('type', 'filter');
     $this->setAttribute('thing', $filter);
     $this->setAttribute('needs_environment', $filter->needsEnvironment());
     $this->setAttribute('needs_context', $filter->needsContext());
     $this->setAttribute('arguments', $filter->getArguments());
     if ($filter instanceof IfwPsn_Vendor_Twig_FilterCallableInterface || $filter instanceof IfwPsn_Vendor_Twig_SimpleFilter) {
         $this->setAttribute('callable', $filter->getCallable());
     }
     $this->compileCallable($compiler);
 }
Example #11
0
 protected function addTemplateArguments(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     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'));
         }
     }
 }
Example #12
0
 /**
  * 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");
 }
Example #13
0
 /**
  * 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");
 }
Example #14
0
 /**
  * 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->raw('(')->subcompile($this->getNode('left'))->raw(' ');
     $this->operator($compiler);
     $compiler->raw(' ')->subcompile($this->getNode('right'))->raw(')');
 }
Example #15
0
 /**
  * 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");
 }
Example #16
0
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->repr($this->getAttribute('value'));
 }
Example #17
0
 /**
  * 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");
 }
Example #18
0
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->raw('(0 == ')->subcompile($this->getNode('node'))->raw(' % ')->subcompile($this->getNode('arguments')->getNode(0))->raw(')');
 }
Example #19
0
 protected function compileDisplayBody(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->write("\$this->checkSecurity();\n");
     parent::compileDisplayBody($compiler);
 }
Example #20
0
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->raw('((')->subcompile($this->getNode('expr1'))->raw(') ? (')->subcompile($this->getNode('expr2'))->raw(') : (')->subcompile($this->getNode('expr3'))->raw('))');
 }
Example #21
0
 protected function addGetTemplate(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->write("\$this->env->loadTemplate(")->string($this->getAttribute('filename'))->raw(', ')->string($this->getAttribute('index'))->raw(")");
 }
Example #22
0
 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");
 }
Example #23
0
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->raw('(')->subcompile($this->getNode('node'))->raw(' % 2 == 0')->raw(')');
 }
Example #24
0
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $name = $this->getAttribute('name');
     if ($this->getAttribute('is_defined_test')) {
         if ($this->isSpecial()) {
             $compiler->repr(true);
         } else {
             $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)');
         }
     } elseif ($this->isSpecial()) {
         $compiler->raw($this->specialVars[$name]);
     } elseif ($this->getAttribute('always_defined')) {
         $compiler->raw('$context[')->string($name)->raw(']');
     } else {
         // remove the non-PHP 5.4 version when PHP 5.3 support is dropped
         // as the non-optimized version is just a workaround for slow ternary operator
         // when the context has a lot of variables
         if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
             // PHP 5.4 ternary operator performance was optimized
             $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : ');
             if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
                 $compiler->raw('null)');
             } else {
                 $compiler->raw('$this->getContext($context, ')->string($name)->raw('))');
             }
         } else {
             $compiler->raw('$this->getContext($context, ')->string($name);
             if ($this->getAttribute('ignore_strict_check')) {
                 $compiler->raw(', true');
             }
             $compiler->raw(')');
         }
     }
 }
Example #25
0
 public function compile(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     $compiler->subcompile($this->getNode('node'));
 }
Example #26
0
 protected function compileLoadTemplate(IfwPsn_Vendor_Twig_Compiler $compiler, $node, $var)
 {
     if ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_Constant) {
         $compiler->write(sprintf("%s = \$this->env->loadTemplate(", $var))->subcompile($node)->raw(");\n");
     } else {
         $compiler->write(sprintf("%s = ", $var))->subcompile($node)->raw(";\n")->write(sprintf("if (!%s", $var))->raw(" instanceof IfwPsn_Vendor_Twig_Template) {\n")->indent()->write(sprintf("%s = \$this->env->loadTemplate(%s);\n", $var, $var))->outdent()->write("}\n");
     }
 }
Example #27
0
 public function operator(IfwPsn_Vendor_Twig_Compiler $compiler)
 {
     return $compiler->raw('^');
 }
Example #28
0
 /**
  * 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");
 }
Example #29
0
 /**
  * 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->raw('$context[')->string($this->getAttribute('name'))->raw(']');
 }
Example #30
0
 /**
  * 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')));
 }