-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
MaintenanceModeHandler.php
57 lines (49 loc) · 1.57 KB
/
MaintenanceModeHandler.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Foundation;
use Illuminate\Support\Str;
use Laminas\Diactoros\Response\HtmlResponse;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Tobscure\JsonApi\Document;
class MaintenanceModeHandler implements RequestHandlerInterface
{
const MESSAGE = 'Currently down for maintenance. Please come back later.';
/**
* Handle the request and return a response.
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
// Special handling for API requests: they get a proper API response
if ($this->isApiRequest($request)) {
return $this->apiResponse();
}
// By default, return a simple text message.
return new HtmlResponse(self::MESSAGE, 503);
}
private function isApiRequest(ServerRequestInterface $request): bool
{
return Str::contains(
$request->getHeaderLine('Accept'),
'application/vnd.api+json'
);
}
private function apiResponse(): ResponseInterface
{
return new JsonResponse(
(new Document)->setErrors([
'status' => '503',
'title' => self::MESSAGE
]),
503,
['Content-Type' => 'application/vnd.api+json']
);
}
}