/** * This function implements the reading mechanism for a shared memory * segment following a specific layout. * @param $index position that should be read regarding the layout. * if $index is left -1 then the upcoming parameters will * be used to perform a search otherwise they are used to * perform checks on the elment addressed. * @param $skey string key for the element's $kpos'th node * @param $kpos $skey position regarding the segment layout * @param $val value of the last serialized element stored * if it is a string the type of the objects will be compared * otherwise the obejects will be compared * @return trimmed string for the whole contents of the segment if * $index is -1, if $index is >= 0 and valid the complete * element at this position, otherwise an empty string */ public function read($index = -1, $skey = "", $kpos = -1, $val = null, $full = true) { $rs = ""; $this->log(__METHOD__ . ": idx=%, skey=%, kpos=%, val=%, full=%", array($index, $skey, $kpos, StringUtil::get_object_string($val), $full)); // reading the whole contents if index is not set and the upcoming // parameters either if (strncmp(gettype($index), "integer", 7) == 0 && $index == -1 && strncmp(gettype($skey), "string", 6) == 0 && strlen($skey) == 0 && strncmp(gettype($kpos), "integer", 7) == 0 && $kpos == -1) { $s = shmop_read($this->element->get_shm_seg_id(), 0, $this->element->get_shm_seg_size()); if ($s) { $rs = trim($s); } // searching if the index is not set but parameters 2 to 5 are // but not with default values } else { if (strncmp(gettype($index), "integer", 7) == 0 && $index == -1 && strncmp(gettype($skey), "string", 6) == 0 && strlen($skey) > 0 && strncmp(gettype($kpos), "integer", 7) == 0 && $kpos > -1) { $rs = $this->search($index, $skey, $kpos, $val, $this->get_search_type()); // reading an index is a bit more complex } else { if (strncmp(gettype($index), "integer", 7) == 0 && $index > -1) { $xcontents = $this->get(); $xcli = count($xcontents) - 1; // now lets get the requested element if it exist // and complete its layout if ($index <= $xcli) { $rs = trim($xcontents[$index]) . $this->element->get_shm_seg_var_eright() . $this->element->get_shm_seg_var_delimiter(); } } } } if ($full) { return $rs; } else { return unserialize(substr($rs, strrpos($rs, $this->element->get_shm_seg_var_eleft()) + 1, strlen($rs) - strlen($this->element->get_shm_seg_var_eright() . $this->element->get_shm_seg_var_delimiter()) - strrpos($rs, $this->element->get_shm_seg_var_eleft()) - 1)); } }
/** * Function to flat several objects for logging. Arrays will be flattened. * For other objects there will be looked after a class method named * __toString(). If no such method exists the object signature will be used. * @param object $obj object to flatte for logging. * @param boolean $debug indicates to get detailed information for $obj * @return string flattened object, its string representation or a unique id */ public function flatten($obj = null, $debug = false) { $a = array(); // NOTE: get_class_methods invokes autoloader for a string value if (is_object($obj) && strncmp(gettype(get_class_methods($obj)), "array", 5) == 0) { $a = get_class_methods($obj); } $ret = ""; if (in_array("__toString", $a)) { $ret = $obj->__toString(); } else { if (strncmp(gettype($obj), "array", 5) == 0) { $ret = StringUtil::get_object_string($obj); } else { if (strncmp(gettype($obj), "string", 6) == 0) { $ret = $obj; } else { if (strncmp(gettype($obj), "integer", 7) == 0) { $ret = strval($obj); } else { if (is_null($obj)) { $ret = "NULL"; } else { if (is_object($obj)) { $ret = get_class($obj) . spl_object_hash($obj); } } } } } } if ($debug === true) { $ret = "OBJECT(" . StringUtil::get_object_string($obj) . ")=" . $ret; } // replace multiple whitespaces by a single one return $ret; }
/** * Set the segment type. Currently supported types be SharedMemoryWriter * are 'stor', 'fifo', 'lifo'. * @param $seg_type segment type */ public function set_shm_seg_type($seg_type = "stor") { if (in_array($seg_type, $this->shm_seg_supported_types, true)) { switch ($seg_type) { case "stor": $this->shm_seg_type = "stor"; break; case "fifo": $this->shm_seg_type = "fifo"; break; case "lifo": $this->shm_seg_type = "lifo"; break; default: $this->shm_seg_type = "stor"; break; } } else { $this->log(__METHOD__ . ": %", array(new SegmentException(StringUtil::get_object_string($seg_type), 3))); throw new SegmentException(StringUtil::get_object_string($seg_type), 3); } }