Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Switched GUID base implementation from uniqid() to UUID v4

  • Loading branch information...
commit 9f04ac95a6ecac6e813ec2014b939c7a21ccb7ef 1 parent ff01a31
Doug Hurst authored June 25, 2011
29  library/Tracks/Model/Guid.php
@@ -59,7 +59,34 @@ public function __toString()
59 59
      */
60 60
     static public function create()
61 61
     {
62  
-        return new self(uniqid('', true));
  62
+        return new self(Guid::uuid());
  63
+    }
  64
+
  65
+    /**
  66
+     * UUID v4
  67
+     *
  68
+     * @return string
  69
+     * @link   http://www.php.net/manual/en/function.uniqid.php#94959
  70
+     * @link   http://www.ietf.org/rfc/rfc4122.txt
  71
+     */
  72
+    public static function uuid()
  73
+    {
  74
+        return sprintf(
  75
+            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  76
+            // 32 bits for "time_low"
  77
+            mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  78
+            // 16 bits for "time_mid"
  79
+            mt_rand(0, 0xffff),
  80
+            // 16 bits for "time_hi_and_version",
  81
+            // four most significant bits holds version number 4
  82
+            mt_rand(0, 0x0fff) | 0x4000,
  83
+            // 16 bits, 8 bits for "clk_seq_hi_res",
  84
+            // 8 bits for "clk_seq_low",
  85
+            // two most significant bits holds zero and one for variant DCE1.1
  86
+            mt_rand(0, 0x3fff) | 0x8000,
  87
+            // 48 bits for "node"
  88
+            mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  89
+        );
63 90
     }
64 91
 
65 92
     /** @var string */
4  schema/event_store-mysql.sql
@@ -9,7 +9,7 @@
9 9
 
10 10
 CREATE TABLE IF NOT EXISTS entity (
11 11
   id int(10) unsigned NOT NULL AUTO_INCREMENT,
12  
-  guid char(23) NOT NULL,
  12
+  guid char(36) NOT NULL,
13 13
   `type` varchar(255) NOT NULL,
14 14
   version int(10) unsigned NOT NULL DEFAULT '0',
15 15
   date_created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
@@ -20,7 +20,7 @@ CREATE TABLE IF NOT EXISTS entity (
20 20
 
21 21
 CREATE TABLE IF NOT EXISTS `event` (
22 22
   id int(10) unsigned NOT NULL AUTO_INCREMENT,
23  
-  guid char(23) NOT NULL,
  23
+  guid char(36) NOT NULL,
24 24
   `data` text NOT NULL,
25 25
   date_created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
26 26
   PRIMARY KEY (id)

0 notes on commit 9f04ac9

Please sign in to comment.
Something went wrong with that request. Please try again.