This is an automated email from the ASF dual-hosted git repository. wangxin pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/dubbo-php-framework.git
commit 56045c060c25faf206de0be47ed1ae57b970c7f2 Author: wangjinxi <wangji...@che001.com> AuthorDate: Wed Jul 24 16:26:50 2019 +0800 add Automatic identification of parameter types --- common/protocol/fsof/DubboParser.php | 4 +- common/protocol/fsof/DubboRequest.php | 12 +- consumer/Type.php | 228 +++++++++------------------------- consumer/proxy/Proxy.php | 13 +- 4 files changed, 75 insertions(+), 182 deletions(-) diff --git a/common/protocol/fsof/DubboParser.php b/common/protocol/fsof/DubboParser.php index 921e912..0497f5f 100644 --- a/common/protocol/fsof/DubboParser.php +++ b/common/protocol/fsof/DubboParser.php @@ -106,7 +106,7 @@ class DubboParser } $reqData .= json_encode($request->getMethod()) . PHP_EOL; $reqData .= json_encode($this->typeRefs($request)) . PHP_EOL; - foreach (Type::getDataForSafed($request->getParams()) as $value) { + foreach ($request->getParams() as $value) { $reqData .= json_encode($value) . PHP_EOL; } $attach = array(); @@ -138,7 +138,7 @@ class DubboParser } $reqData .= $encode->encode($request->getMethod()); $reqData .= $encode->encode($this->typeRefs($request)); - foreach (Type::getDataForSafed($request->getParams()) as $value) { + foreach ($request->getParams() as $value) { $reqData .= $encode->encode($value); } $attach = ['path' => $request->getService(), 'interface' => $request->getService(), 'timeout' => $request->getTimeout()]; diff --git a/common/protocol/fsof/DubboRequest.php b/common/protocol/fsof/DubboRequest.php index cb30cb7..8ec2c8e 100644 --- a/common/protocol/fsof/DubboRequest.php +++ b/common/protocol/fsof/DubboRequest.php @@ -17,6 +17,8 @@ */ namespace com\fenqile\fsof\common\protocol\fsof; +use com\fenqile\fsof\consumer\Type; + class DubboRequest { //包头字段 @@ -89,7 +91,6 @@ class DubboRequest } - /** * @return boolean */ @@ -107,8 +108,6 @@ class DubboRequest } - - /** * @return mixed */ @@ -142,7 +141,6 @@ class DubboRequest } - /** * @return mixed */ @@ -269,6 +267,11 @@ class DubboRequest public function setParams($params) { $this->params = $params; + foreach ($this->params as &$val) { + if ($val instanceof Type) { + $val = $val->object; + } + } } /** @@ -320,7 +323,6 @@ class DubboRequest } - //用于监控service中各方法的性能 public $startTime; //开始处理请求的时间 public $endTime; //请求处理结束的时间 diff --git a/consumer/Type.php b/consumer/Type.php index 673d507..20c8123 100644 --- a/consumer/Type.php +++ b/consumer/Type.php @@ -2,8 +2,12 @@ namespace com\fenqile\fsof\consumer; +use com\fenqile\fsof\consumer\ConsumerException; +use Icecave\Collections\Collection; + class Type { + /* const SHORT = 1; const INT = 2; const INTEGER = 2; @@ -13,10 +17,13 @@ class Type const STRING = 6; const BOOL = 7; const BOOLEAN = 7; - const ARRAYLIST = 8; - const MAP = 9; + const MAP = 8; + */ + const ARRAYLIST = 9; + const DEFAULT_TYPE = 10; const adapter = [ + /* Type::SHORT => 'S', Type::INT => 'I', Type::LONG => 'J', @@ -24,193 +31,82 @@ class Type Type::DOUBLE => 'D', Type::BOOLEAN => 'Z', Type::STRING => 'Ljava/lang/String;', + Type::MAP => 'Ljava/util/Map;', + */ Type::ARRAYLIST => 'Ljava/util/ArrayList;', - Type::MAP => 'Ljava/util/Map;' + Type::DEFAULT_TYPE => 'Ljava/lang/Object;' ]; - public function __construct($type, $value) - { - $this->type = $type; - $this->value = $value; - } - - /** - * Short type - * - * @param integer $value - * @return Type - */ - public static function short($value) - { - return new self(self::SHORT, $value); - } - - /** - * Int type - * - * @param integer $value - * @return Type - */ - public static function int($value) - { - return new self(self::INT, $value); - } - - /** - * Integer type - * - * @param integer $value - * @return Type - */ - public static function integer($value) - { - return new self(self::INTEGER, $value); - } - - /** - * Long type - * - * @param integer $value - * @return Type - */ - public static function long($value) - { - return new self(self::LONG, $value); - } - - /** - * Float type - * - * @param integer $value - * @return Type - */ - public static function float($value) - { - return new self(self::FLOAT, $value); - } - - /** - * Double type - * - * @param integer $value - * @return Type - */ - public static function double($value) - { - return new self(self::DOUBLE, $value); - } - - /** - * String type - * - * @param string $value - * @return Type - */ - public static function string($value) - { - return new self(self::STRING, $value); - } - - /** - * Bool type - * - * @param boolean $value - * @return Type - */ - public static function bool($value) - { - return new self(self::BOOL, $value); - } - - /** - * Boolean type - * - * @param boolean $value - * @return Type - */ - public static function boolean($value) + private function __construct() { - return new self(self::BOOLEAN, $value); } /** - * Arraylist type * - * @param arraylist $value - * @return Type + * @param integer $value + * @return UniversalObject */ - public static function arrayList($value) + public static function object($class, $properties) { - return new self(self::ARRAYLIST, $value); + $typeObj = new self(); + $typeObj->className = $class; + $std = new \stdClass; + foreach ($properties as $key => $value) { + $std->$key = $value; + } + $typeObj->object = $std; + return $typeObj; } /** - * Map type * - * @param map $value - * @return Type + * @param mixed $arg + * @return string + * @throws ConsumerException */ - public static function map($value) + public static function argToType($arg) { - return new self(self::MAP, $value); + $type = gettype($arg); + switch ($type) { + case 'integer': + case 'boolean': + case 'double': + case 'string': + case 'NULL': + return self::adapter[Type::DEFAULT_TYPE]; + case 'array': + if (Collection::isSequential($arg)) { + return self::adapter[Type::ARRAYLIST]; + } else { + return self::adapter[Type::DEFAULT_TYPE]; + } + case 'object': + if ($arg instanceof Type) { + $className = $arg->className; + } else { + $className = get_class($arg); + } + return 'L' . str_replace(['.', '\\'], '/', $className) . ';'; + default: + throw new ConsumerException("Handler for type {$type} not implemented"); + } } - /** - * Object type * - * @param integer $value - * @return UniversalObject + * @param int $arg + * @return int */ - public static function object($class, $properties) - { - $std = new \stdClass; - foreach ($properties as $key => $value) - { - $std->$key = ($value instanceof Type) ? self::typeTosafe($value) : $value; - } - $std_wrap = new \stdClass(); - $std_wrap->object = $std; - $std_wrap->class = 'L'.str_replace('.', '/', $class).';'; - return $std_wrap; - } - public static function getDataForSafed($args) + /* + private static function numToType($value) { - foreach ($args as &$value) - { - if ($value instanceof \stdClass) { - $value = $value->object; - } elseif ($value instanceof Type) { - $value = self::typeTosafe($value); - } + if (-32768 <= $value && $value <= 32767) { + return Type::SHORT; + } elseif (-2147483648 <= $value && $value <= 2147483647) { + return Type::INT; } - return $args; + return Type::LONG; } + */ - public static function typeTosafe(Type $type) - { - switch ($type->type){ - case Type::SHORT: - case Type::INT: - case Type::LONG: - $value = (int)$type->value; - break; - case Type::FLOAT: - case Type::DOUBLE: - $value = (float)$type->value; - break; - case Type::BOOLEAN: - $value = (bool)$type->value; - break; - case Type::ARRAYLIST: - case Type::MAP: - $value = (array)$type->value; - break; - case Type::STRING: - default: - $value = (string)$type->value; - break; - } - return $value; - } } \ No newline at end of file diff --git a/consumer/proxy/Proxy.php b/consumer/proxy/Proxy.php index 723a4ba..3914ee4 100644 --- a/consumer/proxy/Proxy.php +++ b/consumer/proxy/Proxy.php @@ -18,6 +18,7 @@ namespace com\fenqile\fsof\consumer\proxy; use com\fenqile\fsof\common\protocol\fsof\DubboRequest; +use com\fenqile\fsof\consumer\ConsumerException; use com\fenqile\fsof\consumer\fsof\FSOFProcessor; use com\fenqile\fsof\consumer\Type; @@ -85,13 +86,7 @@ final class Proxy { $types = []; foreach ($args as $val) { - if($val instanceof \stdClass){ - $types[] = $val->class; - }else if($val instanceof Type){ - $types[] = Type::adapter[$val->type]??'Ljava/lang/Object;'; - }else{ - $types[] = 'Ljava/lang/Object;'; - } + $types[] = Type::argToType($val); } return $types; } @@ -119,14 +114,14 @@ final class Proxy $request->setService($this->serviceInterface); $request->setMethod($args[0]); array_shift($args); + $request->setTypes($this->generateParamType($args)); $request->setParams($args); - $request->setTypes($this->generateParamType($request->getParams())); $result = $this->fsofProcessor->executeRequest($request, $this->serviceAddress, $this->ioTimeOut, $providerAddress); }catch (\Exception $e) { $cost_time = (int)((microtime(true) - $begin_time) * 1000000); //记录consumer接口告警日志 $this->setAccLog($request, $cost_time, $e->getMessage()); - throw $e; + throw new ConsumerException($e->getMessage(), $e); } $cost_time = (int)((microtime(true) - $begin_time) * 1000000); //记录consumer接口告警日志