|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2014 SURFnet bv |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Surfnet\StepupBundle\Request; |
|
20
|
|
|
|
|
21
|
|
|
use LogicException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Exposes the current request ID. The request ID identifies all request/response cycles involved in a single |
|
25
|
|
|
* user/client interaction. |
|
26
|
|
|
*/ |
|
27
|
|
|
class RequestId |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var RequestIdGenerator |
|
31
|
|
|
*/ |
|
32
|
|
|
private $generator; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private $requestId; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param RequestIdGenerator $generator |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(RequestIdGenerator $generator) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->generator = $generator; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Returns the current request ID, optionally generating one if it doesn't exist. The request ID identifies all |
|
49
|
|
|
* request/response cycles involved in a single user/client interaction. |
|
50
|
|
|
* |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
public function get() |
|
54
|
|
|
{ |
|
55
|
|
|
if ($this->requestId === null) { |
|
56
|
|
|
$this->requestId = $this->generator->generateRequestId(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this->requestId; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* We allow overwriting the RequestId so that we can inject a RequestId from a header when log statements already |
|
64
|
|
|
* have been made - which would cause an exception otherwise. The use-case here is the Stepup-Middleware |
|
65
|
|
|
* application, this application receives API-calls, but by then log messages have already been written. |
|
66
|
|
|
* However, for the sake of correlation we do want to use the log to show the correct request_id when actually |
|
67
|
|
|
* handling the request. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $requestId |
|
70
|
|
|
* @param bool $allowOverwrite |
|
71
|
|
|
*/ |
|
72
|
|
|
public function set($requestId, $allowOverwrite = false) |
|
73
|
|
|
{ |
|
74
|
|
|
if ($this->requestId !== null && !$allowOverwrite) { |
|
75
|
|
|
throw new LogicException('May not overwrite request ID.'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->requestId = $requestId; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|