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: class NotFound extends AbstractHandler
24: {
25: 26: 27: 28: 29: 30: 31:
32: public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
33: {
34: $contentType = $this->determineContentType($request);
35: switch ($contentType) {
36: case 'application/json':
37: $output = $this->renderJsonOutput();
38: break;
39:
40: case 'text/xml':
41: case 'application/xml':
42: $output = $this->renderXmlOutput();
43: break;
44:
45: case 'text/html':
46: default:
47: $output = $this->renderHtmlOutput();
48: }
49:
50: $body = new Body(fopen('php://temp', 'r+'));
51: $body->write($output);
52:
53: return $response->withStatus(404)
54: ->withHeader('Content-Type', $contentType)
55: ->withBody($body);
56: }
57:
58: 59: 60: 61: 62:
63: protected function renderPlainOutput()
64: {
65: $message = $this->translator()->translate('Not Found');
66:
67: return $this->render($message);
68: }
69:
70: 71: 72: 73: 74:
75: protected function renderJsonOutput()
76: {
77: $message = $this->translator()->translate('Not Found');
78:
79: return $this->render('{"message":"'.$message.'"}');
80: }
81:
82: 83: 84: 85: 86:
87: protected function renderXmlOutput()
88: {
89: $message = $this->translator()->translate('Not Found');
90:
91: return $this->render('<root><message>'.$message.'</message></root>');
92: }
93:
94: 95: 96: 97: 98:
99: public function messageTitle()
100: {
101: return $this->translator()->translate('Page Not Found');
102: }
103:
104: 105: 106: 107: 108:
109: public function renderHtmlMessage()
110: {
111: $title = $this->messageTitle();
112: $link = sprintf(
113: '<a href="%1$s">%2$s</a>',
114: $this->baseUrl(),
115: $this->translator()->translate('Visit the Home Page')
116: );
117: $notice = $this->translator()->translate('The page you are looking for could not be found. Check the address bar to ensure your URL is spelled correctly. If all else fails, you can visit our home page at the link below.');
118: $message = '<h1>'.$title."</h1>\n\t\t<p>".$notice."</p>\n\t\t<p>".$link."</p>\n";
119:
120: return $message;
121: }
122: }
123: