-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.php
More file actions
157 lines (138 loc) · 2.86 KB
/
Copy pathApp.php
File metadata and controls
157 lines (138 loc) · 2.86 KB
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
<?php
declare(strict_types=1);
namespace MaplePHP\Core;
use MaplePHP\Core\Support\Dir;
use MaplePHP\Core\Enums\Environment;
use MaplePHP\Emitron\Contracts\AppInterface;
final class App implements AppInterface
{
private static ?self $inst = null;
private Dir $dir;
private string $coreDir;
private array $config;
private array $app;
private function __construct(Dir $dir, array $config = []) {
$this->dir = $dir;
$this->coreDir = __DIR__;
$this->config = $config;
// Renamed configs to app
$this->app = $this->config['app'] ?? ($this->config['configs'] ?? []);
}
/**
* This is a single to set App globals
*
* @param Dir $dir
* @param array $config
* @return self
*/
public static function boot(Dir $dir, array $config = []): self
{
if (self::$inst !== null) {
throw new \RuntimeException('App already initialized.');
}
return self::$inst = new self($dir, $config);
}
/**
* Get App singleton instance
*
* @return self
*/
public static function get(): self
{
if (self::$inst === null) {
throw new \RuntimeException('App not initialized. Call App::boot() first.');
}
return self::$inst;
}
/**
* Check if the environment is in prod
*
* @return bool
*/
public function isProd(): bool
{
return ($this->app['env'] ?? "development") === Environment::PROD->name();
}
/**
* Check if the environment is in stage
*
* @return bool
*/
public function isStage(): bool
{
return ($this->app['env'] ?? "development") === Environment::STAGE->name();
}
/**
* Check if the environment is in test
*
* @return bool
*/
public function isTest(): bool
{
return ($this->app['env'] ?? "development") === Environment::TEST->name();
}
/**
* Check if the environment is in dev
*
* @return bool
*/
public function isDev(): bool
{
return ($this->app['env'] ?? "development") === Environment::DEV->name();
}
/**
* Get current Environment
*
* @return string
*/
public function env(): string
{
return ($this->app['env'] ?? "development") ?? Environment::PROD->name();
}
/**
* Get core/boot dir where code app boot originate
*
* @return string
*/
public function coreDir(): string
{
return $this->coreDir;
}
/**
* Get the app core Dir instance
*
* @return Dir
*/
public function dir(): Dir
{
return $this->dir;
}
/**
* Get the app core configs
*
* @return array
*/
public function configs(): array
{
return $this->config;
}
/**
* Get the app core configs
*
* @return array
*/
public function app(): array
{
return $this->app;
}
/**
* Get the app core configs prop
*
* @param string $key
* @return mixed
*/
public function getApp(string $key): mixed
{
return $this->app[$key] ?? null;
}
}