public function testPerformCallback() { $collection = new Request(); $entityA = new EntityRequest(); $entityA->setId(1); $response = new Response(); $response->setId(1); $self = $this; $collection->addRequest($entityA, function ($data) use($self, $response) { $self->assertInstanceOf('Jdolieslager\\JsonRpc\\Entity\\Response', $data); $self->assertEquals(true, $data === $response); }); $collection->performCallback($response); }
public function addRequest(EntityRequest $request, $callable = null) { $id = $request->getId(); if ($id === null && $callable !== null) { throw new Exception\InvalidArgument('Request ID is NULL. This means no response for server. Cannot ' . 'attach callable to this request.', 1); } if ($id !== null && array_key_exists($id, $this->ids)) { throw new Exception\RuntimeException('Cannot add same id `' . $id . '` twice to the stack!'); } // Add the callable to the stack if ($id !== null) { $this->ids[$id] = $callable; } return parent::offsetSet(null, $request); }
/** * Send notification. This is a fire and forget action. No response given * * @param string $method * @param array $params * @return null */ public function sendNotification($method, array $params = array()) { $request = new Entity\Request(); $request->setId(null); $request->setJsonrpc(self::VERSION_2); $request->setMethod($method); foreach ($params as $index => $value) { $request->addParam($index, $value); } return $this->sendSingleRequest($request); }
/** * General response creator for exceptions * * @param Entity\Request $request * @param \Exception $e * @return Entity\Response */ public function createResponseFromException(Entity\Request $request, \Exception $e) { if ($e instanceof Exception\ExceptionInterface === false) { $e = new Exception\InvalidRequest('Internal error', static::INTERNAL_ERROR, $e); } // Create required objects $response = new Entity\Response(); $error = new Entity\Error(); // Attach error the response object $response->setError($error); $response->setJsonrpc($request->getJsonrpc()); // Set the error data $error->setCode($e->getCode()); $error->setMessage($e->getMessage()); // The error code $code = $e->getCode(); // Set the request ID based on error code if ($code !== static::PARSE_ERROR && $code !== static::INVALID_REQUEST) { $response->setId($request->getId()); } $data = array(); if ($response->getId() !== null && $code !== static::METHOD_NOT_FOUND) { $methodName = strtolower($request->getMethod()); $namespace = 'global'; if (strpos($methodName, '.') !== false) { list($namespace, $methodName) = explode('.', $methodName, 2); } if ($this->handlers->offsetExists($namespace)) { $methods = $this->getHandlerReflectionMaker()->reflect($this->handlers->offsetGet($namespace)); if ($methods->offsetExists($methodName)) { $method = $methods->offsetGet($methodName); $data['parameters'] = $method->getParameters()->getArrayCopy(); } } } // In debug mode we print more data if ($this->debugMode === true) { $data['exceptions'] = array(); $data['backtrace'] = $e->getTrace(); while (null !== ($e = $e->getPrevious())) { $data['exceptions'][] = array($e->getCode() => $e->getMessage()); } } // Set all the data $error->setData($data); return $response; }
/** * Get single request * * @return \Jdolieslager\JsonRpc\Entity\Request */ protected function dummyRequest($id = 1) { $request = new Request(); $request->setJsonrpc(Request::VERSION_2); $request->setMethod('dummy'); $request->setId($id); $request->addParam('positional'); $request->addParam('world', 'hello'); return $request; }
public function getArrayCopy() { $result = parent::getArrayCopy(); $result['resource'] = fopen('php://input', 'r'); return $result; }