Exemplo n.º 1
0
 /**
  * Build proxy or not
  *
  * @throws \Initially\Rpc\Exception\InitiallyRpcException
  */
 public function buildProxyOrNot()
 {
     if (!Util::existsDirWritable(INITIALLY_RPC_ROOT_PATH)) {
         throw new InitiallyRpcException("rpc framework root dir not to be write");
     }
     if ($this->compareConfigFile()) {
         $services = $this->config->getServices();
         foreach ($services as $service) {
             $this->builder->create($service->getInterface());
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Invoke service
  *
  * @param Invocation $invocation
  * @return mixed
  * @throws InitiallyRpcException
  */
 public function invoke(Invocation $invocation)
 {
     $request = new Request();
     $request->setInterface($this->interface);
     $request->setMethodName($invocation->getMethodName());
     $request->setArguments($invocation->getArguments());
     $transport = Transport::factory($this->config->getTransport());
     if (!$transport instanceof Transport) {
         throw new InitiallyRpcException("missing transport");
     }
     return $transport->send($request);
 }
 /**
  * Get proxy class dir and create it
  *
  * @param string $namespace
  * @return string
  * @throws InitiallyRpcException
  */
 protected function getProxyClassDirAndCreate($namespace)
 {
     $path = str_replace("\\", DIRECTORY_SEPARATOR, $namespace);
     $dir = $this->config->getProxyRootDir() . DIRECTORY_SEPARATOR . $path;
     if (!Util::createDirIfNotExists($dir)) {
         throw new InitiallyRpcException("Proxy builder error: get proxy class dir and create it");
     }
     return $dir;
 }
Exemplo n.º 4
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;
 }