-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.php
More file actions
142 lines (131 loc) · 3.67 KB
/
Application.php
File metadata and controls
142 lines (131 loc) · 3.67 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
<?php
namespace MaplePHP\Unitary\Console;
use MaplePHP\Blunder\Interfaces\AbstractHandlerInterface;
use MaplePHP\Blunder\Run;
use MaplePHP\Container\Container;
use MaplePHP\Http\Environment;
use MaplePHP\Http\ServerRequest;
use MaplePHP\Http\Uri;
use MaplePHP\Emitron\Kernel as EmitronKernel;
use MaplePHP\Unitary\Console\Middlewares\{AddCommandMiddleware,
CheckAllowedProps,
CliInitMiddleware,
ConfigPropsMiddleware,
LocalMiddleware};
final class Application
{
private array $middlewares = [
AddCommandMiddleware::class,
ConfigPropsMiddleware::class,
CheckAllowedProps::class,
LocalMiddleware::class,
CliInitMiddleware::class
];
public function __construct()
{
// Default config
if(is_file(__DIR__ . '/../../../../../unitary.config.php')) {
// From the vendor dir
EmitronKernel::setConfigFilePath(__DIR__ . '/../../../../../unitary.config.php');
} else {
// From the repo dir
EmitronKernel::setConfigFilePath(__DIR__ . '/../../unitary.config.php');
}
EmitronKernel::setRouterFilePath(__DIR__ . "/ConsoleRouter.php");
}
/**
* Clear the default middlewares, be careful with this
*
* @return $this
*/
public function clearDefaultMiddleware(): self
{
$inst = clone $this;
$inst->middlewares = [];
return $inst;
}
/**
* Clear the default middlewares, be careful with this
*
* @return $this
*/
public function unsetMiddleware(string $class): self
{
$inst = clone $this;
foreach($inst->middlewares as $key => $middleware) {
if($middleware === $class) {
unset($inst->middlewares[$key]);
break;
}
}
return $inst;
}
/**
* Add custom middlewares, follow PSR convention
*
* @param array $middleware
* @return $this
*/
public function withMiddleware(array $middleware): self
{
$inst = clone $this;
$inst->middlewares = array_merge($inst->middlewares, $middleware);
return $inst;
}
/**
* Change router file
*
* @param string $path
* @return $this
*/
public function withRouter(string $path): self
{
$inst = clone $this;
EmitronKernel::setRouterFilePath($path);
return $inst;
}
/**
* Change the config file
*
* @param string $path
* @return $this
*/
public function withConfig(string $path): self
{
$inst = clone $this;
EmitronKernel::setConfigFilePath($path);
return $inst;
}
/**
* Default error handler boot
* @param AbstractHandlerInterface $handler
* @return $this
*/
public function withErrorHandler(AbstractHandlerInterface $handler): self
{
$inst = clone $this;
$run = new Run($handler);
$run->severity()
->excludeSeverityLevels([E_USER_WARNING, E_NOTICE, E_USER_NOTICE, E_DEPRECATED, E_USER_DEPRECATED])
->redirectTo(function () {
// Let PHP’s default error handler process excluded severities
return false;
});
$run->setExitCode(1);
$run->load();
return $inst;
}
/**
* @param array $parts
* @return Kernel
* @throws \Exception
*/
public function boot(array $parts): Kernel
{
$env = new Environment();
$request = new ServerRequest(new Uri($env->getUriParts($parts)), $env);
$kernel = new Kernel(new Container(), $this->middlewares);
$kernel->run($request);
return $kernel;
}
}