-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathServerTimings.php
More file actions
54 lines (45 loc) · 1.28 KB
/
ServerTimings.php
File metadata and controls
54 lines (45 loc) · 1.28 KB
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
<?php
namespace Fetzi\ServerTiming;
use Psr\Http\Message\ResponseInterface;
class ServerTimings
{
private array $timings = [];
/**
* creates a new ServerTiming instance and registers it.
*
* @param string $name the name of the server timing
* @param string $description the description for the server timing
*
* @return ServerTiming
*/
public function create(string $name, ?string $description = null)
{
$serverTiming = new ServerTiming($name, $description);
$this->timings[] = $serverTiming;
return $serverTiming;
}
/**
* returns the formatted Server-Timing header value.
*
* @return string
*/
public function getTimings(): ?string
{
if (empty($this->timings)) {
return null;
}
return implode(', ', $this->timings);
}
/**
* adds the stored server timings to the given response instance.
*
* @param ResponseInterface $response the response to add to
*/
public function addToResponse(ResponseInterface $response): ResponseInterface
{
if (!empty($this->timings)) {
$response = $response->withAddedHeader('Server-Timing', $this->getTimings());
}
return $response;
}
}