1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mouf\Mvc\Splash\Routers; |
4
|
|
|
|
5
|
|
|
use Mouf\Mvc\Splash\Controllers\Http404HandlerInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use Mouf\Mvc\Splash\Services\SplashUtils; |
10
|
|
|
use Zend\Stratigility\MiddlewareInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This router always returns a 404 page, based on the configured page not found controller. |
14
|
|
|
* |
15
|
|
|
* @author Kevin Nguyen |
16
|
|
|
* @author David Négrier |
17
|
|
|
*/ |
18
|
|
|
class NotFoundRouter implements MiddlewareInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The logger. |
22
|
|
|
* |
23
|
|
|
* @var LoggerInterface |
24
|
|
|
*/ |
25
|
|
|
private $log; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Http404HandlerInterface |
29
|
|
|
*/ |
30
|
|
|
private $pageNotFoundController; |
31
|
|
|
|
32
|
|
|
public function __construct(Http404HandlerInterface $pageNotFoundController, LoggerInterface $log = null) |
33
|
|
|
{ |
34
|
|
|
$this->pageNotFoundController = $pageNotFoundController; |
35
|
|
|
$this->log = $log; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Process an incoming request and/or response. |
40
|
|
|
* |
41
|
|
|
* Accepts a server-side request and a response instance, and does |
42
|
|
|
* something with them. |
43
|
|
|
* |
44
|
|
|
* If the response is not complete and/or further processing would not |
45
|
|
|
* interfere with the work done in the middleware, or if the middleware |
46
|
|
|
* wants to delegate to another process, it can use the `$out` callable |
47
|
|
|
* if present. |
48
|
|
|
* |
49
|
|
|
* If the middleware does not return a value, execution of the current |
50
|
|
|
* request is considered complete, and the response instance provided will |
51
|
|
|
* be considered the response to return. |
52
|
|
|
* |
53
|
|
|
* Alternately, the middleware may return a response instance. |
54
|
|
|
* |
55
|
|
|
* Often, middleware will `return $out();`, with the assumption that a |
56
|
|
|
* later middleware will return a response. |
57
|
|
|
* |
58
|
|
|
* @param Request $request |
59
|
|
|
* @param Response $response |
60
|
|
|
* @param null|callable $out |
61
|
|
|
* |
62
|
|
|
* @return null|Response |
63
|
|
|
*/ |
64
|
|
|
public function __invoke(Request $request, Response $response, callable $out = null) |
65
|
|
|
{ |
66
|
|
|
if ($this->log) { |
67
|
|
|
$this->log->info('404 - Page not found on URL: '.$request->getUri()->getPath()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$response = SplashUtils::buildControllerResponse( |
71
|
|
|
function () use ($request) { |
72
|
|
|
return $this->pageNotFoundController->pageNotFound($request); |
73
|
|
|
} |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
return $response; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|