1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tight; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License |
7
|
|
|
* |
8
|
|
|
* Copyright 2016 Alejandro Peña Florentín ([email protected]) |
9
|
|
|
* |
10
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
11
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
12
|
|
|
* in the Software without restriction, including without limitation the rights |
13
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
14
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
15
|
|
|
* furnished to do so, subject to the following conditions: |
16
|
|
|
* |
17
|
|
|
* The above copyright notice and this permission notice shall be included in |
18
|
|
|
* all copies or substantial portions of the Software. |
19
|
|
|
* |
20
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
21
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
22
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
23
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
24
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
25
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
26
|
|
|
* THE SOFTWARE. |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Tight framework main class |
31
|
|
|
* |
32
|
|
|
* @author Alejandro Peña Florentín ([email protected]) |
33
|
|
|
*/ |
34
|
|
|
class Tight |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Current Version |
39
|
|
|
*/ |
40
|
|
|
const VERSION = "v1.1.0-dev"; |
41
|
|
|
|
42
|
|
|
private static $INSTANCE = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
* @var \Tight\TightConfig Configuration |
47
|
|
|
*/ |
48
|
|
|
private $config = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var \Tight\Router Router |
52
|
|
|
*/ |
53
|
|
|
private $router; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var \Smarty Smarty templates system |
57
|
|
|
*/ |
58
|
|
|
private $smarty; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var \Tight\Modules\ModuleLoader Module loader |
62
|
|
|
*/ |
63
|
|
|
private $moduleLoader; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Creates an instance of Tight Framework. |
67
|
|
|
* @param array|TightConfig $config Settings to override the config file |
68
|
|
|
*/ |
69
|
|
|
public function __construct($config = []) { |
70
|
|
|
// Sets the instance |
71
|
|
|
self::$INSTANCE = $this; |
72
|
|
|
set_exception_handler([$this, "exceptionHandler"]); |
73
|
|
|
$this->setConfig($config); |
74
|
|
|
$this->router = new Router($this->config->basePath); |
75
|
|
|
$this->smarty = new \Smarty; |
76
|
|
|
$this->smarty->template_dir = $this->config->smarty["template_dir"]; |
77
|
|
|
$this->smarty->compile_dir = $this->config->smarty["compile_dir"]; |
78
|
|
|
$this->smarty->config_dir = $this->config->smarty["config_dir"]; |
79
|
|
|
$this->smarty->cache_dir = $this->config->smarty["cache_dir"]; |
80
|
|
|
$this->moduleLoader = new \Tight\Modules\ModuleLoader($this); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Gets the class instance |
85
|
|
|
* @return \Tight\Tight |
86
|
|
|
*/ |
87
|
|
|
public static function getInstance() { |
88
|
|
|
if (null !== self::$INSTANCE) { |
89
|
|
|
return self::$INSTANCE; |
90
|
|
|
} else { |
91
|
|
|
return new \Tight\Tight; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Sets the configuration |
97
|
|
|
* @param array|\Tight\TightConfig $config Configuration object or |
98
|
|
|
* associative array with available options |
99
|
|
|
* @throws \InvalidArgumentException If $config is not an array or an |
100
|
|
|
* instance of \Tight\TightConfig class |
101
|
|
|
*/ |
102
|
|
|
public function setConfig($config) { |
103
|
|
|
if (is_array($config)) { |
104
|
|
|
$config = new \Tight\TightConfig($config); |
105
|
|
|
} elseif (!$config instanceof \Tight\TightConfig) { |
106
|
|
|
throw new \InvalidArgumentException("Argument passed to Tight::__constructor must be array or <b>\Tight\TightConfig</b> object"); |
107
|
|
|
} |
108
|
|
|
$this->config = $config; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Gets the configuration |
113
|
|
|
* @return \Tight\TightConfig |
114
|
|
|
*/ |
115
|
|
|
public function getConfig() { |
116
|
|
|
return $this->config; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Gets the Smarty |
121
|
|
|
* @return \Smarty |
122
|
|
|
*/ |
123
|
|
|
public function getSmarty() { |
124
|
|
|
return $this->smarty; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Custom exception handler |
129
|
|
|
* @param \Exception $exception Exception |
130
|
|
|
*/ |
131
|
|
|
public function exceptionHandler($exception) { |
132
|
|
|
self::printException($exception); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Custom exception handler |
137
|
|
|
* @param \Exception $exception Exception |
138
|
|
|
* @codeCoverageIgnore |
139
|
|
|
*/ |
140
|
|
|
public static function printException($exception) { |
141
|
|
|
$lastTrace = $exception->getTrace()[count($exception->getTrace()) - 1]; |
142
|
|
|
$output = <<<EXC |
143
|
|
|
<!DOCTYPE> |
144
|
|
|
<html> |
145
|
|
|
<head> |
146
|
|
|
<title>Tight Framework Exception</title> |
147
|
|
|
<style> |
148
|
|
|
*{ |
149
|
|
|
margin: 0; |
150
|
|
|
padding: 0; |
151
|
|
|
} |
152
|
|
|
html { |
153
|
|
|
font-family: Helvetica; |
154
|
|
|
} |
155
|
|
|
body { |
156
|
|
|
padding: 1em; |
157
|
|
|
} |
158
|
|
|
h1 { |
159
|
|
|
margin-bottom: 0.5em; |
160
|
|
|
} |
161
|
|
|
p { |
162
|
|
|
margin-top: 0.25em; |
163
|
|
|
margin-bottom: 0.5em; |
164
|
|
|
} |
165
|
|
|
.padding-left { |
166
|
|
|
padding-left: 2em; |
167
|
|
|
} |
168
|
|
|
</style> |
169
|
|
|
</head> |
170
|
|
|
<body> |
171
|
|
|
<h1>Tight Framework Exception</h1> |
172
|
|
|
EXC; |
173
|
|
|
$output .= "<p><strong>" . get_class($exception) . ": </strong>" . $exception->getMessage() . "</p>"; |
174
|
|
|
$output .= "<p class='padding-left'>in <strong>" . $lastTrace['file'] . "</strong> at line <strong>" . $lastTrace['line'] . "</strong></p>"; |
175
|
|
|
$output .= "<br/>"; |
176
|
|
|
$output .= "<p>Stack Trace:</p>"; |
177
|
|
|
$trace = $exception->getTrace(); |
178
|
|
|
$size = count($trace); |
179
|
|
|
for ($index = 0; $index < $size; $index++) { |
180
|
|
|
$element = $trace[$index]; |
181
|
|
|
$class = isset($element["class"]) ? $element["class"] : ""; |
182
|
|
|
$type = isset($element["type"]) ? $element["type"] : ""; |
183
|
|
|
$func = isset($element["function"]) ? $element["function"] : ""; |
184
|
|
|
$file = isset($element["file"]) ? "at <strong>" . $element["file"] . "</strong>" : ""; |
185
|
|
|
$line = isset($element["line"]) ? " at line <strong>" . $element["line"] . "</strong>" : ""; |
186
|
|
|
$output .= "<p>#" . ($index + 1) . ": " . $class . $type . $func . "() " . $file . $line . "</strong></p>"; |
187
|
|
|
} |
188
|
|
|
echo $output; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Gets the router |
193
|
|
|
* @return \Tight\Router |
194
|
|
|
*/ |
195
|
|
|
public function getRouter() { |
196
|
|
|
return $this->router; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Runs the router to send response to the request URI |
201
|
|
|
*/ |
202
|
|
|
public function run() { |
203
|
|
|
if ($this->config->mvc["enableRouter"]) { |
204
|
|
|
$this->router->runMvc(); |
205
|
|
|
} else { |
206
|
|
|
$this->router->run(); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function getModuleLoader() { |
211
|
|
|
return $this->moduleLoader; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function moduleAdd(\Tight\Modules\AbstractModule $module) { |
215
|
|
|
$this->moduleLoader->add($module); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function moduleRemove($moduleName) { |
219
|
|
|
return $this->moduleLoader->remove($moduleName); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Gets all the modules added |
224
|
|
|
* @return array Modules |
225
|
|
|
*/ |
226
|
|
|
public function getModules() { |
227
|
|
|
return $this->moduleLoader->getModules(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Gets a single module |
232
|
|
|
* @param string $moduleName Module name |
233
|
|
|
* @return \Tight\Module\AbstractModule Module or null if the module name |
234
|
|
|
* cant be found |
235
|
|
|
*/ |
236
|
|
|
public function getModule($moduleName) { |
237
|
|
|
return $this->moduleLoader->getModule($moduleName); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
|