Response.php 2.05 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7 8 9 10 11 12 13
 * @license http://www.yiiframework.com/license/
 */

namespace yii\base;

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
14
class Response extends Component
Qiang Xue committed
15
{
Qiang Xue committed
16 17 18
	/**
	 * @event Event an event raised when the application begins to generate the response.
	 */
Qiang Xue committed
19
	const EVENT_BEGIN_RESPONSE = 'beginResponse';
Qiang Xue committed
20 21 22
	/**
	 * @event Event an event raised when the generation of the response finishes.
	 */
Qiang Xue committed
23 24
	const EVENT_END_RESPONSE = 'endResponse';

Alexander Makarov committed
25 26 27
	/**
	 * Starts output buffering
	 */
Qiang Xue committed
28
	public function beginBuffer()
Qiang Xue committed
29 30 31 32 33
	{
		ob_start();
		ob_implicit_flush(false);
	}

Alexander Makarov committed
34
	/**
Qiang Xue committed
35
	 * Returns contents of the output buffer and stops the buffer.
Alexander Makarov committed
36 37
	 * @return string output buffer contents
	 */
Qiang Xue committed
38
	public function endBuffer()
Qiang Xue committed
39 40 41 42
	{
		return ob_get_clean();
	}

Alexander Makarov committed
43 44 45 46
	/**
	 * Returns contents of the output buffer
	 * @return string output buffer contents
	 */
Qiang Xue committed
47
	public function getBuffer()
Qiang Xue committed
48 49 50 51
	{
		return ob_get_contents();
	}

Alexander Makarov committed
52 53
	/**
	 * Discards the output buffer
Qiang Xue committed
54
	 * @param boolean $all if true, it will discards all output buffers.
Alexander Makarov committed
55
	 */
Qiang Xue committed
56
	public function cleanBuffer($all = true)
Qiang Xue committed
57 58 59 60 61 62 63 64 65 66 67
	{
		if ($all) {
			for ($level = ob_get_level(); $level > 0; --$level) {
				if (!@ob_end_clean()) {
					ob_clean();
				}
			}
		} else {
			ob_end_clean();
		}
	}
Qiang Xue committed
68

Qiang Xue committed
69 70 71 72 73 74 75
	/**
	 * Begins generating the response.
	 * This method is called at the beginning of [[Application::run()]].
	 * The default implementation will trigger the [[EVENT_BEGIN_RESPONSE]] event.
	 * If you overwrite this method, make sure you call the parent implementation so that
	 * the event can be triggered.
	 */
Qiang Xue committed
76 77 78 79 80
	public function begin()
	{
		$this->trigger(self::EVENT_BEGIN_RESPONSE);
	}

Qiang Xue committed
81 82 83 84 85 86 87
	/**
	 * Ends generating the response.
	 * This method is called at the end of [[Application::run()]].
	 * The default implementation will trigger the [[EVENT_END_RESPONSE]] event.
	 * If you overwrite this method, make sure you call the parent implementation so that
	 * the event can be triggered.
	 */
Qiang Xue committed
88 89 90 91
	public function end()
	{
		$this->trigger(self::EVENT_END_RESPONSE);
	}
Qiang Xue committed
92
}