-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.php
More file actions
262 lines (240 loc) · 5.68 KB
/
Copy pathApplication.php
File metadata and controls
262 lines (240 loc) · 5.68 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
declare(strict_types = 1);
namespace Innmind\Framework;
use Innmind\OperatingSystem\OperatingSystem;
use Innmind\CLI\{
Environment as CliEnv,
Command,
};
use Innmind\DI\{
Container,
Service,
};
use Innmind\Router\{
Component,
Pipe,
};
use Innmind\Http\{
ServerRequest,
Response,
};
use Innmind\Immutable\{
Attempt,
SideEffect,
};
/**
* @template I of ServerRequest|CliEnv
* @template O of Response|CliEnv
*/
final class Application
{
/**
* @psalm-mutation-free
*
* @param Application\Implementation<I, O> $app
*/
private function __construct(
private Application\Implementation $app,
) {
}
/**
* @psalm-pure
*
* @return self<ServerRequest, Response>
*/
#[\NoDiscard]
public static function http(OperatingSystem $os, Environment $env): self
{
return new self(Application\Http::of($os, $env));
}
/**
* @psalm-pure
*
* @return self<CliEnv, CliEnv>
*/
#[\NoDiscard]
public static function cli(OperatingSystem $os, Environment $env): self
{
return new self(Application\Cli::of($os, $env));
}
/**
* @psalm-pure
* @experimental
*
* @return self<CliEnv, CliEnv>
*/
#[\NoDiscard]
public static function asyncHttp(OperatingSystem $os): self
{
return new self(Application\Async\Http::of($os));
}
/**
* @psalm-mutation-free
*
* @param callable(Environment, OperatingSystem): Environment $map
*
* @return self<I, O>
*/
#[\NoDiscard]
public function mapEnvironment(callable $map): self
{
return new self($this->app->mapEnvironment($map));
}
/**
* @psalm-mutation-free
*
* @param callable(OperatingSystem, Environment): OperatingSystem $map
*
* @return self<I, O>
*/
#[\NoDiscard]
public function mapOperatingSystem(callable $map): self
{
return new self($this->app->mapOperatingSystem($map));
}
/**
* @psalm-mutation-free
*
* @return self<I, O>
*/
#[\NoDiscard]
public function map(Middleware $map): self
{
/** @psalm-suppress ImpureMethodCall Mutation free to force the user to use the returned object */
return $map($this);
}
/**
* @psalm-mutation-free
*
* @param callable(Container, OperatingSystem, Environment): object $definition
*
* @return self<I, O>
*/
#[\NoDiscard]
public function service(Service $name, callable $definition): self
{
return new self($this->app->service($name, $definition));
}
/**
* @psalm-mutation-free
*
* @param callable(Container): Command $command
*
* @return self<I, O>
*/
#[\NoDiscard]
public function command(callable $command): self
{
return new self($this->app->command($command));
}
/**
* @psalm-mutation-free
*
* @param callable(Command, Container): Command $map
*
* @return self<I, O>
*/
#[\NoDiscard]
public function mapCommand(callable $map): self
{
return new self($this->app->mapCommand($map));
}
/**
* @psalm-mutation-free
*
* @param Http\Route\Reference|callable(Pipe, Container): Component<SideEffect, Response> $handle
*
* @return self<I, O>
*/
#[\NoDiscard]
public function route(Http\Route\Reference|callable $handle): self
{
if ($handle instanceof Http\Route\Reference) {
$handle = $handle->route();
}
return new self($this->app->route($handle));
}
/**
* @psalm-mutation-free
*
* @param class-string<Http\Route\Reference> $routes
*
* @return self<I, O>
*/
#[\NoDiscard]
public function routes(string $routes): self
{
$self = $this;
foreach ($routes::cases() as $route) {
$self = $self->route($route);
}
return $self;
}
/**
* This wraps each route individually.
*
* This means that the code may be executed multiple times until it reaches
* the appropriate route.
*
* @psalm-mutation-free
*
* @param callable(Component<SideEffect, Response>, Container): Component<SideEffect, Response> $map
*
* @return self<I, O>
*/
#[\NoDiscard]
public function mapRoute(callable $map): self
{
return new self($this->app->mapRoute($map));
}
/**
* This wraps all routes as whole.
*
* This means that the code will be executed only once.
*
* @psalm-mutation-free
*
* @param callable(Component<SideEffect, Response>, Container): Component<SideEffect, Response> $map
*
* @return self<I, O>
*/
#[\NoDiscard]
public function mapRoutes(callable $map): self
{
return new self($this->app->mapRoutes($map));
}
/**
* @psalm-mutation-free
*
* @param callable(ServerRequest, Container): Attempt<Response> $handle
*
* @return self<I, O>
*/
#[\NoDiscard]
public function routeNotFound(callable $handle): self
{
return new self($this->app->routeNotFound($handle));
}
/**
* @psalm-mutation-free
*
* @param callable(ServerRequest, \Throwable, Container): Attempt<Response> $recover
*
* @return self<I, O>
*/
#[\NoDiscard]
public function recoverRouteError(callable $recover): self
{
return new self($this->app->recoverRouteError($recover));
}
/**
* @param I $input
*
* @return Attempt<O>
*/
#[\NoDiscard]
public function run(CliEnv|ServerRequest $input): Attempt
{
return $this->app->run($input);
}
}