Skip to content

Commit 6d75459

Browse files
committed
Issue #1969572 by neclimdul, larowlan, hussainweb, dawehner, damiankloip, ParisLiakos, pwieck, Berdir, socketwench: Make Uuid a service.
1 parent ec13c8b commit 6d75459

File tree

4 files changed

+10
-65
lines changed

4 files changed

+10
-65
lines changed

Com.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* @file
5-
* Definition of Drupal\Component\Uuid\Com.
5+
* Contains \Drupal\Component\Uuid\Com.
66
*/
77

88
namespace Drupal\Component\Uuid;

Pecl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* @file
5-
* Definition of Drupal\Component\Uuid\Pecl.
5+
* Contains \Drupal\Component\Uuid\Pecl.
66
*/
77

88
namespace Drupal\Component\Uuid;

Php.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* @file
5-
* Definition of Drupal\Component\Uuid\Php.
5+
* Contains \Drupal\Component\Uuid\Php.
66
*/
77

88
namespace Drupal\Component\Uuid;
@@ -46,4 +46,5 @@ public function generate() {
4646

4747
return $uuid;
4848
}
49+
4950
}

Uuid.php

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,30 @@
22

33
/**
44
* @file
5-
* Definition of Drupal\Component\Uuid\Uuid.
5+
* Contains \Drupal\Component\Uuid\Uuid.
66
*/
77

88
namespace Drupal\Component\Uuid;
99

1010
/**
11-
* Factory class for UUIDs.
12-
*
13-
* Determines which UUID implementation to use, and uses that to generate
14-
* and validate UUIDs.
11+
* UUID Helper methods.
1512
*/
1613
class Uuid {
1714

18-
/**
19-
* Holds the UUID implementation.
20-
*
21-
* @var Drupal\Component\Uuid\UuidInterface
22-
*/
23-
protected $plugin;
24-
25-
/**
26-
* Instantiates the correct UUID object.
27-
*/
28-
public function __construct() {
29-
$class = $this->determinePlugin();
30-
$this->plugin = new $class();
31-
}
32-
33-
/**
34-
* Generates a universally unique identifier.
35-
*
36-
* @see Drupal\Component\Uuid\UuidInterface::generate()
37-
*/
38-
public function generate() {
39-
return $this->plugin->generate();
40-
}
41-
4215
/**
4316
* Checks that a string appears to be in the format of a UUID.
4417
*
45-
* Plugins should not implement validation, since UUIDs should be in a
46-
* consistent format across all plugins.
18+
* Implementations should not implement validation, since UUIDs should be in
19+
* a consistent format across all implementations.
4720
*
4821
* @param string $uuid
4922
* The string to test.
5023
*
5124
* @return bool
5225
* TRUE if the string is well formed, FALSE otherwise.
5326
*/
54-
public function isValid($uuid) {
55-
return preg_match("/^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$/", $uuid);
27+
public static function isValid($uuid) {
28+
return (bool) preg_match("/^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$/", $uuid);
5629
}
5730

58-
/**
59-
* Determines the optimal implementation to use for generating UUIDs.
60-
*
61-
* The selection is made based on the enabled PHP extensions with the
62-
* most performant available option chosen.
63-
*
64-
* @return string
65-
* The class name for the optimal UUID generator.
66-
*/
67-
protected function determinePlugin() {
68-
static $plugin;
69-
if (!empty($plugin)) {
70-
return $plugin;
71-
}
72-
73-
$plugin = 'Drupal\Component\Uuid\Php';
74-
75-
// Debian/Ubuntu uses the (broken) OSSP extension as their UUID
76-
// implementation. The OSSP implementation is not compatible with the
77-
// PECL functions.
78-
if (function_exists('uuid_create') && !function_exists('uuid_make')) {
79-
$plugin = 'Drupal\Component\Uuid\Pecl';
80-
}
81-
// Try to use the COM implementation for Windows users.
82-
elseif (function_exists('com_create_guid')) {
83-
$plugin = 'Drupal\Component\Uuid\Com';
84-
}
85-
return $plugin;
86-
}
8731
}

0 commit comments

Comments
 (0)