示例#1
0
 protected function compileWithArguments(\Twig_Compiler $compiler, $expressionNode, array $arguments)
 {
     $compiler->raw("\n");
     $compiler->indent();
     $compiler->addIndentation();
     $compiler->raw("function() use(&\$context) {\n");
     $compiler->indent();
     // copy of arguments and __ from context
     foreach ($arguments as $arg) {
         $compiler->addIndentation();
         $compiler->raw("if (isset(\$context['{$arg}'])) \$outer{$arg} = \$context['{$arg}'];\n");
     }
     $compiler->addIndentation();
     $compiler->raw("if (isset(\$context['__'])) \$outer__ = \$context['__'];\n");
     // adding closure's arguments to context
     $compiler->addIndentation();
     $compiler->raw("\$context['__'] = func_get_args();\n");
     foreach ($arguments as $i => $arg) {
         $compiler->addIndentation();
         $compiler->raw("if (func_num_args()>{$i}) \$context['{$arg}'] = func_get_arg({$i});\n");
         $compiler->addIndentation();
         $compiler->raw("else unset(\$context['{$arg}']);\n");
     }
     // getting call result
     $compiler->addIndentation();
     $compiler->raw("\$result = ");
     $compiler->subcompile($this->getNode($expressionNode));
     $compiler->raw(";\n");
     // recreating original context
     foreach ($arguments as $arg) {
         $compiler->addIndentation();
         $compiler->raw("if (isset(\$outer{$arg})) \$context['{$arg}'] = \$outer{$arg} ;\n");
         $compiler->addIndentation();
         $compiler->raw("else unset(\$context['{$arg}']);\n");
     }
     $compiler->addIndentation();
     $compiler->raw("if (isset(\$outer__)) \$context['__'] = \$outer__ ;\n");
     $compiler->addIndentation();
     $compiler->raw("else unset(\$context['__']);\n");
     // return statement
     $compiler->addIndentation();
     $compiler->raw("return \$result;\n");
     $compiler->outdent();
     $compiler->addIndentation();
     $compiler->raw("}\n");
     $compiler->outdent();
     $compiler->addIndentation();
 }
示例#2
0
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler $compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write('try {');
     $compiler->indent()->subcompile($this->getNode('try'));
     if ($this->hasNode('catch') && null !== $this->getNode('catch')) {
         $compiler->outdent()->write('} catch (\\Exception $e) {' . "\n")->indent()->write('if ($context[\'gantry\']->debug()) throw $e;' . "\n")->write('$context[\'e\'] = $e;' . "\n")->subcompile($this->getNode('catch'));
     }
     $compiler->outdent()->write("}\n");
 }
示例#3
0
 /**
  * Собирает PHP-выражение
  *
  * @param TwigCompiler $compiler Компилятор Twig.
  *
  * @return void
  */
 public function compile(TwigCompiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write('while (');
     $compiler->subcompile($this->getNode('condition'));
     $compiler->write(") {\n");
     $compiler->indent();
     $compiler->subcompile($this->getNode('body'));
     $compiler->outdent();
     $compiler->write("}\n");
 }
示例#4
0
 private function compileMaxPerPage(\Twig_Compiler $compiler)
 {
     $compiler->write("public function getMaxesPerPage()\n", "{\n")->indent();
     $compiler->write("return array(\n");
     $compiler->indent();
     foreach ($this->getAttribute('maxesPerPage') as $maxPerPage) {
         $compiler->write($maxPerPage . ",\n");
     }
     $compiler->outdent();
     $compiler->write(");\n");
     $compiler->outdent()->write("}\n\n");
 }
示例#5
0
文件: Type.php 项目: superdav42/Twig
 public function compile(Twig_Compiler $compiler)
 {
     if ($compiler->getEnvironment()->isStrictVariables()) {
         $name = $this->getAttribute('name');
         $type = $this->getAttribute('type');
         $compiler->addDebugInfo($this);
         if (0 === strcasecmp($type, 'array')) {
             $compiler->write("if (false === is_array(\$context['{$name}'])) {\n");
         } else {
             $compiler->write("if (false === \$context['{$name}'] instanceof {$type}) {\n");
         }
         $compiler->indent()->write("throw new Twig_Error_Runtime('variable \\'{$name}\\' is expected to be of type {$type} but '.get_class(\$context['{$name}']).' was provided.', {$this->getLine()}, '{$compiler->getFilename()}');\n")->outdent()->write("}\n");
     }
 }
示例#6
0
文件: Component.php 项目: h4kbas/Just
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler $compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $typeis = $this->getNode('typeis')->getAttribute('value');
     //dd($this->getNode('param')->getAttribute('value'));
     if ($typeis == 'repeat') {
         $compiler->write("\$context['_seq'] = ")->subcompile($this->getNode('sequence'))->write(";\n")->write("foreach (\$context['_seq'] as ")->write("\$context['" . $this->getNode('key')->getAttribute('value') . "']")->raw(' => ')->write("\$context['" . $this->getNode('value')->getAttribute('value') . "']")->raw(") {\n")->indent()->subcompile($this->getNode('body'))->outdent()->write("}\n");
     } else {
         if ($typeis == 'if') {
             $compiler->write('if (')->subcompile($this->getNode('param'))->write("){\n")->indent()->subcompile($this->getNode('body'))->outdent()->write("}\n");
         } else {
             $compiler->indent()->addDebugInfo($this)->write('$context = array_merge($context, ')->subcompile($this->getNode('variables'))->write(");\n")->subcompile($this->getNode('body'))->outdent();
         }
     }
     //echo $compiler->getSource();exit;
 }
示例#7
0
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write("{\n")->indent();
     // Generate the variants
     $compiler->write("\$ab_variants = [\n");
     $compiler->indent();
     foreach ($this->getNode('variants') as $k => $v) {
         $compiler->write("[\n");
         $compiler->indent()->write("'name' => '{$k}',\n");
         $compiler->write("'value' => function(){\n");
         $compiler->subcompile($v);
         $compiler->write("}\n");
         $compiler->outdent()->write("],\n");
     }
     $compiler->outdent()->write("];\n\n");
     $compiler->write("\$ab_selected_test = null;\n");
     // Generate the selector method
     $compiler->write("if (isset(\$_GET['__ab_" . $this->test_name . "'])) {\n");
     $compiler->indent()->write("foreach (\$ab_variants as \$ab_variant_iteration) {\n");
     $compiler->indent()->write("if (\$ab_variant_iteration['name'] === \$_GET['__ab_" . $this->test_name . "']) {\n");
     $compiler->indent()->write("\$ab_selected_test = \$ab_variant_iteration;\n");
     $compiler->write("break;\n");
     $compiler->outdent()->write("}\n");
     $compiler->outdent()->write("}\n");
     $compiler->outdent()->write("}\n\n");
     $compiler->write("if (!isset(\$ab_selected_test)) {\n");
     $compiler->write("\$ip = isset(\$_SERVER['HTTP_X_REAL_IP']) ? \$_SERVER['HTTP_X_REAL_IP'] : \$_SERVER['REMOTE_ADDR'];");
     $compiler->indent()->write("\$ab_random_seed = \$ip . count(\$ab_variants) . '" . $this->test_name . "';\n");
     $compiler->write("\$ab_hash_seed = substr(md5(\$ab_random_seed), 0, 6);\n");
     $compiler->write("\$ab_int_seed = intval(hexdec(\$ab_hash_seed));\n");
     $compiler->write("mt_srand(\$ab_int_seed);\n");
     $compiler->write("\$ab_selected_test_id = mt_rand(0, count(\$ab_variants) - 1);\n");
     $compiler->write("\$ab_selected_test = \$ab_variants[\$ab_selected_test_id];\n");
     $compiler->outdent()->write("}\n\n");
     // Set the global list of enrolled tests
     $compiler->write("global \$ab_enrolled_tests;\n");
     $compiler->write("if (!isset(\$ab_enrolled_tests)) {\n");
     $compiler->indent()->write("\$ab_enrolled_tests = [];\n");
     $compiler->outdent()->write("}\n");
     $compiler->write("\$ab_enrolled_tests['" . $this->test_name . "'] = \$ab_selected_test['name'];\n\n");
     // Execute the variant
     $compiler->write("\$ab_selected_test['value']();\n");
     $compiler->outdent()->write("}");
 }
示例#8
0
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write('switch (')->subcompile($this->getNode('expression'))->raw(") {\n")->indent();
     /* @var $case \Twig_Node */
     foreach ($this->getNode('cases')->getIterator() as $key => $case) {
         $compiler->write('case ')->subcompile($case->getNode('expression'))->raw(":\n");
         if ($case->hasNode('body')) {
             $compiler->indent()->subcompile($case->getNode('body'));
         }
         if ($case->hasAttribute('break') && $case->getAttribute('break') == true) {
             $compiler->write("break;\n");
         }
         $compiler->outdent();
     }
     if ($this->hasNode('default') && $this->getNode('default') !== null) {
         $compiler->write('default')->raw(":\n")->indent()->subcompile($this->getNode('default'))->outdent();
     }
     $compiler->outdent()->write("}\n");
 }
示例#9
0
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write('switch(')->subcompile($this->getNode('expression'))->raw(") {\n")->indent();
     $total = count($this->getNode('cases'));
     for ($i = 0; $i < $total; $i++) {
         $expr = $this->getNode('cases')->getNode($i)->getAttribute('expression');
         $body = $this->getNode('cases')->getNode($i)->getNode('body');
         if (is_null($expr)) {
             $compiler->write('default')->raw(":\n");
         } else {
             foreach ($expr as $subExpr) {
                 $compiler->write('case ')->subcompile($subExpr)->raw(":\n");
             }
         }
         $compiler->indent();
         $compiler->subcompile($body);
         if ($i + 1 >= $total || !$this->getNode('cases')->getNode($i + 1)->getAttribute('fallthrough')) {
             $compiler->write("break;\n");
         }
         $compiler->outdent();
     }
     $compiler->outdent()->write('}');
 }
示例#10
0
 protected function compileConstructor(Twig_Compiler $compiler)
 {
     $compiler->write("public function __construct(Twig_Environment \$env)\n", "{\n")->indent()->subcompile($this->getNode('constructor_start'))->write("parent::__construct(\$env);\n\n");
     // parent
     if (null === ($parent = $this->getNode('parent'))) {
         $compiler->write("\$this->parent = false;\n\n");
     } elseif ($parent instanceof Twig_Node_Expression_Constant) {
         $compiler->addDebugInfo($parent)->write('$this->parent = $this->loadTemplate(')->subcompile($parent)->raw(', ')->repr($compiler->getFilename())->raw(', ')->repr($this->getNode('parent')->getLine())->raw(");\n");
     }
     $countTraits = count($this->getNode('traits'));
     if ($countTraits) {
         // traits
         foreach ($this->getNode('traits') as $i => $trait) {
             $node = $trait->getNode('template');
             $compiler->write(sprintf('$_trait_%s = $this->loadTemplate(', $i))->subcompile($node)->raw(', ')->repr($compiler->getFilename())->raw(', ')->repr($node->getLine())->raw(");\n");
             $compiler->addDebugInfo($trait->getNode('template'))->write(sprintf("if (!\$_trait_%s->isTraitable()) {\n", $i))->indent()->write("throw new Twig_Error_Runtime('Template \"'.")->subcompile($trait->getNode('template'))->raw(".'\" cannot be used as a trait.');\n")->outdent()->write("}\n")->write(sprintf("\$_trait_%s_blocks = \$_trait_%s->getBlocks();\n\n", $i, $i));
             foreach ($trait->getNode('targets') as $key => $value) {
                 $compiler->write(sprintf('if (!isset($_trait_%s_blocks[', $i))->string($key)->raw("])) {\n")->indent()->write("throw new Twig_Error_Runtime(sprintf('Block ")->string($key)->raw(' is not defined in trait ')->subcompile($trait->getNode('template'))->raw(".'));\n")->outdent()->write("}\n\n")->write(sprintf('$_trait_%s_blocks[', $i))->subcompile($value)->raw(sprintf('] = $_trait_%s_blocks[', $i))->string($key)->raw(sprintf(']; unset($_trait_%s_blocks[', $i))->string($key)->raw("]);\n\n");
             }
         }
         if ($countTraits > 1) {
             $compiler->write("\$this->traits = array_merge(\n")->indent();
             for ($i = 0; $i < $countTraits; ++$i) {
                 $compiler->write(sprintf('$_trait_%s_blocks' . ($i == $countTraits - 1 ? '' : ',') . "\n", $i));
             }
             $compiler->outdent()->write(");\n\n");
         } else {
             $compiler->write("\$this->traits = \$_trait_0_blocks;\n\n");
         }
         $compiler->write("\$this->blocks = array_merge(\n")->indent()->write("\$this->traits,\n")->write("array(\n");
     } else {
         $compiler->write("\$this->blocks = array(\n");
     }
     // blocks
     $compiler->indent();
     foreach ($this->getNode('blocks') as $name => $node) {
         $compiler->write(sprintf("'%s' => array(\$this, 'block_%s'),\n", $name, $name));
     }
     if ($countTraits) {
         $compiler->outdent()->write(")\n");
     }
     $compiler->outdent()->write(");\n")->outdent()->subcompile($this->getNode('constructor_end'))->write("}\n\n");
 }