1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace System; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Exception; |
7
|
|
|
use Dotenv\Dotenv; |
8
|
|
|
|
9
|
|
|
class Application |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Container |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private $container = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Set and rename core classes |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $classes = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Application Object |
27
|
|
|
* |
28
|
|
|
* @var \System\Application |
29
|
|
|
*/ |
30
|
|
|
private static $instance; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor |
34
|
|
|
* |
35
|
|
|
* @property object $file |
36
|
|
|
* @property object $error |
37
|
|
|
* @param \System\File $file |
38
|
|
|
*/ |
39
|
|
|
private function __construct(File $file) |
40
|
|
|
{ |
41
|
|
|
$this->share('file', $file); |
42
|
|
|
|
43
|
|
|
$this->classes = $this->file->call('config/classes.php'); |
|
|
|
|
44
|
|
|
|
45
|
|
|
Dotenv::createImmutable($this->file->root())->load(); |
|
|
|
|
46
|
|
|
|
47
|
|
|
$this->file->call('Core/helpers.php'); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$this->error->toggleError(); |
|
|
|
|
50
|
|
|
|
51
|
|
|
register_shutdown_function([$this->error, 'handleErrors']); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get Application instance |
56
|
|
|
* |
57
|
|
|
* @param \System\File $file |
58
|
|
|
* @return \System\Application |
59
|
|
|
*/ |
60
|
|
|
public static function getInstance($file) |
61
|
|
|
{ |
62
|
|
|
self::$instance = is_null(self::$instance) ? new static($file) : self::$instance; |
63
|
|
|
|
64
|
|
|
return self::$instance; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Run the Application |
69
|
|
|
* |
70
|
|
|
* @property object $session |
71
|
|
|
* @property object $request |
72
|
|
|
* @property object $file |
73
|
|
|
* @property object $route |
74
|
|
|
* @property object $response |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
public function run() |
78
|
|
|
{ |
79
|
|
|
$this->session->start(); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$this->request->prepareUrl(); |
|
|
|
|
82
|
|
|
|
83
|
|
|
foreach (glob("routes/**/*.php") as $route) { |
84
|
|
|
$this->file->call($route); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$output = $this->route->getProperRoute(); |
|
|
|
|
88
|
|
|
|
89
|
|
|
$this->response->setOutput($output); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$this->response->send(); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Share the given key|value through Application |
96
|
|
|
* |
97
|
|
|
* @param string $key |
98
|
|
|
* @param mixed $value |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
public function share($key, $value) |
102
|
|
|
{ |
103
|
|
|
if ($value instanceof Closure) { |
104
|
|
|
$value = call_user_func($value, $this); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->container[$key] = $value; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get shared value |
112
|
|
|
* When the key exists in the $classes, it will look if it was sharing before |
113
|
|
|
* is not sharing: it will create in an object and add it to the $container |
114
|
|
|
* is sharing: it will grab it direct from the $container |
115
|
|
|
* |
116
|
|
|
* When the key is not exists in the core $classes it will look in all the folders and subfolders |
117
|
|
|
* is it exists: it will process the name and create an object and add it to the $container |
118
|
|
|
* is it not exists: it will throw an Exception |
119
|
|
|
* |
120
|
|
|
* @property object $file |
121
|
|
|
* @param string $key |
122
|
|
|
* @return mixed |
123
|
|
|
*/ |
124
|
|
|
public function get($key) |
125
|
|
|
{ |
126
|
|
|
if (!$this->isSharing($key)) { |
127
|
|
|
if ($this->isClassAliasIsset($key)) { |
128
|
|
|
$this->share($key, $this->createObject($key)); |
129
|
|
|
} else { |
130
|
|
|
$found = false; |
131
|
|
|
$dirs = getAllSubDires('core/System/'); |
132
|
|
|
|
133
|
|
|
foreach ($dirs as $dir) { |
134
|
|
|
$path = $this->file->fullPath($dir . ucwords($key)) . '.php'; |
|
|
|
|
135
|
|
|
|
136
|
|
|
if ($this->file->exists($path)) { |
|
|
|
|
137
|
|
|
$found = true; |
138
|
|
|
|
139
|
|
|
$dir = $this->file->fullPath($dir . ucwords($key)); |
|
|
|
|
140
|
|
|
$dir = ltrim($dir, $this->file->root() . 'core'); |
|
|
|
|
141
|
|
|
|
142
|
|
|
$this->classes[$key] = $dir; |
143
|
|
|
|
144
|
|
|
$this->share($key, $this->createObject($key)); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (!$found) { |
149
|
|
|
throw new Exception("$key is not found"); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this->container[$key]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Determine if the given key is shared through Application |
159
|
|
|
* |
160
|
|
|
* @param string $key |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
public function isSharing($key) |
164
|
|
|
{ |
165
|
|
|
return isset($this->container[$key]); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Determine if the given key is an alias to core class |
170
|
|
|
* |
171
|
|
|
* @param string $key |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
|
|
public function isClassAliasIsset($key) |
175
|
|
|
{ |
176
|
|
|
return isset($this->classes[$key]); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Create new object for the core class based on the given key |
181
|
|
|
* |
182
|
|
|
* @param string $key |
183
|
|
|
* @return object |
184
|
|
|
*/ |
185
|
|
|
public function createObject($key) |
186
|
|
|
{ |
187
|
|
|
$object = $this->classes[$key]; |
188
|
|
|
|
189
|
|
|
return new $object($this); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get shared value dynamically |
194
|
|
|
* |
195
|
|
|
* @param string $key |
196
|
|
|
* @return mixed |
197
|
|
|
*/ |
198
|
|
|
public function __get($key) |
199
|
|
|
{ |
200
|
|
|
return $this->get($key); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.