Skip to content

Commit 6f4d873

Browse files
committed
suggest using ramsey/uuid library to generate UUIDs
1 parent 4fbfb1d commit 6f4d873

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ valid UUID.
6666
We recommend all implementations to use this implementation to guarantee
6767
consistent behaviour.
6868

69+
**Note**
70+
71+
You can use [ramsey/uuid](https://github.com/ramsey/uuid) library to generate UUIDs. In this case,
72+
install it using Composer and generating UUIDs will be taken over by `ramsey/uuid`.
73+
6974
### QOM QueryBuilder
7075

7176
The ``QueryBuilder`` is a fluent query builder with method names matching the

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
"require": {
3030
"php": ">=5.3.3",
3131
"phpcr/phpcr": "~2.1.0",
32-
"symfony/console": "~2.3|~3.0",
33-
"ramsey/uuid": "~2.8"
32+
"symfony/console": "~2.3|~3.0"
33+
},
34+
"suggest": {
35+
"ramsey/uuid": "A PHP 5.4+ library for generating RFC 4122 version 1, 3, 4, and 5 universally unique identifiers (UUID)."
3436
},
3537
"conflict": {
3638
"jackalope/jackalope-jackrabbit": "<1.2.1"

src/PHPCR/Util/UUIDHelper.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace PHPCR\Util;
44

5-
use Rhumsaa\Uuid\Uuid;
5+
use Ramsey\Uuid\Uuid;
66

77
/**
88
* Static helper functions to deal with Universally Unique IDs (UUID).
@@ -32,12 +32,39 @@ public static function isUUID($id)
3232
/**
3333
* Generate a UUID.
3434
*
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+
*
3540
* @return string a random UUID
3641
*/
3742
public static function generateUUID()
3843
{
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,
4065

41-
return $uuid4->toString();
66+
// 48 bits for "node"
67+
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
68+
);
4269
}
4370
}

0 commit comments

Comments
 (0)