1 | <?php |
||
26 | class Generator |
||
27 | { |
||
28 | /** |
||
29 | * Max timestamp. |
||
30 | */ |
||
31 | const MAX_ADJUSTED_TIMESTAMP = 2199023255551; |
||
32 | |||
33 | /** |
||
34 | * Hexdec lookup. |
||
35 | * |
||
36 | * @staticvar array |
||
37 | */ |
||
38 | private static $hexdec = array( |
||
39 | '0' => 0, |
||
40 | '1' => 1, |
||
41 | '2' => 2, |
||
42 | '3' => 3, |
||
43 | '4' => 4, |
||
44 | '5' => 5, |
||
45 | '6' => 6, |
||
46 | '7' => 7, |
||
47 | '8' => 8, |
||
48 | '9' => 9, |
||
49 | 'a' => 10, |
||
50 | 'b' => 11, |
||
51 | 'c' => 12, |
||
52 | 'd' => 13, |
||
53 | 'e' => 14, |
||
54 | 'f' => 15, |
||
55 | ); |
||
56 | |||
57 | /** |
||
58 | * Timer. |
||
59 | * |
||
60 | * @var TimerInterface |
||
61 | */ |
||
62 | private $timer; |
||
63 | |||
64 | /** |
||
65 | * Configured machine ID - 10 bits (dec 0 -> 1023). |
||
66 | * |
||
67 | * @var int |
||
68 | */ |
||
69 | private $machine; |
||
70 | |||
71 | /** |
||
72 | * Epoch - in UTC millisecond timestamp. |
||
73 | * |
||
74 | * @var int |
||
75 | */ |
||
76 | private $epoch = 1325376000000; |
||
77 | |||
78 | /** |
||
79 | * Sequence number - 12 bits, we auto-increment for same-millisecond collisions. |
||
80 | * |
||
81 | * @var int |
||
82 | */ |
||
83 | private $sequence = 1; |
||
84 | |||
85 | /** |
||
86 | * The most recent millisecond time window encountered. |
||
87 | * |
||
88 | * @var integer |
||
89 | */ |
||
90 | private $lastTime = null; |
||
91 | |||
92 | /** |
||
93 | * Constructor. |
||
94 | * |
||
95 | * @param @inject ConfigInterface $config |
||
96 | * @param @inject TimerInterface $timer |
||
97 | */ |
||
98 | 18 | public function __construct(ConfigInterface $config, TimerInterface $timer) |
|
108 | |||
109 | /** |
||
110 | * Generate ID. |
||
111 | * |
||
112 | * @return string A 64 bit integer as a string of numbers (so we can deal |
||
113 | * with this on 32 bit platforms) |
||
114 | */ |
||
115 | 12 | public function generate() |
|
151 | |||
152 | /** |
||
153 | * Get stats. |
||
154 | * |
||
155 | * @return GeneratorStatus |
||
156 | */ |
||
157 | public function status() |
||
161 | |||
162 | private function mintId32($timestamp, $machine, $sequence) |
||
177 | |||
178 | 10 | private function mintId64($timestamp, $machine, $sequence) |
|
185 | |||
186 | private function hexdec($hex) |
||
196 | } |
||
197 |