public static function serialize($var, $simple = false) { return hprose_serialize($var, $simple); // $stream = new HproseStringStream(); // $hproseWriter = new HproseWriter($stream, $simple); // $hproseWriter->serialize($var); // return $stream->toString(); }
function handle($name, array &$args, stdClass $context, Closure $next) { if (isset($context->userdata->cache)) { $key = hprose_serialize($args); if (isset($this->cache[$name])) { if (isset($this->cache[$name][$key])) { return $this->cache[$name][$key]; } } else { $this->cache[$name] = array(); } $result = $next($name, $args, $context); $this->cache[$name][$key] = $result; return $result; } return $next($name, $args, $context); }
private function doInvoke() { // $simpleReader = new HproseReader($this->input, true); do { $functionName = hprose_unserialize_with_stream($this->input, true); // $functionName = $simpleReader->readString(); $aliasName = strtolower($functionName); $resultMode = HproseResultMode::Normal; if (array_key_exists($aliasName, $this->functions)) { $function = $this->functions[$aliasName]; $resultMode = $this->resultModes[$aliasName]; $simple = $this->simpleModes[$aliasName]; } elseif (array_key_exists('*', $this->functions)) { $function = $this->functions['*']; $resultMode = $this->resultModes['*']; $simple = $this->simpleModes['*']; } else { throw new Exception("Can't find this function " . $functionName . "()."); } if ($simple === NULL) { $simple = $this->simple; } // $writer = new HproseWriter($this->output, $simple); $args = array(); $byref = false; // $tag = $simpleReader->checkTags(array(HproseTags::TagList, // HproseTags::TagEnd, // HproseTags::TagCall)); $tag = $this->input->getc(); if ($tag == HproseTags::TagList) { // $reader = new HproseReader($this->input); // $args = &$reader->readListWithoutTag(); $args =& hprose_unserialize_list_with_stream($this->input); $tag = $this->input->getc(); // $tag = $reader->checkTags(array(HproseTags::TagTrue, // HproseTags::TagEnd, // HproseTags::TagCall)); if ($tag == HproseTags::TagTrue) { $byref = true; $tag = $this->input->getc(); // $tag = $reader->checkTags(array(HproseTags::TagEnd, // HproseTags::TagCall)); } } if ($tag != HproseTags::TagEnd && $tag != HproseTags::TagCall) { throw new Exception($tag); //throw new Exception("Wrong Request: \r\n" . $GLOBALS['HTTP_RAW_POST_DATA']); } if ($this->onBeforeInvoke) { call_user_func($this->onBeforeInvoke, $functionName, $args, $byref); } if (array_key_exists('*', $this->functions) && $function === $this->functions['*']) { $arguments = array($functionName, &$args); } elseif ($byref) { $arguments = array(); for ($i = 0; $i < count($args); $i++) { $arguments[$i] =& $args[$i]; } } else { $arguments = $args; } $result = call_user_func_array($function, $arguments); if ($this->onAfterInvoke) { call_user_func($this->onAfterInvoke, $functionName, $args, $byref, $result); } // some service functions/methods may echo content, we need clean it @ob_clean(); if ($resultMode == HproseResultMode::RawWithEndTag) { $this->output->write($result); return; } elseif ($resultMode == HproseResultMode::Raw) { $this->output->write($result); } else { $this->output->write(HproseTags::TagResult); if ($resultMode == HproseResultMode::Serialized) { $this->output->write($result); } else { // $writer->reset(); // $writer->serialize($result); $this->output->write(hprose_serialize($result, $simple)); } if ($byref) { $this->output->write(HproseTags::TagArgument . hprose_serialize_list($args, $simple)); // $writer->reset(); // $writer->writeList($args); } } } while ($tag == HproseTags::TagCall); $this->output->write(HproseTags::TagEnd); $this->responseEnd(); }
public function testReference() { $o = new Person(); $o->name = "tom"; $o->age = 18; $a = array($o, $o); $s = hprose_serialize($a); $this->assertEquals($s, 'a2{c6"Person"2{s4"name"s3"age"}o0{s3"tom"i18;}r3;}'); $this->assertEquals(hprose_unserialize($s), $a); }