Created
July 21, 2011 14:25
-
-
Save ptz0n/1097298 to your computer and use it in GitHub Desktop.
Magic call method for setting and removing object references with Redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Basic call method for setting and removing object properties | |
* | |
* @param String $method | |
* @param Array $args | |
* | |
* @return Mixed | |
* | |
* @access public | |
*/ | |
public function __call($method, $args) | |
{ | |
$return = false; | |
$reference = isset($args[0]) ? $args[0] : null; | |
if($this->id && strlen($reference) > 0) { // If object is loaded | |
if(preg_match('~^(add|remove)([A-Z])(.*)$~', $method, $matches)) { | |
$class = strtolower($matches[2]) . $matches[3]; | |
if(!class_exists($class)) { // Check if class exists | |
$key = $this->_singular . ':' . $this->id . ':' . self::_pluralize($class); | |
} | |
else { | |
$instance = new $class(); | |
$key = $this->_singular . ':' . $this->id . ':' . $instance->_plural; | |
} | |
$type = self::$_redis->type($key); | |
if(isset($args[1]) && is_int($args[1])) { // If secound param is int | |
$score = $args[1]; // Setup value for the score in sorted set | |
} | |
if(is_string($reference)) { // If reference is string | |
$md5 = md5($reference); // Calculate MD5 hash from string | |
$md5Key = $this->_singular.':'.$this->id.':'.$md5; // Build the md5 key | |
} | |
if(isset($score) && !isset($md5)) { // If want to be stored as sorted set (zset) | |
if(($type == 0 || $type == 4) && $matches[1] == 'add') { // If key does not exists or is of type zset | |
$return = self::$_redis->zAdd($key, $score, $reference); | |
} | |
else { | |
throw new Exception('Key \'' . $key . '\' must be of type zset.'); | |
} | |
} | |
else { // Store as values in non-sorted (regular) set or removal of member in sorted set | |
switch($matches[1]) { | |
case 'add': | |
if(isset($md5) && !self::$_redis->exists($md5Key)) { // If md5Key is set md5Key not exists | |
self::$_redis->set($md5Key, $this->id); // Add reference to object | |
$return = self::$_redis->sAdd($key, $reference); // Add reference to objects list | |
} | |
elseif(!isset($md5)) { // Else if md5 is not set | |
$return = self::$_redis->sAdd($key, $reference); // Just add the reference to objects list | |
} | |
break; | |
case 'remove': | |
if($type == 4) { // If key is of type zset | |
$return = self::$_redis->zRemove($key, $reference); | |
} | |
else { // Else it's a regular set | |
if(isset($md5Key)) { | |
self::$_redis->delete($md5Key); | |
} | |
$return = self::$_redis->sRemove($key, $reference); | |
} | |
break; | |
} | |
} | |
} | |
else { | |
throw new Exception('Undefined method \'' . $method . '\''); | |
} | |
} | |
return $return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment