Permalink
Browse files

Added manipulation of specific Output stack levels

Signed-off-by: dchill42 <dchill42@gmail.com>
  • Loading branch information...
1 parent bbd3b98 commit e838382f4553cfa355a482b0a327ddd53b50704f @dchill42 committed Sep 27, 2012
Showing with 52 additions and 23 deletions.
  1. +44 −20 system/core/Output.php
  2. +8 −3 tests/codeigniter/core/Output_test.php
View
@@ -132,12 +132,20 @@ public function __construct()
*
* Returns the current output string
*
- * @param bool Get all flag
+ * @param mixed Optional level (FALSE = current, TRUE = all)
* @return string
*/
- public function get_output($all = FALSE)
+ public function get_output($level = FALSE)
{
- return ($all ? implode($this->final_output) : end($this->final_output));
+ // Check for all flag
+ if ($level === TRUE)
+ {
+ // Return entire stack
+ return implode($this->final_output);
+ }
+
+ // Return specified level (or current)
+ return $this->final_output[$this->_get_index($level)];
}
// --------------------------------------------------------------------
@@ -148,23 +156,21 @@ public function get_output($all = FALSE)
* Sets the output string
*
* @param string Output
- * @param bool Overwrite all flag
- * @return void
+ * @param mixed Optional level (FALSE = current, TRUE = all)
+ * @return object This
*/
- public function set_output($output, $all = FALSE)
+ public function set_output($output, $level = FALSE)
{
// Check for all flag
- if ($all)
+ if ($level === TRUE)
{
// Reset stack to one level with output
$this->final_output = array($output);
}
else
{
- // Set buffer contents for current buffer in stack
- // Note: stack_pop() prevents emptying the array, so count will always be >= 1
- $level = count($this->final_output) - 1;
- $this->final_output[$level] = $output;
+ // Set buffer contents to stack
+ $this->final_output[$this->_get_index($level)] = $output;
}
return $this;
@@ -177,17 +183,33 @@ public function set_output($output, $all = FALSE)
*
* Appends data onto the output string
*
- * @param string
- * @return void
+ * @param string Output to append
+ * @param mixed Optional stack level (FALSE = current)
+ * @return object This
*/
- public function append_output($output)
+ public function append_output($output, $level = FALSE)
{
- // Append output to current buffer in stack
+ // Append output to stack
+ $this->final_output[$this->_get_index($level)] .= $output;
+ return $this;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Get stack index from level number
+ *
+ * @param mixed Level number (or FALSE for current)
+ * @return int Stack index
+ */
+ protected function _get_index($level)
+ {
+ // Get top level
// Note: stack_pop() prevents emptying the array, so count will always be >= 1
- $level = count($this->final_output) - 1;
- $this->final_output[$level] .= $output;
+ $top = count($this->final_output);
- return $this;
+ // Return level if valid, otherwise top
+ return (is_int($level) && $level > 0 && $level <= $top) ? $level - 1 : $top - 1;
}
// --------------------------------------------------------------------
@@ -243,8 +265,10 @@ public function stack_pop()
return array_pop($this->final_output);
}
- // Nothing to pop - just return contents of bottom buffer
- return $this->final_output[0];
+ // Nothing to pop - just return contents and empty bottom buffer
+ $out = $this->final_output[0];
+ $this->final_output[0] = '';
+ return $out;
}
// --------------------------------------------------------------------
@@ -27,7 +27,7 @@ public function set_up()
*/
public function test_output()
{
- // Do we start at level 1?
+ // Do we start at level 0?
$this->assertEquals(1, $this->output->stack_level());
// Append output and check it
@@ -56,17 +56,22 @@ public function test_output()
$this->assertEquals(1, $this->output->stack_level());
$this->assertEquals($out1, $this->output->get_output(TRUE));
- // Make sure pop doesn't remove first level
+ // Make sure pop empties but doesn't remove first level
$this->assertEquals($out1, $this->output->stack_pop());
$this->assertEquals(1, $this->output->stack_level());
+ $this->assertEquals('', $this->output->get_output());
// Add another
$out2 = 'Second hand ';
$this->output->stack_push($out2);
$this->assertEquals(2, $this->output->stack_level());
+ $this->assertEquals($out2, $this->output->get_output(TRUE));
+
+ // Restore first level
+ $this->output->set_output($out1, 1);
$this->assertEquals($out1.$out2, $this->output->get_output(TRUE));
- // ...and remove it
+ // Remove second level
$this->assertEquals($out2, $this->output->stack_pop());
$this->assertEquals(1, $this->output->stack_level());
$this->assertEquals($out1, $this->output->get_output(TRUE));

0 comments on commit e838382

Please sign in to comment.