|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Slim Framework (https://slimframework.com) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/slimphp/Slim |
|
6
|
|
|
* @copyright Copyright (c) 2011-2017 Josh Lockhart |
|
7
|
|
|
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License) |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Slim\Handlers; |
|
10
|
|
|
|
|
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
13
|
|
|
use Slim\Http\Body; |
|
14
|
|
|
use UnexpectedValueException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Default Slim application not found handler. |
|
18
|
|
|
* |
|
19
|
|
|
* It outputs a simple message in either JSON, XML or HTML based on the |
|
20
|
|
|
* Accept header. |
|
21
|
|
|
*/ |
|
22
|
|
|
class NotFound extends AbstractHandler |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Invoke not found handler |
|
26
|
|
|
* |
|
27
|
|
|
* @param ServerRequestInterface $request The most recent Request object |
|
28
|
|
|
* @param ResponseInterface $response The most recent Response object |
|
29
|
|
|
* |
|
30
|
|
|
* @return ResponseInterface |
|
31
|
|
|
* @throws UnexpectedValueException |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response) |
|
34
|
|
|
{ |
|
35
|
|
|
if ($request->getMethod() === 'OPTIONS') { |
|
36
|
|
|
$contentType = 'text/plain'; |
|
37
|
|
|
$output = $this->renderPlainNotFoundOutput(); |
|
38
|
|
|
} else { |
|
39
|
|
|
$contentType = $this->determineContentType($request); |
|
40
|
|
|
switch ($contentType) { |
|
41
|
|
|
case 'application/json': |
|
42
|
|
|
$output = $this->renderJsonNotFoundOutput(); |
|
43
|
|
|
break; |
|
44
|
|
|
|
|
45
|
|
|
case 'text/xml': |
|
46
|
|
|
case 'application/xml': |
|
47
|
|
|
$output = $this->renderXmlNotFoundOutput(); |
|
48
|
|
|
break; |
|
49
|
|
|
|
|
50
|
|
|
case 'text/html': |
|
51
|
|
|
$output = $this->renderHtmlNotFoundOutput($request); |
|
52
|
|
|
break; |
|
53
|
|
|
|
|
54
|
|
|
default: |
|
55
|
|
|
throw new UnexpectedValueException('Cannot render unknown content type ' . $contentType); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$body = new Body(fopen('php://temp', 'r+')); |
|
60
|
|
|
$body->write($output); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
return $response->withStatus(404) |
|
63
|
|
|
->withHeader('Content-Type', $contentType) |
|
64
|
|
|
->withBody($body); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Render plain not found message |
|
69
|
|
|
* |
|
70
|
|
|
* @return ResponseInterface |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function renderPlainNotFoundOutput() |
|
73
|
|
|
{ |
|
74
|
|
|
return 'Not found'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Return a response for application/json content not found |
|
79
|
|
|
* |
|
80
|
|
|
* @return ResponseInterface |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function renderJsonNotFoundOutput() |
|
83
|
|
|
{ |
|
84
|
|
|
return '{"message":"Not found"}'; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Return a response for xml content not found |
|
89
|
|
|
* |
|
90
|
|
|
* @return ResponseInterface |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function renderXmlNotFoundOutput() |
|
93
|
|
|
{ |
|
94
|
|
|
return '<root><message>Not found</message></root>'; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Return a response for text/html content not found |
|
99
|
|
|
* |
|
100
|
|
|
* @param ServerRequestInterface $request The most recent Request object |
|
101
|
|
|
* |
|
102
|
|
|
* @return ResponseInterface |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function renderHtmlNotFoundOutput(ServerRequestInterface $request) |
|
105
|
|
|
{ |
|
106
|
|
|
$homeUrl = (string)($request->getUri()->withPath('')->withQuery('')->withFragment('')); |
|
107
|
|
|
return <<<END |
|
108
|
|
|
<html> |
|
109
|
|
|
<head> |
|
110
|
|
|
<title>Page Not Found</title> |
|
111
|
|
|
<style> |
|
112
|
|
|
body{ |
|
113
|
|
|
margin:0; |
|
114
|
|
|
padding:30px; |
|
115
|
|
|
font:12px/1.5 Helvetica,Arial,Verdana,sans-serif; |
|
116
|
|
|
} |
|
117
|
|
|
h1{ |
|
118
|
|
|
margin:0; |
|
119
|
|
|
font-size:48px; |
|
120
|
|
|
font-weight:normal; |
|
121
|
|
|
line-height:48px; |
|
122
|
|
|
} |
|
123
|
|
|
strong{ |
|
124
|
|
|
display:inline-block; |
|
125
|
|
|
width:65px; |
|
126
|
|
|
} |
|
127
|
|
|
</style> |
|
128
|
|
|
</head> |
|
129
|
|
|
<body> |
|
130
|
|
|
<h1>Page Not Found</h1> |
|
131
|
|
|
<p> |
|
132
|
|
|
The page you are looking for could not be found. Check the address bar |
|
133
|
|
|
to ensure your URL is spelled correctly. If all else fails, you can |
|
134
|
|
|
visit our home page at the link below. |
|
135
|
|
|
</p> |
|
136
|
|
|
<a href='$homeUrl'>Visit the Home Page</a> |
|
137
|
|
|
</body> |
|
138
|
|
|
</html> |
|
139
|
|
|
END; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.