Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added option to return sub-controller output

  • Loading branch information...
commit 296eecc881366fac440fe0894772bd893bff5b8c 1 parent 84a15a6
Darren Hill authored
26  system/core/CodeIgniter.php
@@ -116,9 +116,10 @@ public function is_callable($class, $method)
116 116
 	 * @param	string	method
117 117
 	 * @param	array	arguments
118 118
 	 * @param	string	optional object name
119  
-	 * @return	boolean	TRUE on success, otherwise FALSE
  119
+	 * @param	bool	TRUE to return output
  120
+	 * @return	mixed	Output if $return, TRUE on success, otherwise FALSE
120 121
 	 */
121  
-	public function call_controller($class, $method, array $args = array(), $name = '')
  122
+	public function call_controller($class, $method, array $args = array(), $name = '', $return = FALSE)
122 123
 	{
123 124
 		// Default name if not provided
124 125
 		if (empty($name))
@@ -133,15 +134,23 @@ public function call_controller($class, $method, array $args = array(), $name =
133 134
 			// Check for _remap
134 135
 			if ($this->is_callable($class, '_remap'))
135 136
 			{
136  
-				// Call _remap
  137
+				// Call _remap, capturing output if requested
  138
+				if ($return)
  139
+				{
  140
+					$this->output->fork_output();
  141
+				}
137 142
 				$this->$name->_remap($method, $args);
138  
-				return TRUE;
  143
+				return $return ? $this->output->get_forked() : TRUE;
139 144
 			}
140 145
 			else if ($this->is_callable($class, $method))
141 146
 			{
142  
-				// Call method
  147
+				// Call method, capturing output if requested
  148
+				if ($return)
  149
+				{
  150
+					$this->output->fork_output();
  151
+				}
143 152
 				call_user_func_array(array(&$this->$name, $method), $args);
144  
-				return TRUE;
  153
+				return $return ? $this->output->get_forked() : TRUE;
145 154
 			}
146 155
 		}
147 156
 
@@ -199,11 +208,6 @@ public static function &instance()
199 208
  * @var string
200 209
  *
201 210
  */
202  
-	/**
203  
-	 * CodeIgniter Version
204  
-	 *
205  
-	 * @var string
206  
-	 */
207 211
 	define('CI_VERSION', '2.1.0-dev');
208 212
 
209 213
 /*
9  system/core/Loader.php
@@ -249,10 +249,11 @@ public function library($library = '', $params = NULL, $object_name = NULL)
249 249
 	 * @access	public
250 250
 	 * @param	string	the name of the class
251 251
 	 * @param	string	name for the controller
252  
-	 * @param	boolean	FALSE to skip calling controller method
253  
-	 * @return	boolean TRUE on success, otherwise FALSE
  252
+	 * @param	bool	FALSE to skip calling controller method
  253
+	 * @param	bool	TRUE to return output (depends on $call == TRUE)
  254
+	 * @return	mixed	Output if $return, TRUE on success, otherwise FALSE
254 255
 	 */
255  
-	public function controller($route, $name = NULL, $call = TRUE)
  256
+	public function controller($route, $name = NULL, $call = TRUE, $return = FALSE)
256 257
 	{
257 258
 		// Check for missing class
258 259
 		if (empty($route))
@@ -352,7 +353,7 @@ public function controller($route, $name = NULL, $call = TRUE)
352 353
 		// Call method unless disabled
353 354
 		if ($call)
354 355
 		{
355  
-			return $this->CI->call_controller($class, $method, $route, $name);
  356
+			return $this->CI->call_controller($class, $method, $route, $name, $return);
356 357
 		}
357 358
 
358 359
 		return TRUE;
96  system/core/Output.php
@@ -41,7 +41,23 @@ class CI_Output {
41 41
 	 * @var		string
42 42
 	 * @access	protected
43 43
 	 */
44  
-	protected $final_output;
  44
+	protected $final_output		= '';
  45
+
  46
+	/**
  47
+	 * Forked output string
  48
+	 *
  49
+	 * @var		string
  50
+	 * @access	protected
  51
+	 */
  52
+	protected $forked_output	= '';
  53
+
  54
+	/**
  55
+	 * Forked output flag
  56
+	 *
  57
+	 * @var		bool
  58
+	 * @access	protected
  59
+	 */
  60
+	protected $forked			= FALSE;
45 61
 
46 62
 	/**
47 63
 	 * Cache expiration time
@@ -70,7 +86,7 @@ class CI_Output {
70 86
 	/**
71 87
 	 * Determines wether profiler is enabled
72 88
 	 *
73  
-	 * @var		book
  89
+	 * @var		bool
74 90
 	 * @access	protected
75 91
 	 */
76 92
 	protected $enable_profiler	= FALSE;
@@ -102,7 +118,7 @@ class CI_Output {
102 118
 	/**
103 119
 	 * Constructor
104 120
 	 */
105  
-	function __construct()
  121
+	public function __construct()
106 122
 	{
107 123
 		// Get parent reference
108 124
 		$this->CI =& CodeIgniter::instance();
@@ -129,7 +145,7 @@ function __construct()
129 145
 	 * @access	public
130 146
 	 * @return	string
131 147
 	 */
132  
-	function get_output()
  148
+	public function get_output()
133 149
 	{
134 150
 		return $this->final_output;
135 151
 	}
@@ -145,9 +161,16 @@ function get_output()
145 161
 	 * @param	string
146 162
 	 * @return	void
147 163
 	 */
148  
-	function set_output($output)
  164
+	public function set_output($output)
149 165
 	{
150  
-		$this->final_output = $output;
  166
+		if ($this->forked)
  167
+		{
  168
+			$this->forked_output = $output;
  169
+		}
  170
+		else
  171
+		{
  172
+			$this->final_output = $output;
  173
+		}
151 174
 
152 175
 		return $this;
153 176
 	}
@@ -163,11 +186,11 @@ function set_output($output)
163 186
 	 * @param	string
164 187
 	 * @return	void
165 188
 	 */
166  
-	function append_output($output)
  189
+	public function append_output($output)
167 190
 	{
168  
-		if ($this->final_output == '')
  191
+		if ($this->forked)
169 192
 		{
170  
-			$this->final_output = $output;
  193
+			$this->forked_output .= $output;
171 194
 		}
172 195
 		else
173 196
 		{
@@ -180,6 +203,43 @@ function append_output($output)
180 203
 	// --------------------------------------------------------------------
181 204
 
182 205
 	/**
  206
+	 * Fork Output
  207
+	 *
  208
+	 * Redirects output to the forked output string until get_fork() is called
  209
+	 *
  210
+	 * @access	public
  211
+	 * @return	void
  212
+	 */
  213
+	public function fork_output()
  214
+	{
  215
+		// Set forked flag and clear forked output
  216
+		$this->forked = TRUE;
  217
+		$this->forked_output = '';
  218
+	}
  219
+
  220
+	// --------------------------------------------------------------------
  221
+
  222
+	/**
  223
+	 * Get Forked Output
  224
+	 *
  225
+	 * Returns forked output and ends forking, unless $continue is TRUE
  226
+	 * In order to capture the forked output and continue with an empty buffer, call:
  227
+	 *	$this->CI->output->get_forked(TRUE);
  228
+	 *	$this->CI->output->set_output('');
  229
+	 *
  230
+	 * @access	public
  231
+	 * @param	bool	TRUE to continue fork
  232
+	 * @return	string
  233
+	 */
  234
+	public function get_forked($continue = FALSE)
  235
+	{
  236
+		$this->forked = $continue;
  237
+		return $this->forked_output;
  238
+	}
  239
+
  240
+	// --------------------------------------------------------------------
  241
+
  242
+	/**
183 243
 	 * Set Header
184 244
 	 *
185 245
 	 * Lets you set a server header which will be outputted with the final display.
@@ -192,7 +252,7 @@ function append_output($output)
192 252
 	 * @param 	bool
193 253
 	 * @return	void
194 254
 	 */
195  
-	function set_header($header, $replace = TRUE)
  255
+	public function set_header($header, $replace = TRUE)
196 256
 	{
197 257
 		// If zlib.output_compression is enabled it will compress the output,
198 258
 		// but it will not modify the content-length header to compensate for
@@ -217,7 +277,7 @@ function set_header($header, $replace = TRUE)
217 277
 	 * @param	string	extension of the file we're outputting
218 278
 	 * @return	void
219 279
 	 */
220  
-	function set_content_type($mime_type)
  280
+	public function set_content_type($mime_type)
221 281
 	{
222 282
 		if (strpos($mime_type, '/') === FALSE)
223 283
 		{
@@ -253,7 +313,7 @@ function set_content_type($mime_type)
253 313
 	 * @param	string
254 314
 	 * @return	void
255 315
 	 */
256  
-	function set_status_header($code = 200, $text = '')
  316
+	public function set_status_header($code = 200, $text = '')
257 317
 	{
258 318
 		set_status_header($code, $text);
259 319
 
@@ -269,7 +329,7 @@ function set_status_header($code = 200, $text = '')
269 329
 	 * @param	bool
270 330
 	 * @return	void
271 331
 	 */
272  
-	function enable_profiler($val = TRUE)
  332
+	public function enable_profiler($val = TRUE)
273 333
 	{
274 334
 		$this->enable_profiler = (is_bool($val)) ? $val : TRUE;
275 335
 
@@ -287,7 +347,7 @@ function enable_profiler($val = TRUE)
287 347
 	 * @param	array
288 348
 	 * @return	void
289 349
 	 */
290  
-	function set_profiler_sections($sections)
  350
+	public function set_profiler_sections($sections)
291 351
 	{
292 352
 		foreach ($sections as $section => $enable)
293 353
 		{
@@ -306,7 +366,7 @@ function set_profiler_sections($sections)
306 366
 	 * @param	integer
307 367
 	 * @return	void
308 368
 	 */
309  
-	function cache($time)
  369
+	public function cache($time)
310 370
 	{
311 371
 		$this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
312 372
 
@@ -330,7 +390,7 @@ function cache($time)
330 390
 	 * @param 	string
331 391
 	 * @return	mixed
332 392
 	 */
333  
-	function _display($output = '')
  393
+	public function _display($output = '')
334 394
 	{
335 395
 		// Set the output data
336 396
 		if ($output == '')
@@ -454,7 +514,7 @@ function _display($output = '')
454 514
 	 * @param 	string
455 515
 	 * @return	void
456 516
 	 */
457  
-	function _write_cache($output)
  517
+	public function _write_cache($output)
458 518
 	{
459 519
 		$path = $this->CI->config->item('cache_path');
460 520
 
@@ -506,7 +566,7 @@ function _write_cache($output)
506 566
 	 * @param 	object	uri class
507 567
 	 * @return	void
508 568
 	 */
509  
-	function _display_cache(&$CFG, &$URI)
  569
+	public function _display_cache(&$CFG, &$URI)
510 570
 	{
511 571
 		$cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path');
512 572
 

0 notes on commit 296eecc

Please sign in to comment.
Something went wrong with that request. Please try again.