-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewer.php
executable file
·194 lines (178 loc) · 6.71 KB
/
Viewer.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
<?php
/**
* StupidlySimple Framework - A PHP Framework For Lazy Developers.
*
* Copyright (c) 2017 Fariz Luqman
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @author Fariz Luqman <fariz.fnb@gmail.com>
* @copyright 2017 Fariz Luqman
* @license MIT
*
* @link https://stupidlysimple.github.io/
*/
namespace Simplyfier;
use Simplyfier\DI\Sharer;
/**
* The Viewer - View template files
* -----------------------------------------------------------------------.
*
* Reads and render the template file. Responsible for injecting
* dependencies from both Container and the Core\Sharer
*
* @since 0.5.0
*/
class Viewer
{
/**
* the hive is where all data is stored, which is then usable from all template
* files.
*
* @since 0.5.0
*/
private static $hive = [];
/**
* Finds, renders and displays a template file. Reports a 404 error in
* case of missing files.
*
* @param string $file file name / path to the file
* @param array $data array of data
*
* @static
*
* @see Viewer::render()
* @since 0.5.0
*/
public static function file($file, array $data = [])
{
// Do you love displaying blank pages?
if ($file === 'index' || $file === 'index.php') {
Debugger::report(404, true);
} else {
/**
* Get the path of the calling script and get it's containing Directory
* to enable include() style of accessing files.
*/
$calling_script_path = debug_backtrace()[0]['file'];
$calling_script_directory = realpath(dirname($calling_script_path));
/*
* Check if file exists, try directories
* 1. in the same directory as the calling script
* 2. same as #1 but without .tpl.php
* 3. Check in resources/views directory
* 4. same as #3 but without .tpl.php
* 5. check on the root directory
* 6. same #5 but without .tpl.php
*/
if (file_exists($render_path = $calling_script_directory.'/'.$file.'.tpl.php')) {
self::render($render_path, $data);
} elseif (file_exists($render_path = $calling_script_directory.'/'.$file)) {
self::render($render_path, $data);
} elseif (file_exists($render_path = SS_PATH.'/resources/views/'.$file.'.tpl.php')) {
self::render($render_path, $data);
} elseif (file_exists($render_path = SS_PATH.'/resources/views/'.$file)) {
self::render($render_path, $data);
} elseif (file_exists($render_path = SS_PATH.'/'.$file.'.tpl.php')) {
self::render($render_path, $data);
} elseif (file_exists($render_path = SS_PATH.'/'.$file)) {
self::render($render_path, $data);
} else {
Debugger::report(404, true);
}
}
}
/**
* Renders a template file. Inject dependencies from the Application
* Container and the Core\Sharer before viewing the file. Also,
* extracts $data into variables usable from the template files.
*
* The template file will be echoed in the scope of this static
* private method.
*
* @param string $file file name / path to the file
*
* @static
*
* @since 0.5.0
*/
private static function render($file, $data)
{
// Extract data passed by the user
extract($data);
// Extract data from the Sharer
if (Sharer::get() !== null) {
extract(Sharer::get());
}
// Merge data into the hive
self::$hive = array_merge(self::$hive, get_defined_vars());
// Unset data since we have extracted it
unset($data);
// Capture all contents of the template file into string $input
ob_start();
include $file;
$input = ob_get_contents();
ob_end_clean();
// Replace all {{ }} with values
$output = preg_replace_callback('!\{\{(.*?)\}\}!', 'Viewer::replace', $input);
// Display final output of the template file
echo $output;
}
/**
* Replace {{ }} with values.
*
* @static
*
* @param $matches
*
* @return mixed
*
* @since 0.5.0
*/
private static function replace($matches)
{
// If '.' is found in the $matches[1], assume it is an object
// which have a property.
if (strpos($matches[1], '.') !== false) {
// Explode the part before and after '.'
// the part before '.' is an object, while the part after '.' is a property
list($object, $property) = explode('.', $matches[1]);
// If a '()' is found in $property, we will then assume it to be a callable
// method.
if (strpos($property, '()') !== false) {
// Remove paranthesis
list($function, $parenthesis) = explode('()', $property);
// Execute the method and return the value given by the method
return self::$hive[$object]->$function();
} else {
// Return the property of the object from the hive
return self::$hive[$object]->$property;
}
} else {
if (strpos($matches[1], '()') !== false) {
// Remove paranthesis
list($function, $parenthesis) = explode('()', $matches[1]);
// Execute function and return the value given by the function
return self::$hive[$function]();
} elseif (isset(self::$hive[$matches[1]])) {
return self::$hive[$matches[1]];
}
}
}
}