1 | <?php |
||
27 | class Generator |
||
28 | { |
||
29 | /** |
||
30 | * Max timestamp. |
||
31 | */ |
||
32 | const MAX_ADJUSTED_TIMESTAMP = 2199023255551; |
||
33 | |||
34 | /** |
||
35 | * Hexdec lookup. |
||
36 | * |
||
37 | * @staticvar array |
||
38 | */ |
||
39 | private static $hexdec = array( |
||
40 | '0' => 0, |
||
41 | '1' => 1, |
||
42 | '2' => 2, |
||
43 | '3' => 3, |
||
44 | '4' => 4, |
||
45 | '5' => 5, |
||
46 | '6' => 6, |
||
47 | '7' => 7, |
||
48 | '8' => 8, |
||
49 | '9' => 9, |
||
50 | 'a' => 10, |
||
51 | 'b' => 11, |
||
52 | 'c' => 12, |
||
53 | 'd' => 13, |
||
54 | 'e' => 14, |
||
55 | 'f' => 15, |
||
56 | ); |
||
57 | |||
58 | /** |
||
59 | * Timer. |
||
60 | * |
||
61 | * @var TimerInterface |
||
62 | */ |
||
63 | private $timer; |
||
64 | |||
65 | /** |
||
66 | * Configured machine ID - 10 bits (dec 0 -> 1023). |
||
67 | * |
||
68 | * @var int |
||
69 | */ |
||
70 | private $machine; |
||
71 | |||
72 | /** |
||
73 | * Epoch - in UTC millisecond timestamp. |
||
74 | * |
||
75 | * @var int |
||
76 | */ |
||
77 | private $epoch = 1325376000000; |
||
78 | |||
79 | /** |
||
80 | * Sequence number - 12 bits, we auto-increment for same-millisecond collisions. |
||
81 | * |
||
82 | * @var int |
||
83 | */ |
||
84 | private $sequence = 1; |
||
85 | |||
86 | /** |
||
87 | * The most recent millisecond time window encountered. |
||
88 | * |
||
89 | * @var int |
||
90 | */ |
||
91 | private $lastTime = null; |
||
92 | |||
93 | /** |
||
94 | * Config. |
||
95 | * |
||
96 | * @var ConfigInterface |
||
97 | */ |
||
98 | private $config; |
||
99 | |||
100 | /** |
||
101 | * Constructor. |
||
102 | * |
||
103 | * @param @inject ConfigInterface $config |
||
104 | * @param @inject TimerInterface $timer |
||
105 | */ |
||
106 | 22 | public function __construct(ConfigInterface $config, TimerInterface $timer) |
|
117 | |||
118 | /** |
||
119 | * Generate ID. |
||
120 | * |
||
121 | * @return string A 64 bit integer as a string of numbers (so we can deal |
||
122 | * with this on 32 bit platforms) |
||
123 | */ |
||
124 | 16 | public function generate() |
|
139 | |||
140 | /** |
||
141 | * Return true, if we are on 32 bit platform. |
||
142 | * |
||
143 | * @return bool |
||
144 | */ |
||
145 | 13 | protected function is32Bit() |
|
149 | |||
150 | /** |
||
151 | * Generate new ID with different time. |
||
152 | * |
||
153 | * @param int $t |
||
154 | * |
||
155 | * @throws UnexpectedValueException |
||
156 | * @throws OverflowException |
||
157 | */ |
||
158 | 16 | private function generateWithDifferentTime($t) |
|
178 | |||
179 | /** |
||
180 | * Generate new ID with same time. |
||
181 | * |
||
182 | * @throws OverflowException |
||
183 | */ |
||
184 | 9 | private function generateWithSameTime() |
|
193 | |||
194 | /** |
||
195 | * Get stats. |
||
196 | * |
||
197 | * @return GeneratorStatus |
||
198 | */ |
||
199 | 1 | public function status() |
|
204 | |||
205 | /** |
||
206 | * Perform configuration heartbeat. |
||
207 | * |
||
208 | * This refreshes the configuration and may eventually result in obtaining new machine ID. |
||
209 | * It may be usefull, when we want to perform garbage collection for stalled machine IDs |
||
210 | * in some configuration mechanisms. |
||
211 | */ |
||
212 | 1 | public function heartbeat() |
|
220 | |||
221 | 1 | private function mintId32($timestamp, $machine, $sequence) |
|
236 | |||
237 | 13 | private function mintId64($timestamp, $machine, $sequence) |
|
244 | |||
245 | 1 | private function hexdec($hex) |
|
255 | } |
||
256 |