Permalink
Browse files

added uuid plugin handler

  • Loading branch information...
1 parent d8a1fbd commit 2b723f78a3761221c5f567f5876875ad187ffbad @mjpearson committed May 11, 2010
Showing with 157 additions and 39 deletions.
  1. +1 −1 config.php
  2. +3 −0 lib/loader.php
  3. +48 −38 lib/uuid/UUID.class.php
  4. +45 −0 lib/uuid/UUIDPlugin.interface.php
  5. +60 −0 lib/uuid/UUIDPluginOSSP.class.php
View
@@ -23,5 +23,5 @@
define('THRIFT_PORT_DEFAULT', 9160);
define('DEFAULT_ROW_LIMIT', 10);
define('PERSIST_CONNECTIONS', FALSE); // TSocket Persistence
-define('CASSANDRA_CONF_PATH', '/usr/local/src/apache-cassandra-0.6.0/conf/storage-conf.xml');
+define('CASSANDRA_CONF_PATH', '/usr/local/src/apache-cassandra-0.6.1/conf/storage-conf.xml');
?>
View
@@ -11,6 +11,7 @@
* @package pandra
*/
define('PANDRA_64', PHP_INT_SIZE == 8);
+define('PANDRA_INSTALL_DIR', dirname(__FILE__));
function _pandraAutoLoad($className) {
// seperate classes and interfaces for clarity
$fExt = array('.class.php', '.interface.php');
@@ -42,4 +43,6 @@ function _pandraAutoLoad($className) {
// Setup our capabilities
PandraCore::setMemcachedAvailable(class_exists('Memcached'));
PandraCore::setAPCAvailable(function_exists('apc_sma_info') && apc_sma_info() !== FALSE);
+
+UUID::auto();
?>
View
@@ -1,69 +1,79 @@
<?php
/**
- * Generates v1 (Timestamp) and v5 (Lexical, SHA-1) UUID's for use with Cassandra
- *
- * Requires OSSP PHP-UUID module
+ * UUID plugin registrar
*
* @author Michael Pearson <pandra-support@phpgrease.net>
- * @author Marius Karthaus
- * @link http://www.php.net/manual/en/function.uniqid.php#88434
+ * @copyright 2010 phpgrease.net
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
+ * @version 0.2
+ * @package pandra
*/
class UUID {
- public static $_uuid;
-
const UUID_BIN = 0;
const UUID_STR = 1;
- const UUID_FMT_STR = UUID_FMT_STR;
+ static private $_pluginRef = NULL;
- const UUID_FMT_BIN = UUID_FMT_BIN;
+ static private $_pluginPfx = 'UUIDPlugin';
- const UUID_MAKE_V1 = UUID_MAKE_V1;
-
- const UUID_MAKE_V5 = UUID_MAKE_V5;
+ /**
+ * Automatically registers the first uuid plugin which meets capability
+ */
+ public static function auto() {
+ $dir = PANDRA_INSTALL_DIR.'/uuid/';
+ $files = scandir($dir);
+ $ok = FALSE;
+ foreach ($files as $fname) {
+ if ($fname == '..' ||
+ $fname == '..' ||
+ !preg_match('/^'.(self::$_pluginPfx).'.*\.class.php/', $fname)) {
+ continue;
+ }
+
+ $tokens = explode('.', $fname);
+ $className = preg_replace('/^'.(self::$_pluginPfx).'/', '', $tokens[0]);
+ if (self::register($className)) {
+ PandraLog::info('Registered UUID class '.$className);
+ $ok = TRUE;
+ break;
+ }
+ }
+ return $ok;
+ }
- private static function instance() {
- if (!is_resource(self::$_uuid)) {
- uuid_create(&self::$_uuid);
+ private static function bridge($function) {
+ if (self::$_pluginRef !== NULL) {
+ return call_user_func(array(self::$_pluginRef, $function));
}
- return self::$_uuid;
+ throw new RuntimeException('No UUID Plugins initialised');
}
public static function generate() {
- return self::v1();
+ return self::bridge('generate');
}
- /**
- * returns a type 1 (MAC address and time based) uuid
- * @return string
- */
public static function v1() {
- return self::_generate(self::UUID_MAKE_V1);
+ return self::bridge('v1');
}
- /**
- * returns a type 5 (SHA-1 hash) uuid
- * @return string
- */
- public static function v5() {
- return self::_generate(self::UUID_MAKE_V5);
+ public static function v4() {
+ return self::bridge('v4');
}
- private static function _generate($type) {
- uuid_make ( self::instance(), $type );
- uuid_export ( self::instance(), self::UUID_FMT_STR, &$uuidstring );
- return trim ( $uuidstring );
+ public static function v5() {
+ return self::bridge('v5');
}
- public static function convert($uuid, $toFmt) {
- if ($toFmt == self::UUID_FMT_BIN) {
- return self::toBin($uuid);
- } elseif ($toFmt == self::UUID_FMT_STR) {
- return self::toStr($uuid);
+ public static function register($pluginName) {
+ $className = 'Pandra'.self::$_pluginPfx.$pluginName;
+
+ if (class_exists($className) && $className::isCapable()) {
+ self::$_pluginRef = $className;
+ return TRUE;
}
- return $uuid;
+ return FALSE;
}
public static function toStr($uuid) {
@@ -0,0 +1,45 @@
+<?php
+/**
+ * UUIDPlugin
+ *
+ * Plugin interface for UUID libraries
+ *
+ * @author Michael Pearson <pandra-support@phpgrease.net>
+ * @copyright 2010 phpgrease.net
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
+ * @version 0.2
+ * @package pandra
+ */
+interface PandraUUIDPlugin {
+
+ /**
+ * Checks that local dependencies are met
+ * @return bool UUID library dependencies met (module exists)
+ */
+ public static function isCapable();
+
+ /**
+ * Generates a string UUID of a default type (usually v1, time based)
+ * @return string new timeuuid
+ */
+ public static function generate();
+
+ /**
+ * returns a type 1 (MAC address and time based) uuid
+ * @return string
+ */
+ public static function v1();
+
+ /**
+ * returns a type 4 (random) uuid
+ * @return string
+ */
+ public static function v4();
+
+ /**
+ * returns a type 5 (SHA-1 hash) uuid
+ * @return string
+ */
+ public static function v5();
+}
+?>
@@ -0,0 +1,60 @@
+<?php
+/**
+ * OSSP UUID plugin
+ *
+ * @author Michael Pearson <pandra-support@phpgrease.net>
+ * @copyright 2010 phpgrease.net
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
+ * @version 0.2
+ * @package pandra
+ */
+class PandraUUIDPluginOSSP implements PandraUUIDPlugin {
+
+ private static $_uuid;
+
+ public static function isCapable() {
+ return (defined('UUID_MAKE_V1') && function_exists('uuid_create'));
+ }
+
+ private static function instance() {
+ if (!is_resource(self::$_uuid)) {
+ uuid_create(&self::$_uuid);
+ }
+ return self::$_uuid;
+ }
+
+ public static function generate() {
+ return self::_generate(UUID_MAKE_V1);
+ }
+
+ /**
+ * returns a type 1 (MAC address and time based) uuid
+ * @return string
+ */
+ public static function v1() {
+ return self::_generate(UUID_MAKE_V1);
+ }
+
+ /**
+ * returns a type 4 (random) uuid
+ * @return string
+ */
+ public static function v4() {
+ return self::_generate(UUID_MAKE_V4);
+ }
+
+ /**
+ * returns a type 5 (SHA-1 hash) uuid
+ * @return string
+ */
+ public static function v5() {
+ return self::_generate(UUID_MAKE_V5);
+ }
+
+ private static function _generate($type) {
+ uuid_make ( self::instance(), $type );
+ uuid_export ( self::instance(), UUID_FMT_STR, &$uuidstring );
+ return trim ( $uuidstring );
+ }
+}
+?>

0 comments on commit 2b723f7

Please sign in to comment.