Completed
Push — master ( 125148...6e4e8e )
by Timothy
16:08
created

View::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * Ion
4
 *
5
 * Building blocks for web development
6
 *
7
 * @package     Ion
8
 * @author      Timothy J. Warren
9
 * @copyright   Copyright (c) 2015
10
 * @license     MIT
11
 */
12
13
namespace Aviat\Ion;
14
15
use Aviat\Ion\Di\ContainerInterface;
16
use Aviat\Ion\Type\StringType;
17
18
/**
19
 * Base view response class
20
 */
21
abstract class View {
22
23
	use Di\ContainerAware;
24
	use \Aviat\Ion\StringWrapper;
25
26
	/**
27
	 * HTTP response Object
28
	 *
29
	 * @var Aura\Web\Response
30
	 */
31
	protected $response;
32
33
	/**
34
	 * Response mime type
35
	 *
36
	 * @var string
37
	 */
38
	protected $contentType = '';
39
40
	/**
41
	 * String of response to be output
42
	 *
43
	 * @var StringType
44
	 */
45
	protected $output;
46
47
	/**
48
	 * If the view has sent output via
49
	 * __toString or send method
50
	 *
51
	 * @var boolean
52
	 */
53
	protected $hasRendered = false;
54
55
	/**
56
	 * Constructor
57
	 *
58
	 * @param ContainerInterface $container
59
	 */
60
	public function __construct(ContainerInterface $container)
61
	{
62
		$this->setContainer($container);
63
		$this->response = $container->get('response');
64
	}
65
66
	/**
67
	 * Send output to client
68
	 */
69
	public function __destruct()
70
	{
71
		if ( ! $this->hasRendered)
72
		{
73
			$this->send();
74
		}
75
	}
76
77
	/**
78
	 * Return rendered output
79
	 *
80
	 * @return string
81
	 */
82
	public function __toString()
83
	{
84
		$this->hasRendered = true;
85
		return $this->getOutput();
86
	}
87
88
	/**
89
	 * Set the output string
90
	 *
91
	 * @param string $string
92
	 * @return View
93
	 */
94
	public function setOutput($string)
95
	{
96
		$this->output = $this->string($string);
97
98
		return $this;
99
	}
100
101
	/**
102
	 * Append additional output
103
	 *
104
	 * @param string $string
105
	 * @return View
106
	 */
107
	public function appendOutput($string)
108
	{
109
		$this->output = $this->string($this->output)->append($string);
110
111
		return $this;
112
	}
113
114
	/**
115
	 * Get the current output string
116
	 *
117
	 * @return string
118
	 */
119
	public function getOutput()
120
	{
121
		return $this->string($this->output)->__toString();
122
	}
123
124
	/**
125
	 * Send output to client
126
	 */
127
	public function send()
128
	{
129
		$this->hasRendered = true;
130
		$this->output();
131
	}
132
133
	/**
134
	 * Send the appropriate response
135
	 *
136
	 * @return void
137
	 */
138
	protected function output()
139
	{
140
		$content =& $this->response->content;
141
		$content->set($this->output);
142
		$content->setType($this->contentType);
143
		$content->setCharset('utf-8');
144
	}
145
}
146
// End of View.php