Esempio n. 1
0
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     list($msg, $vars) = $this->compileString($this->getNode('body'));
     if (null !== $this->getNode('plural')) {
         list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
         $vars = array_merge($vars, $vars1);
     }
     $function = null === $this->getNode('plural') ? '\\Skeleton\\I18n\\Translation::translate' : '\\Skeleton\\I18n\\Translation::translate_plural';
     if ($vars) {
         $compiler->write('echo strtr(' . $function . '(')->subcompile($msg);
         if (null !== $this->getNode('plural')) {
             $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->getNode('count'))->raw(')');
         }
         $compiler->raw(', $context[\'env\'][\'translation\']), array(');
         foreach ($vars as $var) {
             if ('count' === $var->getAttribute('name')) {
                 $compiler->string('%count%')->raw(' => abs(')->subcompile($this->getNode('count'))->raw('), ');
             } else {
                 $compiler->string('%' . $var->getAttribute('name') . '%')->raw(' => ')->subcompile($var)->raw(', ');
             }
         }
         $compiler->raw("));\n");
     } else {
         $compiler->write('echo ' . $function . '(')->subcompile($msg);
         if (null !== $this->getNode('plural')) {
             $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->getNode('count'))->raw(')');
         }
         $compiler->raw(", " . '$context[\'env\'][\'translation\']' . ");\n");
     }
 }
Esempio n. 2
0
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler $compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $vars = $this->getNode('vars');
     $defaults = new \Twig_Node_Expression_Array(array(), -1);
     if ($vars instanceof \Twig_Node_Expression_Array) {
         $defaults = $this->getNode('vars');
         $vars = null;
     }
     list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults, (bool) $vars);
     $method = null === $this->getNode('count') ? 'trans' : 'transChoice';
     $compiler->write('echo $this->env->getExtension(\'translator\')->getTranslator()->' . $method . '(')->subcompile($msg);
     $compiler->raw(', ');
     if (null !== $this->getNode('count')) {
         $compiler->subcompile($this->getNode('count'))->raw(', ');
     }
     if (null !== $vars) {
         $compiler->raw('array_merge(')->subcompile($defaults)->raw(', ')->subcompile($this->getNode('vars'))->raw(')');
     } else {
         $compiler->subcompile($defaults);
     }
     $compiler->raw(', ');
     if (null === $this->getNode('domain')) {
         $compiler->repr('messages');
     } else {
         $compiler->subcompile($this->getNode('domain'));
     }
     if (null !== $this->getNode('locale')) {
         $compiler->raw(', ')->subcompile($this->getNode('locale'));
     }
     $compiler->raw(");\n");
 }
Esempio n. 3
0
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     list($msg, $vars) = $this->compileString($this->getNode('body'));
     if (null !== $this->getNode('plural')) {
         list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
         $vars = array_merge($vars, $vars1);
     }
     $function = null === $this->getNode('plural') ? 'gettext' : 'ngettext';
     if ($vars) {
         $compiler->write('echo strtr(' . $function . '(')->subcompile($msg);
         if (null !== $this->getNode('plural')) {
             $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->getNode('count'))->raw(')');
         }
         $compiler->raw('), array(');
         foreach ($vars as $var) {
             if ('count' === $var->getAttribute('name')) {
                 $compiler->string('%count%')->raw(' => abs(')->subcompile($this->getNode('count'))->raw('), ');
             } else {
                 $compiler->string('%' . $var->getAttribute('name') . '%')->raw(' => ')->subcompile($var)->raw(', ');
             }
         }
         $compiler->raw("));\n");
     } else {
         $compiler->write('echo ' . $function . '(')->subcompile($msg);
         if (null !== $this->getNode('plural')) {
             $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->getNode('count'))->raw(')');
         }
         $compiler->raw(");\n");
     }
 }
Esempio n. 4
0
 public function compile(Twig_Compiler $compiler)
 {
     $testMap = $compiler->getEnvironment()->getTests();
     if (!isset($testMap[$this->getAttribute('name')])) {
         throw new Twig_Error_Syntax(sprintf('The test "%s" does not exist', $this->getAttribute('name')), $this->getLine());
     }
     $name = $this->getAttribute('name');
     $node = $this->getNode('node');
     // defined is a special case
     if ('defined' === $name) {
         $compiler->subcompile($node);
         return;
     }
     $compiler->raw($testMap[$name]->compile() . '(')->subcompile($node);
     if (null !== $this->getNode('arguments')) {
         $compiler->raw(', ');
         $max = count($this->getNode('arguments')) - 1;
         foreach ($this->getNode('arguments') as $i => $arg) {
             $compiler->subcompile($arg);
             if ($i != $max) {
                 $compiler->raw(', ');
             }
         }
     }
     $compiler->raw(')');
 }
Esempio n. 5
0
 public function compile(Twig_Compiler $compiler)
 {
     $name = $this->getAttribute('name');
     $compiler->addDebugInfo($this);
     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 {
         if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
             $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : null)');
         } else {
             // When Twig will require PHP 7.0, the Template::notFound() method
             // will be removed and the code inlined like this:
             // (function () { throw new Exception(...); })();
             $compiler->raw('(isset($context[')->string($name)->raw(']) || array_key_exists(')->string($name)->raw(', $context) ? $context[')->string($name)->raw('] : $this->notFound(')->string($name)->raw(', ')->repr($this->lineno)->raw('))');
         }
     }
 }
Esempio n. 6
0
 public function compile(Twig_Compiler $compiler)
 {
     $testMap = $compiler->getEnvironment()->getTests();
     if (!isset($testMap[$this->getAttribute('name')])) {
         throw new Twig_Error_Syntax(sprintf('The test "%s" does not exist', $this->getAttribute('name')), $this->getLine());
     }
     $name = $this->getAttribute('name');
     $node = $this->getNode('node');
     // defined is a special case
     if ('defined' === $name) {
         if ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr) {
             $node->setAttribute('is_defined_test', true);
             $compiler->subcompile($node);
             $node->removeAttribute('is_defined_test');
         } else {
             throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine());
         }
         return;
     }
     $compiler->raw($testMap[$name]->compile() . '(')->subcompile($node);
     if (null !== $this->getNode('arguments')) {
         $compiler->raw(', ');
         $max = count($this->getNode('arguments')) - 1;
         foreach ($this->getNode('arguments') as $i => $arg) {
             $compiler->subcompile($arg);
             if ($i != $max) {
                 $compiler->raw(', ');
             }
         }
     }
     $compiler->raw(')');
 }
Esempio n. 7
0
 public function compile(Twig_Compiler $compiler)
 {
     $name = $this->getAttribute('name');
     $testMap = $compiler->getEnvironment()->getTests();
     if (!isset($testMap[$name])) {
         $message = sprintf('The test "%s" does not exist', $name);
         if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getTests()))) {
             $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
         }
         throw new Twig_Error_Syntax($message, $this->getLine());
     }
     $name = $this->getAttribute('name');
     $node = $this->getNode('node');
     $compiler->raw($testMap[$name]->compile() . '(')->subcompile($node);
     if (null !== $this->getNode('arguments')) {
         $compiler->raw(', ');
         $max = count($this->getNode('arguments')) - 1;
         foreach ($this->getNode('arguments') as $i => $arg) {
             $compiler->subcompile($arg);
             if ($i != $max) {
                 $compiler->raw(', ');
             }
         }
     }
     $compiler->raw(')');
 }
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     if ($this->getAttribute('as_string')) {
         $compiler->raw('(string) ');
     }
     $compiler->raw("\$this->renderBlock(")->subcompile($this->getNode('name'))->raw(", \$context, \$blocks)");
 }
Esempio n. 9
0
 /**
  * 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");
     }
 }
Esempio n. 10
0
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     list($msg, $vars) = $this->compileString($this->nodes['body']);
     if (null !== $this->nodes['plural']) {
         list($msg1, $vars1) = $this->compileString($this->nodes['plural']);
         $vars = array_merge($vars, $vars1);
     }
     $function = null === $this->nodes['plural'] ? '__' : '__n';
     if ($vars) {
         $compiler->write('echo strtr(' . $function . '(')->subcompile($msg);
         if (null !== $this->nodes['plural']) {
             $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->nodes['count'])->raw(')');
         }
         $compiler->raw(', true), array(');
         // modified: cakephp $return flag
         foreach ($vars as $var) {
             if ('count' === $var->getAttribute('name')) {
                 $compiler->string('%count%')->raw(' => abs(')->subcompile($this->nodes['count'])->raw('), ');
             } else {
                 $compiler->string('%' . $var->getAttribute('name') . '%')->raw(' => ')->subcompile($var)->raw(', ');
             }
         }
         $compiler->raw("));\n");
     } else {
         $compiler->write('echo ' . $function . '(')->subcompile($msg);
         if (null !== $this->nodes['plural']) {
             $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->nodes['count'])->raw(')');
         }
         $compiler->raw(', true);');
         // modified: cakephp $return flag
     }
 }
 public function compile(Twig_Compiler $compiler)
 {
     $name = $this->getAttribute('name');
     if (false === ($function = $compiler->getEnvironment()->getFunction($name))) {
         $message = sprintf('The function "%s" does not exist', $name);
         if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getFunctions()))) {
             $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
         }
         throw new Twig_Error_Syntax($message, $this->getLine());
     }
     $compiler->raw($function->compile() . '(')->raw($function->needsEnvironment() ? '$this->env' : '');
     if ($function->needsContext()) {
         $compiler->raw($function->needsEnvironment() ? ', $context' : '$context');
     }
     $first = true;
     foreach ($this->getNode('arguments') as $node) {
         if (!$first) {
             $compiler->raw(', ');
         } else {
             if ($function->needsEnvironment() || $function->needsContext()) {
                 $compiler->raw(', ');
             }
             $first = false;
         }
         $compiler->subcompile($node);
     }
     $compiler->raw(')');
 }
Esempio n. 12
0
    public function compile(Twig_Compiler $compiler)
    {
        $function = $compiler->getEnvironment()->getFunction($this->getNode('name')->getAttribute('name'));
        if (!$function) {
            throw new Twig_Error_Syntax(sprintf('The function "%s" does not exist', $this->getNode('name')->getAttribute('name')), $this->getLine());
        }

        $compiler
            ->raw($function->compile().'(')
            ->raw($function->needsEnvironment() ? '$this->env, ' : '')
            ->raw($function->needsContext() ? '$context, ' : '')
        ;

        $first = true;
        foreach ($this->getNode('arguments') as $node) {
            if (!$first) {
                $compiler->raw(', ');
            } else {
                $first = false;
            }
            $compiler->subcompile($node);
        }

        $compiler->raw(')');
    }
Esempio n. 13
0
 private function compileBlockArguments(Twig_Compiler $compiler)
 {
     $compiler->raw('(')->subcompile($this->getNode('name'))->raw(', $context');
     if (!$this->hasNode('template')) {
         $compiler->raw(', $blocks');
     }
     return $compiler->raw(')');
 }
Esempio n. 14
0
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->raw('$this->getAttribute(')->subcompile($this->getNode('node'))->raw(', ')->subcompile($this->getNode('attribute'))->raw(', array(');
     foreach ($this->getNode('arguments') as $node) {
         $compiler->subcompile($node)->raw(', ');
     }
     $compiler->raw('), ')->repr($this->getAttribute('type'))->raw($this->hasAttribute('is_defined_test') ? ', true' : ', false')->raw(sprintf(', %d', $this->lineno))->raw(')');
 }
Esempio n. 15
0
 public function compile(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('))');
 }
Esempio n. 16
0
 protected function compileFilter(Twig_Compiler $compiler, Twig_FilterInterface $filter)
 {
     $compiler->raw($filter->compile() . '(')->raw($filter->needsEnvironment() ? '$this->env, ' : '')->raw($filter->needsContext() ? '$context, ' : '')->subcompile($this->getNode('node'));
     foreach ($this->getNode('arguments') as $node) {
         $compiler->raw(', ')->subcompile($node);
     }
     $compiler->raw(')');
 }
Esempio n. 17
0
 protected function compileDefaults(\Twig_Compiler $compiler, \Twig_Node_Expression_Array $defaults)
 {
     $compiler->raw('array(');
     foreach ($defaults as $name => $default) {
         $compiler->repr($name)->raw(' => ')->subcompile($default)->raw(', ');
     }
     $compiler->raw(')');
 }
 public function compile(\Twig_Compiler $compiler)
 {
     $arguments = $this->getNode('arguments');
     if ($arguments->count() === 1) {
         $compiler->raw('$this->env->getExtension(\'supraPage\')->isPropertyValueEmpty(');
         $compiler->subcompile($arguments->getIterator()->current());
         $compiler->raw(')');
     }
 }
Esempio n. 19
0
 public function compile(\Twig_Compiler $compiler)
 {
     $arguments = $this->getAttribute('arguments');
     $compiler->addDebugInfo($this)->write('$template_storage = new Bolt\\Storage($context[\'app\']);' . "\n")->write('$context[\'' . $this->getAttribute('name') . '\'] = ')->write('$template_storage->getContent(')->subcompile($this->getAttribute('contenttype'))->raw(", ")->subcompile($arguments);
     if (!is_null($this->getNode('wherearguments'))) {
         $compiler->raw(', $pager, ')->subcompile($this->getNode('wherearguments'));
     }
     $compiler->raw(" );\n");
 }
 public function compile(\Twig_Compiler $compiler)
 {
     $options = $this->getNode('options');
     $compiler->write("ob_start();\n")->subcompile($this->getNode('body'))->write("\$body = ob_get_clean();\n")->write("echo \\Craft\\craft()->premailer->compile(\$body");
     if ($options) {
         $compiler->raw(",")->subcompile($options);
     }
     $compiler->raw(");\n");
 }
Esempio n. 21
0
 public function compile(Twig_Compiler $compiler)
 {
     $name = $this->getAttribute('name');
     $isDefined = $this->getAttribute("is_defined_test");
     if ($isDefined) {
         $compiler->raw('isset($context[')->string($name)->raw('])');
     } else {
         $compiler->raw('$context[')->string($name)->raw(']');
     }
 }
 /**
  * Compiles an IncludeResource_Node into PHP.
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $function = $this->getAttribute('function');
     $path = $this->getNode('path');
     $compiler->addDebugInfo($this)->write('\\Craft\\craft()->templates->' . $function . '(')->subcompile($path);
     if ($this->getAttribute('first')) {
         $compiler->raw(', true');
     }
     $compiler->raw(");\n");
 }
Esempio n. 23
0
 /**
  * {@inheritdoc}
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write("\$containerService = \$this->env->getExtension('swp_container')->getContainerService();\n")->write('$container = $containerService->getContainer(')->subcompile($this->getNode('name'))->raw(', ');
     if ($this->hasNode('parameters')) {
         $compiler->subcompile($this->getNode('parameters'));
     } else {
         $compiler->raw('array()');
     }
     $compiler->raw(");\n")->write("if (\$container->isVisible()) {\n")->indent()->write("echo \$container->renderOpenTag();\n")->write("if (\$container->hasWidgets()) {\n")->indent()->write("echo \$container->renderWidgets();\n")->outdent()->write("} else {\n")->indent()->subcompile($this->getNode('body'))->outdent()->write("}\n")->write("echo \$container->renderCloseTag();\n")->outdent()->write("}\n")->write("unset(\$container);unset(\$containerService);\n");
 }
Esempio n. 24
0
 protected function compileGetParent(Twig_Compiler $compiler)
 {
     $compiler->write("protected function doGetParent(array \$context)\n", "{\n")->indent()->write("return ");
     if (null === $this->getNode('parent')) {
         $compiler->raw("false");
     } else {
         $compiler->subcompile($this->getNode('parent'));
     }
     $compiler->raw(";\n")->outdent()->write("}\n\n");
 }
Esempio n. 25
0
 protected function addTemplateArguments(Twig_Compiler $compiler)
 {
     if (null === $this->getNode('variables')) {
         $compiler->raw(false === $this->getAttribute('only') ? '$context' : 'array()');
     } elseif (false === $this->getAttribute('only')) {
         $compiler->raw('array_merge($context, ')->subcompile($this->getNode('variables'))->raw(')');
     } else {
         $compiler->subcompile($this->getNode('variables'));
     }
 }
Esempio n. 26
0
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write('')->subcompile($this->getNode('var'))->raw(' = ');
     if ($this->getNode('expr') instanceof 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");
 }
Esempio n. 27
0
 public function compile(Twig_Compiler $compiler)
 {
     if (function_exists('twig_template_get_attributes')) {
         $compiler->raw('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')) || Twig_TemplateInterface::ANY_CALL !== $this->getAttribute('type') || $this->getAttribute('is_defined_test') || $this->getAttribute('ignore_strict_check')) {
         $compiler->raw(', array(');
         foreach ($this->getNode('arguments') as $node) {
             $compiler->subcompile($node)->raw(', ');
         }
         $compiler->raw(')');
         if (Twig_TemplateInterface::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(')');
 }
Esempio n. 28
0
 public function compile(Twig_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(');
     }
     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'));
     // only generate optional arguments when needed (to make generated code more readable)
     $needFourth = $this->getAttribute('ignore_strict_check');
     $needThird = $needFourth || $this->getAttribute('is_defined_test');
     $needSecond = $needThird || Twig_Template::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'));
     }
     if ($needThird) {
         $compiler->raw(', ')->repr($this->getAttribute('is_defined_test'));
     }
     if ($needFourth) {
         $compiler->raw(', ')->repr($this->getAttribute('ignore_strict_check'));
     }
     $compiler->raw(')');
 }
Esempio n. 29
0
 public function compile(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(')');
         }
     }
 }
Esempio n. 30
0
 /**
  * {@inheritdoc}
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $i = self::$count++;
     $collectionTypeName = $this->getNode('collectionType')->getNode(0)->getAttribute('name');
     $compiler->addDebugInfo($this);
     if ($this->hasNode('collectionFilters')) {
         $compiler->write("\$context['_collection_type_filters'] = [];\n");
         $compiler->write("\$context['" . $collectionTypeName . "'] = null;\n");
         $compiler->write("\$context['_collection_type_filters'] = ")->subcompile($this->getNode('collectionFilters'))->raw("['_collection_type_filters']; unset(\$context['" . $collectionTypeName . "']['_collection_type_filters']);\n");
         if ($this->hasNode('parameters')) {
             $compiler->write('$parameters = array_merge(')->subcompile($this->getNode('parameters'))->raw(", \$context['_collection_type_filters']);\n");
         } else {
             $compiler->write("\$parameters = \$context['_collection_type_filters'];\n");
         }
     } else {
         if ($this->hasNode('parameters')) {
             $compiler->raw('$parameters = ')->subcompile($this->getNode('parameters'))->raw(";\n");
         } else {
             $compiler->raw("\$parameters = [];\n");
         }
     }
     $compiler->write('$swpCollectionMetaLoader' . $i . " = \$this->env->getExtension('swp_gimme')->getLoader();\n")->write('')->subcompile($this->getNode('collectionType'))->raw(' = twig_ensure_traversable($swpCollectionMetaLoader' . $i . '->load("')->raw($collectionTypeName)->raw('", ');
     $compiler->raw('$parameters');
     $compiler->raw(", \\SWP\\Component\\TemplatesSystem\\Gimme\\Loader\\LoaderInterface::COLLECTION));\n");
     // the (array) cast bypasses a PHP 5.2.6 bug
     $compiler->write("\$context['_parent'] = (array) \$context;\n");
     if ($this->hasNode('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') && $this->getNode('collectionType')) {
             $compiler->write('if (is_array(')->subcompile($this->getNode('collectionType'))->raw(') || (is_object(')->subcompile($this->getNode('collectionType'))->raw(') && ')->subcompile($this->getNode('collectionType'))->raw(" instanceof Countable)) {\n")->indent()->write('$length = count(')->subcompile($this->getNode('collectionType'))->raw(");\n")->write("\$context['loop']['revindex0'] = \$length - 1;\n")->write("\$context['loop']['revindex'] = \$length;\n")->write("\$context['loop']['length'] = \$length;\n")->write("\$context['loop']['totalLength'] = \$length;\n")->write("\$context['loop']['last'] = 1 === \$length;\n")->outdent()->write("}\n");
             $compiler->write('if(is_object(')->subcompile($this->getNode('collectionType'))->raw(') && ')->subcompile($this->getNode('collectionType'))->raw(" instanceof \\SWP\\Component\\TemplatesSystem\\Gimme\\Meta\\MetaCollection) {\n")->indent()->write('$context[\'loop\'][\'totalLength\'] = ')->subcompile($this->getNode('collectionType'))->raw("->getTotalItemsCount();\n")->outdent()->write("}\n");
         }
     }
     $this->loop->setAttribute('else', $this->hasNode('else'));
     $this->loop->setAttribute('with_loop', $this->getAttribute('with_loop'));
     $this->loop->setAttribute('ifexpr', $this->getAttribute('ifexpr'));
     if (null !== $this->getNode('collectionType')) {
         $compiler->write('foreach (')->subcompile($this->getNode('collectionType'))->raw(' as $_key')->raw(' => ')->subcompile($this->getNode('variable'))->raw(") {\n")->indent()->subcompile($this->getNode('body'))->outdent()->write("}\n");
     }
     if ($this->hasNode('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[\'' . $this->getNode('variable')->getNode(0)->getAttribute('name') . '\'], $context[\'_iterated\'], $context[\'' . $collectionTypeName . '\'], $context[\'_parent\'], $context[\'loop\']);' . "\n");
     if ($this->hasNode('collectionFilters')) {
         $compiler->write("unset(\$context['_collection_type_filters']);\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");
 }