1 | <?php |
||
27 | class Generator |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * Max timestamp. |
||
32 | */ |
||
33 | const MAX_ADJUSTED_TIMESTAMP = 2199023255551; |
||
34 | |||
35 | /** |
||
36 | * Hexdec lookup. |
||
37 | * |
||
38 | * @staticvar array |
||
39 | */ |
||
40 | private static $hexdec = array( |
||
41 | '0' => 0, |
||
42 | '1' => 1, |
||
43 | '2' => 2, |
||
44 | '3' => 3, |
||
45 | '4' => 4, |
||
46 | '5' => 5, |
||
47 | '6' => 6, |
||
48 | '7' => 7, |
||
49 | '8' => 8, |
||
50 | '9' => 9, |
||
51 | 'a' => 10, |
||
52 | 'b' => 11, |
||
53 | 'c' => 12, |
||
54 | 'd' => 13, |
||
55 | 'e' => 14, |
||
56 | 'f' => 15, |
||
57 | ); |
||
58 | |||
59 | /** |
||
60 | * Timer. |
||
61 | * |
||
62 | * @var TimerInterface |
||
63 | */ |
||
64 | private $timer; |
||
65 | |||
66 | /** |
||
67 | * Configured machine ID - 10 bits (dec 0 -> 1023). |
||
68 | * |
||
69 | * @var int |
||
70 | */ |
||
71 | private $machine; |
||
72 | |||
73 | /** |
||
74 | * Epoch - in UTC millisecond timestamp. |
||
75 | * |
||
76 | * @var int |
||
77 | */ |
||
78 | private $epoch = 1325376000000; |
||
79 | |||
80 | /** |
||
81 | * Sequence number - 12 bits, we auto-increment for same-millisecond collisions. |
||
82 | * |
||
83 | * @var int |
||
84 | */ |
||
85 | private $sequence = 1; |
||
86 | |||
87 | /** |
||
88 | * The most recent millisecond time window encountered. |
||
89 | * |
||
90 | * @var int |
||
91 | */ |
||
92 | private $lastTime = null; |
||
93 | |||
94 | /** |
||
95 | * Constructor. |
||
96 | * |
||
97 | * @param @inject ConfigInterface $config |
||
98 | * @param @inject TimerInterface $timer |
||
99 | */ |
||
100 | 21 | public function __construct(ConfigInterface $config, TimerInterface $timer) |
|
110 | |||
111 | /** |
||
112 | * Generate ID. |
||
113 | * |
||
114 | * @return string A 64 bit integer as a string of numbers (so we can deal |
||
115 | * with this on 32 bit platforms) |
||
116 | */ |
||
117 | 15 | public function generate() |
|
132 | |||
133 | /** |
||
134 | * Return true, if we are on 32 bit platform. |
||
135 | * |
||
136 | * @return boolean |
||
137 | */ |
||
138 | 12 | protected function is32Bit() |
|
142 | |||
143 | /** |
||
144 | * Generate new ID with different time. |
||
145 | * |
||
146 | * @param integer $t |
||
147 | * @throws UnexpectedValueException |
||
148 | * @throws OverflowException |
||
149 | */ |
||
150 | 15 | private function generateWithDifferentTime($t) |
|
170 | |||
171 | /** |
||
172 | * Generate new ID with same time. |
||
173 | * |
||
174 | * @throws OverflowException |
||
175 | */ |
||
176 | 7 | private function generateWithSameTime() |
|
185 | |||
186 | /** |
||
187 | * Get stats. |
||
188 | * |
||
189 | * @return GeneratorStatus |
||
190 | */ |
||
191 | 1 | public function status() |
|
196 | |||
197 | 1 | private function mintId32($timestamp, $machine, $sequence) |
|
212 | |||
213 | 12 | private function mintId64($timestamp, $machine, $sequence) |
|
220 | |||
221 | 1 | private function hexdec($hex) |
|
231 | |||
232 | } |
||
233 |