-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.php
34 lines (30 loc) · 946 Bytes
/
helpers.php
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
<?php
use Narrowspark\MimeType\MimeTypeFileExtensionGuesser;
use React\Filesystem\Node\FileInterface;
use React\Http\Message\Response;
use React\Promise\PromiseInterface;
function response(array $data = [], $status = 200) {
return new Response(
$status,
['Content-Type' => 'application/json'],
json_encode($data)
);
}
function responseWithFile(FileInterface $file, $status = 200): PromiseInterface
{
return $file->getContents()
->then(function ($contents) use ($status, $file) {
return new Response(
$status,
['Content-Type' => MimeTypeFileExtensionGuesser::guess($file->getPath())],
$contents
);
},
function (Exception $exception) {
return response(['message' => array_values($exception->getMessages())], 500);
});
}
function request($request, $value)
{
return $request->getParsedBody()[$value];
}