|
2 | 2 |
|
3 | 3 | namespace PHPCR\Util; |
4 | 4 |
|
5 | | -use Rhumsaa\Uuid\Uuid; |
| 5 | +use Ramsey\Uuid\Uuid; |
6 | 6 |
|
7 | 7 | /** |
8 | 8 | * Static helper functions to deal with Universally Unique IDs (UUID). |
@@ -32,12 +32,39 @@ public static function isUUID($id) |
32 | 32 | /** |
33 | 33 | * Generate a UUID. |
34 | 34 | * |
| 35 | + * This UUID can not be guaranteed to be unique within the repository. |
| 36 | + * Ensuring this is the responsibility of the repository implementation. |
| 37 | + * |
| 38 | + * It also allows the use of Ramsey\Uuid\Uuid class. |
| 39 | + * |
35 | 40 | * @return string a random UUID |
36 | 41 | */ |
37 | 42 | public static function generateUUID() |
38 | 43 | { |
39 | | - $uuid4 = Uuid::uuid4(); |
| 44 | + if (class_exists('Ramsey\Uuid\Uuid')) { |
| 45 | + $uuid4 = Uuid::uuid4(); |
| 46 | + |
| 47 | + return $uuid4->toString(); |
| 48 | + } |
| 49 | + |
| 50 | + return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
| 51 | + // 32 bits for "time_low" |
| 52 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
| 53 | + |
| 54 | + // 16 bits for "time_mid" |
| 55 | + mt_rand(0, 0xffff), |
| 56 | + |
| 57 | + // 16 bits for "time_hi_and_version", |
| 58 | + // four most significant bits holds version number 4 |
| 59 | + mt_rand(0, 0x0fff) | 0x4000, |
| 60 | + |
| 61 | + // 16 bits, 8 bits for "clk_seq_hi_res", |
| 62 | + // 8 bits for "clk_seq_low", |
| 63 | + // two most significant bits holds zero and one for variant DCE1.1 |
| 64 | + mt_rand(0, 0x3fff) | 0x8000, |
40 | 65 |
|
41 | | - return $uuid4->toString(); |
| 66 | + // 48 bits for "node" |
| 67 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) |
| 68 | + ); |
42 | 69 | } |
43 | 70 | } |
0 commit comments