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 | 20 | 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 | 14 | public function generate() |
|
116 | { |
||
117 | 14 | $t = (int) floor($this->timer->getUnixTimestamp()-$this->epoch); |
|
118 | 14 | if ($t !== $this->lastTime) { |
|
119 | 14 | if ($t < $this->lastTime) { |
|
120 | 1 | throw new UnexpectedValueException( |
|
121 | 'Time moved backwards. We cannot generate IDs for ' |
||
122 | 1 | .($this->lastTime-$t).' milliseconds' |
|
123 | 1 | ); |
|
124 | 14 | } elseif ($t < 0) { |
|
125 | 1 | throw new UnexpectedValueException( |
|
126 | 'Time is currently set before our epoch - unable ' |
||
127 | 1 | .'to generate IDs for '.(-$t).' milliseconds' |
|
128 | 1 | ); |
|
129 | 13 | } elseif ($t > self::MAX_ADJUSTED_TIMESTAMP) { |
|
130 | 1 | throw new OverflowException( |
|
131 | 'Timestamp overflow (past end of lifespan) - unable to generate any more IDs' |
||
132 | 1 | ); |
|
133 | } |
||
134 | 12 | $this->sequence = 0; |
|
135 | 12 | $this->lastTime = $t; |
|
136 | 12 | } else { |
|
137 | 8 | ++$this->sequence; |
|
138 | 8 | if ($this->sequence > 4095) { |
|
139 | 1 | throw new OverflowException( |
|
140 | 'Sequence overflow (too many IDs generated) - unable to generate IDs for 1 milliseconds' |
||
141 | 1 | ); |
|
142 | } |
||
143 | } |
||
144 | |||
145 | 12 | if (PHP_INT_SIZE === 4) { |
|
146 | return $this->mintId32($t, $this->machine, $this->sequence); |
||
147 | } else { |
||
148 | 12 | return $this->mintId64($t, $this->machine, $this->sequence); |
|
149 | } |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Get stats. |
||
154 | * |
||
155 | * @return GeneratorStatus |
||
156 | */ |
||
157 | 1 | public function status() |
|
158 | { |
||
159 | 1 | return new GeneratorStatus($this->machine, $this->lastTime, $this->sequence, (PHP_INT_SIZE === 4)); |
|
160 | } |
||
161 | |||
162 | private function mintId32($timestamp, $machine, $sequence) |
||
177 | |||
178 | 12 | private function mintId64($timestamp, $machine, $sequence) |
|
185 | |||
186 | private function hexdec($hex) |
||
196 | } |
||
197 |