1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace System; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Exception; |
7
|
|
|
use Dotenv\Dotenv; |
8
|
|
|
use Whoops\Run as Whoops; |
9
|
|
|
use Whoops\Util\Misc as WhoopsMisc; |
10
|
|
|
use Whoops\Handler\JsonResponseHandler; |
11
|
|
|
use Whoops\Handler\PrettyPageHandler; |
12
|
|
|
|
13
|
|
|
class Application |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Container |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $container = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Application Object |
24
|
|
|
* |
25
|
|
|
* @var \System\Application |
26
|
|
|
*/ |
27
|
|
|
private static $instance; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor |
31
|
|
|
* |
32
|
|
|
* @param \System\File $file |
33
|
|
|
*/ |
34
|
|
|
private function __construct(File $file) |
35
|
|
|
{ |
36
|
|
|
$this->share('file', $file); |
37
|
|
|
|
38
|
|
|
Dotenv::createImmutable($this->file->root())->load(); |
39
|
|
|
|
40
|
|
|
$this->file->call('config/error.php'); |
41
|
|
|
|
42
|
|
|
$this->handleErrors(); |
43
|
|
|
|
44
|
|
|
$this->file->call('Core/helpers.php'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Run error handling of Whoops |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
private function handleErrors() |
53
|
|
|
{ |
54
|
|
|
$run = new Whoops(); |
55
|
|
|
|
56
|
|
|
$run->prependHandler(new PrettyPageHandler()); |
57
|
|
|
|
58
|
|
|
if (WhoopsMisc::isAjaxRequest()) { |
59
|
|
|
$jsonHandler = new JsonResponseHandler(); |
60
|
|
|
|
61
|
|
|
$jsonHandler->setJsonApi(true); |
62
|
|
|
|
63
|
|
|
$run->prependHandler($jsonHandler); |
64
|
|
|
} |
65
|
|
|
$run->register(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get Application instance |
70
|
|
|
* |
71
|
|
|
* @param \System\File $file |
72
|
|
|
* @return \System\Application |
73
|
|
|
*/ |
74
|
|
|
public static function getInstance($file) |
75
|
|
|
{ |
76
|
|
|
self::$instance = is_null(self::$instance) ? new static($file) : self::$instance; |
77
|
|
|
return self::$instance; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Run the Application |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
public function run() |
86
|
|
|
{ |
87
|
|
|
$this->session->start(); |
88
|
|
|
|
89
|
|
|
$this->request->prepareUrl(); |
90
|
|
|
|
91
|
|
|
foreach (glob("routes/**/*.php") as $route) { |
92
|
|
|
$this->file->call($route); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$output = $this->route->getProperRoute(); |
96
|
|
|
|
97
|
|
|
$this->response->setOutput($output); |
98
|
|
|
|
99
|
|
|
$this->response->send(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get all core classes |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function coreClasses() |
108
|
|
|
{ |
109
|
|
|
return $this->file->call('config/alias.php')['classes']; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Share the given key|value through Application |
114
|
|
|
* |
115
|
|
|
* @param string $key |
116
|
|
|
* @param mixed $value |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
public function share($key, $value) |
120
|
|
|
{ |
121
|
|
|
if ($value instanceof Closure) { |
122
|
|
|
$value = call_user_func($value, $this); |
123
|
|
|
} |
124
|
|
|
$this->container[$key] = $value; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get shared value |
129
|
|
|
* |
130
|
|
|
* @param string $key |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
public function get($key) |
134
|
|
|
{ |
135
|
|
|
if (!$this->isSharing($key)) { |
136
|
|
|
if ($this->isCoreAlias($key)) { |
137
|
|
|
$this->share($key, $this->createObject($key)); |
138
|
|
|
} else { |
139
|
|
|
throw new Exception("$key is not found"); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
return $this->container[$key]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Determine if the given key is shared through Application |
147
|
|
|
* |
148
|
|
|
* @param string $key |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
|
|
public function isSharing($key) |
152
|
|
|
{ |
153
|
|
|
return isset($this->container[$key]); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Determine if the given key is an alias to core class |
158
|
|
|
* |
159
|
|
|
* @param string $key |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function isCoreAlias($key) |
163
|
|
|
{ |
164
|
|
|
return isset($this->coreClasses()[$key]); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Create new object for the core class based on the given key |
169
|
|
|
* |
170
|
|
|
* @param string $key |
171
|
|
|
* @return object |
172
|
|
|
*/ |
173
|
|
|
public function createObject($key) |
174
|
|
|
{ |
175
|
|
|
$object = $this->coreClasses()[$key]; |
176
|
|
|
return new $object($this); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get shared value dynamically |
181
|
|
|
* |
182
|
|
|
* @param string $key |
183
|
|
|
* @return mixed |
184
|
|
|
*/ |
185
|
|
|
public function __get($key) |
186
|
|
|
{ |
187
|
|
|
return $this->get($key); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|