/** * @return ServerModule */ public function handleRequest() { $useZend = false; // zend is slow! $result = $this; // zend is slow! if ($useZend) { $this->_handleZend(); return $result; } $rpc = $this->getRpc(); $rpcRequest = $rpc->getRequest(); $rpcResponse = $rpc->getResponse(); try { $class = $rpc->getRouterServiceQualifiedName(); $method = $rpc->getRouterServiceMethodName(); $params = $rpc->getRouterServiceMethodParams(); if (!is_array($params)) { $params = array(); } $classExists = RpcUtil::classExists($class, true, false); if (!$classExists) { $e = $this->newModuleException(); $e->setMessage($e::ERROR_SERVERMODULE_SERVICECLASS_NOT_EXIST); $e->setMethodInfo($this, __METHOD__, __LINE__); $e->setDebugData(array('className' => $class, 'methodName' => $method)); throw $e; } $reflectionClass = new \ReflectionClass($class); if (!$reflectionClass->isInstantiable() || $reflectionClass->isAbstract()) { $e = $this->newModuleException(); $e->setMessage($e::ERROR_SERVERMODULE_SERVICECLASS_NOT_INVOCABLE); $e->setMethodInfo($this, __METHOD__, __LINE__); $e->setDebugData(array('className' => $class, 'methodName' => $method)); throw $e; } if (!$reflectionClass->hasMethod((string) $method)) { $e = $this->newModuleException(); $e->setMessage($e::ERROR_SERVERMODULE_SERVICEMETHOD_NOT_EXISTS); $e->setMethodInfo($this, __METHOD__, __LINE__); $e->setDebugData(array('className' => $class, 'methodName' => $method)); throw $e; } $reflectionMethod = $reflectionClass->getMethod($method); if (!$reflectionMethod->isPublic() || $reflectionMethod->isAbstract() || $reflectionMethod->isStatic()) { $e = $this->newModuleException(); $e->setMessage($e::ERROR_SERVERMODULE_SERVICEMETHOD_NOT_INVOCABLE); $e->setMethodInfo($this, __METHOD__, __LINE__); $e->setDebugData(array('className' => $class, 'methodName' => $method)); throw $e; } $className = $reflectionClass->getName(); $serviceInstance = $this->_newServiceInstance($className); $this->_invokeServiceMethod($reflectionMethod, $serviceInstance, $params); } catch (\Exception $e) { $rpcResponse->setException($e); } return $result; }
/** * @return DebugModule */ public function handleResponseReadyData() { $result = $this; if (!$this->getIsEnabled()) { return $result; } $rpc = $this->getRpc(); $response = $rpc->getResponse(); $readyData = $response->getReadyData(); // readyData.debug if (!$this->getIsDebugEnabled()) { $response->unsetReadyDataKey('debug'); } else { $readyDataDebug = $response->getReadyDataKey('debug'); $readyDataDebug = RpcUtil::arrayEnsure($readyDataDebug, array('demoDebugModuleMessage' => 'foo ' . __METHOD__, 'isDebugEnabled' => true, 'rpc' => array('request' => $rpc->getRequest()->getData(), 'result' => $rpc->getResponse()->getResult(), 'hasException' => $rpc->getResponse()->hasException()))); if ($response->hasException()) { $readyDataDebug['errorInfo'] = RpcUtil::exceptionAsArray($response->getException(), true); } $response->setReadyDataKey('debug', $readyDataDebug); } return $result; }
/** * @return DispatcherZmq * @throws DispatcherZmqException */ protected function _requireZmq() { $result = $this; if (!RpcUtil::classExists('\\ZMQSocket', true, false)) { $e = $this->newException(); $e->setMessage($e::ERROR_ZMQ_EXTENSION_NOT_FOUND); $e->setMethodInfo($this, __METHOD__, __LINE__); throw $e; } return $result; }
/** * @param GatewayModuleException $exception * @return GatewayModule */ protected function _onErrorRawResponseEmitFailed(GatewayModuleException $exception) { $result = $this; // no valid request raw data $isDebugEnabled = $this->getIsDebugEnabled(); $isAutoEmitResponseEnabled = $this->getIsAutoEmitResponseEnabled(); if ($isAutoEmitResponseEnabled) { $responseData = array('result' => null, 'error' => RpcUtil::exceptionAsArray($exception, $isDebugEnabled)); $responseText = RpcUtil::jsonEncode($responseData, false); if (!is_string($responseText)) { $responseData = array('result' => null, 'error' => RpcUtil::exceptionAsArray($exception, false)); $responseText = RpcUtil::jsonEncode($responseData, false); } $headersList = array('HTTP/1.1 500 Internal Server Error'); $this->_emitResponseHeaders($headersList); echo (string) $responseText; return $result; } return $result; }
/** * @param \Exception $exception * @param bool $isDebugEnabled * @return array */ protected function _exportExceptionAsArray(\Exception $exception, $isDebugEnabled) { $error = RpcUtil::exceptionAsArray($exception, $isDebugEnabled); return $error; }
/** * @param $key * @return array */ protected function _getModuleConfigItemByKey($key) { $result = array('config' => null, 'class' => null); $item = $this->getConfigValue($key); $result = RpcUtil::arrayEnsure($item, $result); return $result; }
/** * * @return SecurityModule * @throws \Exception */ private function validateRequestDataSignature() { $result = $this; if (!$this->getIsEnabled()) { return $result; } $rpc = $this->getRpc(); $request = $rpc->getRequest(); $requestData = $request->getData(); if (!is_array($requestData)) { $requestData = array(); } $signatureGiven = $request->getDataKey('signature'); $signSecret = $this->getSignatureRequestSecret(); $signAlgorithm = $this->getSignatureAlgorithm(); $signData = array(); $signKeys = $this->getSignatureSignRequestKeys(); foreach ($signKeys as $key) { $value = null; if (array_key_exists($requestData, $key)) { $value = $requestData[$key]; } $signData[$key] = $value; } $signatureIsValid = RpcUtil::validateSignedRequest($signatureGiven, $signData, $signKeys, $signSecret, $signAlgorithm); if (!$signatureIsValid) { $e = $this->newModuleException(); $e->setMessage($e::ERROR_RPC_SIGNATURE_INVALID); $e->setMethodInfo($this, __METHOD__, __LINE__); throw $e; } return $result; }