1 | <?php |
||
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) |
||
34 | |||
35 | /** |
||
36 | * Set the LoggerInterface instance. |
||
37 | * |
||
38 | * @param LoggerInterface $logger |
||
39 | * |
||
40 | * @return self |
||
41 | */ |
||
42 | public function logger(LoggerInterface $logger) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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: