/**
  * Parse interface info
  *
  * @return ProxyClassInfo
  * @throws InitiallyRpcException
  */
 protected function parseProxyClassInfo()
 {
     $arr = explode("\\", $this->interface);
     $name = array_pop($arr);
     $matches = array();
     if (!preg_match($this->getClassNameRule(), $name, $matches)) {
         throw new InitiallyRpcException("Proxy builder error: interface name must ending of '{$this->interfaceEndOf}' like 'DemoService{$this->interfaceEndOf}'");
     }
     $replace = $this->config->getReplace();
     $replaceKey = $this->service->getReplaceKey();
     if (!empty($arr) && !empty($replace) && isset($replace[$replaceKey])) {
         foreach ($arr as $key => $value) {
             if (isset($replace[$replaceKey][$value])) {
                 $arr[$key] = $replace[$replaceKey][$value];
             }
         }
     }
     $namespace = implode("\\", $arr);
     $info = new ProxyClassInfo();
     $info->setNamespace($namespace);
     $info->setClassName($matches[1]);
     return $info;
 }
Exemplo n.º 2
0
 /**
  * Load client config
  *   <format>
  *     {
  *       "url": "",
  *       "transport": "",
  *       "proxyRootDir": "", // 代理类根目录,相对于配置文件存放的目录,目录必须存在
  *       "replace": { // 接口命名空间中需要替换的字段
  *         "search1": "replace1",
  *         "search2": "replace2"
  *       },
  *       "services": [
  *         {
  *           "interface": "",
  *           "url": "",
  *           "transport": "",
  *           "replace-key": ""
  *         },
  *         ...
  *       ]
  *     }
  *   </format>
  *
  * @return Client
  * @throws InitiallyRpcException
  */
 public static function client()
 {
     $configFile = ClientApp::getInstance()->getConfigFile();
     $content = file_get_contents($configFile);
     $config = json_decode($content, true);
     if (!is_array($config)) {
         throw new InitiallyRpcException("Client config error: format error");
     } else {
         if (!isset($config["proxyRootDir"])) {
             throw new InitiallyRpcException("Client config error: proxy root dir undefined");
         }
     }
     $configDir = dirname($configFile);
     $realProxyRootDir = realpath($configDir . "/" . $config["proxyRootDir"]);
     if ($realProxyRootDir === false) {
         throw new InitiallyRpcException("Client config error: proxy dir not exists");
     }
     $clientConfig = new Client();
     $issetGlobalUrl = isset($config["url"]);
     $issetGlobalTransport = isset($config["transport"]);
     $issetGlobalUrl && $clientConfig->setUrl($config["url"]);
     $issetGlobalTransport && $clientConfig->setTransport($config["transport"]);
     $clientConfig->setProxyRootDir($realProxyRootDir);
     if (isset($config["replace"]) && is_array($config["replace"])) {
         $clientConfig->setReplace($config["replace"]);
     }
     if (isset($config["services"]) && is_array($config["services"]) && !empty($config["services"])) {
         foreach ($config["services"] as $key => $value) {
             $issetUrl = isset($value["url"]);
             $issetTransport = isset($value["transport"]);
             $issetReplaceKey = isset($value["replace-key"]);
             if (!is_array($value)) {
                 throw new InitiallyRpcException("Client config error: undefined service");
             } else {
                 if (!isset($value["interface"])) {
                     throw new InitiallyRpcException("Client config error: undefined interface");
                 } else {
                     if (!$issetGlobalUrl && $issetUrl) {
                         throw new InitiallyRpcException("Client config error: undefined url");
                     } else {
                         if (!$issetGlobalTransport && $issetTransport) {
                             throw new InitiallyRpcException("Client config error: undefined transport");
                         }
                     }
                 }
             }
             $service = new Service();
             $service->setInterface($value["interface"]);
             $service->setUrl($issetUrl ? $value["url"] : $clientConfig->getUrl());
             $service->setTransport($issetTransport ? $value["transport"] : $clientConfig->getTransport());
             $service->setReplaceKey($issetReplaceKey ? $value["replace-key"] : "");
             $clientConfig->addService($service);
         }
     }
     return $clientConfig;
 }