-
Notifications
You must be signed in to change notification settings - Fork 1
/
oop_closure_view.php
251 lines (213 loc) · 5.66 KB
/
oop_closure_view.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
//(MyApplication/ViewInterface.php)
namespace MyApplication;
interface ViewInterface
{
/**
* Set the view template file
*/
public function setTemplateFile($templateFile);
/**
* Get the view template file
*/
public function getTemplateFile();
/**
* Add a new field to the view
*/
public function set($name, $value);
/**
* Get the given field from the view
*/
public function get($name);
/**
* Check if the given view field exists
*/
public function exists($name);
/**
* Remove the given field from the view
*/
public function remove($name);
/**
* Render the view template file
*/
public function render();
}
//Read more at http://www.devshed.com/c/a/PHP/Using-PHP-Closures-as-View-Helpers/#1YlhQ6GmEYUW2v6V.99
?>
<?php
//(MyApplication/DataHandlerInterface.php)
namespace MyApplication;
interface DataHandlerInterface
{
/**
* Write data to the storage
*/
public function write($data);
/**
* Read data from the storage
*/
public function read();
}
//Read more at http://www.devshed.com/c/a/PHP/Using-PHP-Closures-as-View-Helpers/#1YlhQ6GmEYUW2v6V.99
?>
<?php
//(MyApplication/View.php)
namespace MyApplication;
class View implements ViewInterface
{
const DEFAULT_TEMPLATE_FILE = 'default_template.php';
protected $_fields = array();
protected $_templateFile;
/**
* Constructor
*/
public function __construct(array $fields = array(), $templateFile = self::DEFAULT_TEMPLATE_FILE)
{
// optionally populate the view with an array of values
if (!empty($fields)) {
foreach ($fields as $field => $value) {
$this->$field = $value;
}
}
$this->setTemplateFile($templateFile);
}
/**
* Set the view template file
*/
public function setTemplateFile($templateFile)
{
if (!file_exists($templateFile) || !is_readable($templateFile)) {
throw new InvalidArgumentException('The specified template file ' . $templateFile . ' is invalid.');
}
$this->_templateFile = $templateFile;
return $this;
}
/**
* Get the view template file
*/
public function getTemplateFile()
{
return $this->_templateFile;
}
/**
* Reset the template file to the default one
*/
public function resetTemplateFile()
{
$this->_templateFile = self::DEFAULT_TEMPLATE_FILE;
return $this;
}
/**
* Assign a field to the view
*/
public function set($name, $value)
{
return $this->__set($name, $value);
}
/**
* Get the given field from the view
*/
public function get($name)
{
return $this->__get($name);
}
/**
* Check if the given field has been assigned to the view
*/
public function exists($name)
{
return isset($this->_fields[$name]);
}
/**
* Remove the given field from the view
*/
public function remove($name)
{
if (isset($this->_fields[$name])) {
unset($this->_fields[$name]);
}
return $this;
}
/**
* Render the template file
*/
public function render()
{
ob_start();
include $this->_templateFile;
return ob_get_clean();
}
/**
* Assign the given field to the view via the '__set()' magic method
*/
public function __set($name, $value)
{
$this->_fields[$name] = $value;
return $this;
}
/**
* Get the given field from the view via the '__get()' magic method
*/
public function __get($name)
{
if (!isset($this->_fields[$name])) {
throw new InvalidArgumentException('The specified view field ' . $name . ' does not exist.');
}
return (is_callable($this->_fields[$name]))
? $this->_fields[$name]($this)
: $this->_fields[$name];
}
}
//Read more at http://www.devshed.com/c/a/PHP/Using-PHP-Closures-as-View-Helpers/1/#C7CtivrdAw7UzR4V.99
?>
<?php //(default_template.php)?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Using closures in PHP</title>
</head>
<body>
<header>
<h1>Header section</h1>
<h2>Welcome! You're accessing this page from : <?php echo $this->clientIp;?></h2>
<p><?php echo $this->header;?></p>
</header>
<section>
<h2>Main section</h2>
<p><?php echo $this->content;?></p>
</section>
<footer>
<h2>Footer section</h2>
<p><?php echo $this->footer;?></p>
</footer>
</body>
</html>
<?php
//Read more at http://www.devshed.com/c/a/PHP/Using-PHP-Closures-as-View-Helpers/1/#C7CtivrdAw7UzR4V.99
?>
<?php
use MyApplicationView;
// include the autoloader and create an instance of it
require_once __DIR__ . '/Autoloader.php';
$autoloader = new Autoloader;
// create a view object and assign some properties to it
$view = new View;
$view->header = 'This is the content of the header section';
$view->content = 'This is the content of the main section';
$view->footer = 'This is the content of the footer section';
$view->clientIp = function() {
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
return $_SERVER['HTTP_CLIENT_IP'];
}
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else {
return $_SERVER['REMOTE_ADDR'];
}
};
// render the view template
echo $view->render();
//Read more at http://www.devshed.com/c/a/PHP/Using-PHP-Closures-as-View-Helpers/1/#C7CtivrdAw7UzR4V.99
?>