1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DJStarCOM\ServerTiming\Middleware; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use DJStarCOM\ServerTiming\ServerTiming; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
|
11
|
|
|
class ServerTimingMiddleware |
12
|
|
|
{ |
13
|
|
|
/** @var ServerTiming */ |
14
|
|
|
protected $timing; |
15
|
|
|
|
16
|
|
|
/** @var float|mixed|string */ |
17
|
|
|
protected $start; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* ServerTimingMiddleware constructor. |
21
|
|
|
* @param ServerTiming $timing |
22
|
|
|
*/ |
23
|
|
|
public function __construct(ServerTiming $timing) |
24
|
|
|
{ |
25
|
|
|
$this->timing = $timing; |
26
|
|
|
$this->start = $this->getRequestStartTime(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Request $request |
31
|
|
|
* @param Closure $next |
32
|
|
|
* @return mixed|Response |
33
|
|
|
*/ |
34
|
|
|
public function handle(Request $request, Closure $next) |
35
|
|
|
{ |
36
|
|
|
if(false === config('timing.enabled', true)) { |
37
|
|
|
return $next($request); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->timing->setDuration('Bootstrap', $this->getElapsedTimeInMs()); |
41
|
|
|
|
42
|
|
|
$this->timing->start('App'); |
43
|
|
|
|
44
|
|
|
/** @var Response $response */ |
45
|
|
|
$response = $next($request); |
46
|
|
|
|
47
|
|
|
$this->timing->stop('App'); |
48
|
|
|
|
49
|
|
|
$this->timing->stopAllUnfinishedEvents(); |
50
|
|
|
|
51
|
|
|
$this->timing->setDuration('Total', $this->getElapsedTimeInMs()); |
52
|
|
|
|
53
|
|
|
$response->headers->set('Server-Timing', $this->generateHeaders()); |
54
|
|
|
|
55
|
|
|
return $response; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Getting elapsed time in milliseconds |
60
|
|
|
* @return float|int |
61
|
|
|
*/ |
62
|
|
|
protected function getElapsedTimeInMs() |
63
|
|
|
{ |
64
|
|
|
return (microtime(true) - $this->start) * 1000; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Getting request start time in milliseconds |
69
|
|
|
* @return float|mixed|string |
70
|
|
|
*/ |
71
|
|
|
protected function getRequestStartTime() |
72
|
|
|
{ |
73
|
|
|
if (defined('LARAVEL_START')) { |
74
|
|
|
return LARAVEL_START; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $_SERVER["REQUEST_TIME_FLOAT"] ?? microtime(true); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Generate Server-Timing headers |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
protected function generateHeaders(): string |
85
|
|
|
{ |
86
|
|
|
$header = ''; |
87
|
|
|
|
88
|
|
|
foreach ($this->timing->events() as $eventName => $duration) { |
89
|
|
|
$eventNameSlug = Str::slug($eventName); |
90
|
|
|
|
91
|
|
|
$header .= "${eventNameSlug};desc=\"${eventName}\";"; |
92
|
|
|
|
93
|
|
|
if (!is_null($duration)) { |
94
|
|
|
$header .= "dur=${duration}"; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$header .= ", "; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $header; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|