1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psr7Middlewares\Middleware; |
4
|
|
|
|
5
|
|
|
use Psr7Middlewares\Middleware; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use RuntimeException; |
10
|
|
|
|
11
|
|
|
class AccessLog |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var LoggerInterface|null The router container |
15
|
|
|
*/ |
16
|
|
|
private $logger; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
private $combined = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor.Set the LoggerInterface instance. |
25
|
|
|
* |
26
|
|
|
* @param LoggerInterface $logger |
27
|
|
|
*/ |
28
|
|
|
public function __construct(LoggerInterface $logger = null) |
29
|
|
|
{ |
30
|
|
|
if ($logger !== null) { |
31
|
|
|
$this->logger($logger); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set the LoggerInterface instance. |
37
|
|
|
* |
38
|
|
|
* @param LoggerInterface $logger |
39
|
|
|
* |
40
|
|
|
* @return self |
41
|
|
|
*/ |
42
|
|
|
public function logger(LoggerInterface $logger) |
43
|
|
|
{ |
44
|
|
|
$this->logger = $logger; |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Whether use the combined log format instead the common log format. |
51
|
|
|
* |
52
|
|
|
* @param bool $combined |
53
|
|
|
* |
54
|
|
|
* @return self |
55
|
|
|
*/ |
56
|
|
|
public function combined($combined = true) |
57
|
|
|
{ |
58
|
|
|
$this->combined = $combined; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Execute the middleware. |
65
|
|
|
* |
66
|
|
|
* @param ServerRequestInterface $request |
67
|
|
|
* @param ResponseInterface $response |
68
|
|
|
* @param callable $next |
69
|
|
|
* |
70
|
|
|
* @return ResponseInterface |
71
|
|
|
*/ |
72
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
73
|
|
|
{ |
74
|
|
|
if (empty($this->logger)) { |
75
|
|
|
throw new RuntimeException('No PSR-4 logger instance has been provided'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (!Middleware::hasAttribute($request, ClientIp::KEY)) { |
79
|
|
|
throw new RuntimeException('AccessLog middleware needs ClientIp executed before'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$message = $this->combined ? self::combinedFormat($request, $response) : self::commonFormat($request, $response); |
83
|
|
|
|
84
|
|
|
if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 600) { |
85
|
|
|
$this->logger->addError($message); |
|
|
|
|
86
|
|
|
} else { |
87
|
|
|
$this->logger->addInfo($message); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $next($request, $response); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Generates a message using the Apache's Common Log format |
95
|
|
|
* https://httpd.apache.org/docs/2.4/logs.html#accesslog. |
96
|
|
|
* |
97
|
|
|
* Note: The user identifier (identd) is ommited intentionally |
98
|
|
|
* |
99
|
|
|
* @param ServerRequestInterface $request |
100
|
|
|
* @param ResponseInterface $response |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
private static function commonFormat(ServerRequestInterface $request, ResponseInterface $response) |
105
|
|
|
{ |
106
|
|
|
return sprintf('%s %s [%s] "%s %s %s/%s" %d %d', |
107
|
|
|
ClientIp::getIp($request), |
108
|
|
|
$request->getUri()->getUserInfo() ?: '-', |
109
|
|
|
strftime('%d/%b/%Y:%H:%M:%S %z'), |
110
|
|
|
strtoupper($request->getMethod()), |
111
|
|
|
$request->getUri()->getPath(), |
112
|
|
|
strtoupper($request->getUri()->getScheme()), |
113
|
|
|
$request->getProtocolVersion(), |
114
|
|
|
$response->getStatusCode(), |
115
|
|
|
$response->getBody()->getSize() |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Generates a message using the Apache's Combined Log format |
121
|
|
|
* This is exactly the same than Common Log, with the addition of two more fields: Referer and User-Agent headers. |
122
|
|
|
* |
123
|
|
|
* @param ServerRequestInterface $request |
124
|
|
|
* @param ResponseInterface $response |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
private static function combinedFormat(ServerRequestInterface $request, ResponseInterface $response) |
129
|
|
|
{ |
130
|
|
|
return sprintf('%s "%s" "%s"', |
131
|
|
|
self::commonFormat($request, $response), |
132
|
|
|
$request->getHeaderLine('Referer'), |
133
|
|
|
$request->getHeaderLine('User-Agent') |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: