1: <?php
2:
3: namespace Charcoal\App\Handler;
4:
5:
6: use Psr\Http\Message\ServerRequestInterface;
7: use Psr\Http\Message\ResponseInterface;
8:
9:
10: use Slim\Http\Body;
11:
12:
13: use Charcoal\App\Handler\AbstractHandler;
14:
15: 16: 17: 18: 19: 20: 21: 22: 23:
24: class Shutdown extends AbstractHandler
25: {
26: 27: 28: 29: 30:
31: protected $methods;
32:
33: 34: 35: 36: 37: 38: 39: 40:
41: public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $methods)
42: {
43: $this->setMethods($methods);
44:
45: if ($request->getMethod() === 'OPTIONS') {
46: $contentType = 'text/plain';
47: $output = $this->renderPlainOutput();
48: } else {
49: $contentType = $this->determineContentType($request);
50: switch ($contentType) {
51: case 'application/json':
52: $output = $this->renderJsonOutput();
53: break;
54:
55: case 'text/xml':
56: case 'application/xml':
57: $output = $this->renderXmlOutput();
58: break;
59:
60: case 'text/html':
61: default:
62: $output = $this->renderHtmlOutput();
63: break;
64: }
65: }
66:
67: $body = new Body(fopen('php://temp', 'r+'));
68: $body->write($output);
69:
70: return $response
71: ->withStatus(503)
72: ->withHeader('Content-type', $contentType)
73: ->withBody($body);
74: }
75:
76: 77: 78: 79: 80: 81:
82: protected function setMethods(array $methods)
83: {
84: $this->methods = implode(', ', $methods);
85:
86: return $this;
87: }
88:
89: 90: 91: 92: 93:
94: public function methods()
95: {
96: return $this->methods;
97: }
98:
99: 100: 101: 102: 103:
104: protected function renderPlainOutput()
105: {
106: $message = $this->translator()->translate('Down for maintenance!');
107:
108: return $this->render($message);
109: }
110:
111: 112: 113: 114: 115:
116: protected function renderJsonOutput()
117: {
118: $message = $this->translator()->translate('We are currently unavailable. Check back in 15 minutes.');
119:
120: return $this->render('{"message":"'.$message.'"}');
121: }
122:
123: 124: 125: 126: 127:
128: protected function renderXmlOutput()
129: {
130: $message = $this->translator()->translate('We are currently unavailable. Check back in 15 minutes.');
131:
132: return $this->render('<root><message>'.$message.'</message></root>');
133: }
134:
135: 136: 137: 138: 139:
140: public function messageTitle()
141: {
142: return $this->translator()->translate('Down for maintenance!');
143: }
144:
145: 146: 147: 148: 149:
150: public function renderHtmlMessage()
151: {
152: $title = $this->messageTitle();
153: $notice = $this->translator()->translate('currently-unavailable');
154: $message = '<h1>'.$title."</h1>\n\t\t<p>".$notice."</p>\n";
155:
156: return $message;
157: }
158: }
159: