Example #1
0
 /**
  * @param Context $context
  *
  * @return mixed
  */
 public function render($context)
 {
     /** @var $block_context BlockContext */
     $block_context = null;
     if (isset($context->render_context[BLOCK_CONTEXT_KEY])) {
         $block_context = $context->render_context[BLOCK_CONTEXT_KEY];
     }
     $context->push();
     if ($block_context === null) {
         $context['block'] = $this;
         $result = $this->nodelist->render($context);
     } else {
         $push = $block = $block_context->pop($this->name);
         if ($block === null) {
             $block = $this;
         }
         // Create new block so we can store context without thread-safety issues.
         $block = new BlockNode($block->name, $block->nodelist);
         $block->context = $context;
         $context['block'] = $block;
         $result = $block->nodelist->render($context);
         if ($push !== null) {
             $block_context->push($this->name, $push);
         }
     }
     $context->pop();
     return $result;
 }
Example #2
0
 public function testAddItemInInnerScope()
 {
     $this->context->push();
     $this->context->set('test', 'test');
     $this->assertEquals('test', $this->context->get('test'));
     $this->context->pop();
     $this->assertEquals(null, $this->context->get('test'));
 }
Example #3
0
 public function test_stack()
 {
     $context = new Context();
     $this->assertSame([], $context->to_array());
     $context->push();
     $context['one'] = 1;
     $this->assertSame(['one' => 1], $context->to_array());
     $context->push();
     $context['two'] = 2;
     $this->assertSame(['one' => 1, 'two' => 2], $context->to_array());
     $context->push();
     $context['three'] = 3;
     $this->assertSame(['one' => 1, 'two' => 2, 'three' => 3], $context->to_array());
     $context->pop();
     $this->assertSame(['one' => 1, 'two' => 2], $context->to_array());
     $context->pop();
     $this->assertSame(['one' => 1], $context->to_array());
     $context->pop();
     $this->assertSame([], $context->to_array());
 }
Example #4
0
 public function test_context()
 {
     $c = new Context(array('a' => 1, 'b' => 'xyzzy'));
     $this->assertEquals($c['a'], 1);
     $this->assertEquals($c->push(), array());
     $c['a'] = 2;
     $this->assertEquals($c['a'], 2);
     $this->assertEquals($c->get('a'), 2);
     $this->assertEquals($c->pop(), array('a' => 2));
     $this->assertEquals($c['a'], 1);
     $this->assertEquals($c->get('foo', 42), 42);
 }
Example #5
0
 public function testContainsTellsYouIfAGivenContextIsInTheCurrentStack()
 {
     $context = new Context();
     $context->push('foo');
     $context->push('bar');
     $context->push('baz');
     $this->assertTrue($context->contains('foo'));
     $this->assertTrue($context->contains('bar'));
     $this->assertTrue($context->contains('baz'));
     $popped = $context->pop();
     $this->assertFalse($context->contains($popped));
     // TODO: remove once global state is fully deprecated (2.0)
     _elgg_services()->setValue('context', new Context());
     elgg_push_context('foo');
     elgg_push_context('bar');
     elgg_push_context('baz');
     $this->assertTrue(elgg_in_context('foo'));
     $this->assertTrue(elgg_in_context('bar'));
     $this->assertTrue(elgg_in_context('baz'));
     $popped = elgg_pop_context();
     $this->assertFalse(elgg_in_context($popped));
 }
Example #6
0
 public function testContext()
 {
     $server = $this->getMockBuilder('Deployer\\Server\\ServerInterface')->disableOriginalConstructor()->getMock();
     $env = $this->getMockBuilder('Deployer\\Server\\Environment')->disableOriginalConstructor()->getMock();
     $input = $this->getMockBuilder('Symfony\\Component\\Console\\Input\\InputInterface')->disableOriginalConstructor()->getMock();
     $output = $this->getMockBuilder('Symfony\\Component\\Console\\Output\\OutputInterface')->disableOriginalConstructor()->getMock();
     $context = new Context($server, $env, $input, $output);
     $this->assertInstanceOf('Deployer\\Server\\ServerInterface', $context->getServer());
     $this->assertInstanceOf('Deployer\\Server\\Environment', $context->getEnvironment());
     $this->assertInstanceOf('Symfony\\Component\\Console\\Input\\InputInterface', $context->getInput());
     $this->assertInstanceOf('Symfony\\Component\\Console\\Output\\OutputInterface', $context->getOutput());
     Context::push($context);
     $this->assertEquals($context, Context::get());
     $this->assertEquals($context, Context::pop());
 }
Example #7
0
 /**
  * Run task.
  *
  * @param Context $context
  */
 public function run(Context $context)
 {
     Context::push($context);
     $env = $context->getEnvironment();
     // Save cd's working_path path.
     if ($env !== null) {
         $workingPath = $env->get('working_path', false);
     }
     // Call tasks.
     call_user_func($this->callback);
     // Restore cd's working_path path.
     if ($env !== null && isset($workingPath)) {
         $env->set('working_path', $workingPath);
     }
     Context::pop();
 }
Example #8
0
    /**
     * Process Mustache section style
     *
     * @param Context $context current context
     * @param array   $current section node data
     *
     * @throws \RuntimeException
     * @return mixed|string
     */
    private function _mustacheStyleSection(Context $context, $current)
    {
        $sectionName = $current[Tokenizer::NAME];

        // fallback to mustache style each/with/for just if there is
        // no argument at all.
        try {
            $sectionVar = $context->get($sectionName, true);
        } catch (\InvalidArgumentException $e) {
            throw new \RuntimeException(
                $sectionName . ' is not registered as a helper'
            );
        }
        $buffer = '';
        if (is_array($sectionVar) || $sectionVar instanceof \Traversable) {
            $isList = is_array($sectionVar) &&
                (array_keys($sectionVar) === range(0, count($sectionVar) - 1));
            $index = 0;
            $lastIndex = $isList ? (count($sectionVar) - 1) : false;

            foreach ($sectionVar as $key => $d) {
                $specialVariables = array(
                    '@index' => $index,
                    '@first' => ($index === 0),
                    '@last' => ($index === $lastIndex),
                );
                if (!$isList) {
                    $specialVariables['@key'] = $key;
                }
                $context->pushSpecialVariables($specialVariables);
                $context->push($d);
                $buffer .= $this->render($context);
                $context->pop();
                $context->popSpecialVariables();
                $index++;
            }
        } elseif (is_object($sectionVar)) {
            //Act like with
            $context->push($sectionVar);
            $buffer = $this->render($context);
            $context->pop();
        } elseif ($sectionVar) {
            $buffer = $this->render($context);
        }

        return $buffer;
    }
Example #9
0
 public function testPushDoesNotAlterCase()
 {
     $context = new Context();
     $context->push("HELLO");
     $this->assertEquals("HELLO", $context->peek());
 }
Example #10
0
 /**
  * @param Context $context
  * @return SafeString|string
  * @throws Exception|TypeError|VariableDoesNotExist
  * @throws RuntimeError
  */
 public function render($context)
 {
     if (isset($context['forloop'])) {
         $parentloop = $context['forloop'];
     } else {
         $parentloop = array();
     }
     $context->push();
     try {
         $values = $this->sequence->resolve($context, True);
     } catch (VariableDoesNotExist $e) {
         $values = array();
     }
     if ($values === null) {
         $values = array();
     } elseif (is_string($values)) {
         // We convert a string to array to make it iterable.
         $values = str_split($values);
         // TODO Check unicode handling.
     }
     if (!is_array($values) && !$values instanceof Iterator) {
         throw new RuntimeError('Noniterable "' . $values . '" is passed to for loop.');
     }
     $len_values = count($values);
     if ($len_values < 1) {
         $context->pop();
         return $this->nodelist_empty->render($context);
     }
     $nodelist = new NodeList();
     if ($this->is_reversed) {
         $values_ = $values;
         krsort($values_);
         $values = $values_;
         unset($values_);
     }
     $unpack = count($this->loopvars) > 1;
     // Create a forloop value in the context.  We'll update counters on each iteration just below.
     $loop_dict = $context['forloop'] = array('parentloop' => $parentloop);
     foreach ($values as $i => $item) {
         // Shortcuts for current loop iteration number.
         $loop_dict['counter0'] = $i;
         $loop_dict['counter'] = $i + 1;
         // Reverse counter iteration numbers.
         $loop_dict['revcounter'] = $len_values - $i;
         $loop_dict['revcounter0'] = $len_values - $i - 1;
         // Boolean values designating first and last times through loop.
         $loop_dict['first'] = $i == 0;
         $loop_dict['last'] = $i == $len_values - 1;
         $context['forloop'] = array_merge($context['forloop'], $loop_dict);
         $pop_context = False;
         if ($unpack) {
             // If there are multiple loop variables, unpack the item into them.
             $success_ = True;
             try {
                 $unpacked_vars = py_zip($this->loopvars, $item);
             } catch (TypeError $e) {
                 $success_ = False;
             }
             if ($success_) {
                 $pop_context = True;
                 $context->update($unpacked_vars);
             }
         } else {
             $context[$this->loopvars[0]] = $item;
         }
         // In TEMPLATE_DEBUG mode provide source of the node which actually raised the exception
         if (Dja::getSetting('TEMPLATE_DEBUG')) {
             foreach ($this->nodelist_loop as $node) {
                 /** @var $node Node */
                 try {
                     $nodelist[] = $node->render($context);
                 } catch (Exception $e) {
                     if (!py_hasattr($e, 'django_template_source')) {
                         $e->django_template_source = $node->source;
                     }
                     throw $e;
                 }
             }
         } else {
             foreach ($this->nodelist_loop as $node) {
                 $nodelist[] = $node->render($context);
             }
         }
         if ($pop_context) {
             /*
              * The loop variables were pushed on to the context so pop them
              * off again. This is necessary because the tag lets the length
              * of loopvars differ to the length of each set of items and we
              * don't want to leave any vars from the previous loop on the
              * context.
              */
             $context->pop();
         }
     }
     $context->pop();
     return $nodelist->render($context);
 }
Example #11
0
 public function testFailToSetEmptyContext()
 {
     $context = new Context();
     $context->set("  ");
     $this->assertNull($context->peek());
     $this->assertNull($context->pop());
     $context->push("  ");
     $this->assertEquals("  ", $context->peek());
     $this->assertEquals("  ", $context->pop());
 }