-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatsMiddleware.php
57 lines (49 loc) · 1.72 KB
/
StatsMiddleware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <info@arikaim.com>
* @license http://www.arikaim.com/license
*
*/
namespace Arikaim\Modules\Stats;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Arikaim\Core\Framework\Middleware\Middleware;
use Arikaim\Core\Framework\MiddlewareInterface;
use Arikaim\Core\Utils\ClientIp;
/**
* Stats middleware class
*/
class StatsMiddleware extends Middleware implements MiddlewareInterface
{
/**
* Process middleware
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return array [$request,$response]
*/
public function process(ServerRequestInterface $request, ResponseInterface $response): array
{
global $arikaim;
$uri = $request->getUri();
$statsData = [
'method' => $request->getMethod(),
'query_params' => $request->getQueryParams(),
'scheme' => $uri->getScheme(),
'path' => $uri->getPath(),
'fragment' => $uri->getFragment(),
'url' => (string)$uri,
'client_ip' => ClientIp::getClientIpAddress($request),
'http_user_agent' => $request->getHeader('User-Agent')[0] ?? null
];
if (($this->options['dispatch_event'] ?? null) == true) {
// dispatch event
$arikaim->get('event')->dispatch('stats.middleware',$statsData);
}
$request = $request->withAttribute('stats_data',$statsData);
return [$request,$response];
}
}