/** * Load server config * <format> * { * "transport": "", * "services": [ * { * "interface": "", * "reference": "" * }, * ... * ] * } * </format> * * @return Server * @throws InitiallyRpcException */ public static function server() { $configFile = ServerApp::getInstance()->getConfigFile(); $content = file_get_contents($configFile); $config = json_decode($content, true); if (!is_array($config)) { throw new InitiallyRpcException("Server config error: format error"); } else { if (!isset($config["transport"])) { throw new InitiallyRpcException("Server config error: undefined transport"); } } $serverConfig = new Server(); $serverConfig->setTransport($config["transport"]); if (isset($config["services"]) && is_array($config["services"]) && !empty($config["services"])) { foreach ($config["services"] as $key => $value) { if (!is_array($value)) { throw new InitiallyRpcException("Server config error: service format error"); } else { if (!isset($value["interface"])) { throw new InitiallyRpcException("Server config error: undefined interface"); } else { if (!isset($value["reference"])) { throw new InitiallyRpcException("Server config error: undefined reference"); } } } $service = new Service(); $service->setInterface($value["interface"]); $service->setReference($value["reference"]); $serverConfig->addService($service); } } return $serverConfig; }
/** * Handle */ public function handle() { $requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]); if ($requestMethod === "POST") { $request = $this->receive(); $target = ServerApp::getInstance()->getTarget($request->getInterface()); $invoker = ServerApp::getInstance()->getProtocol()->export($target); $invocation = new Invocation($request->getMethodName(), $request->getArguments()); $response = $invoker->invoke($invocation); $this->reply($response); } else { echo "GET"; } }