public function handle($request, $serviceId) { $encoder = new JsonEncoder(); try { $request = $encoder->decode($request, JsonEncoder::FORMAT); } catch (UnexpectedValueException $exception) { return new ErrorResponse(ErrorResponse::ERROR_CODE_PARSE_ERROR, 'Invalid JSON'); } $request = $this->resolveOptions($request); if (!$this->isAllowed($serviceId, $request['method'])) { return new ErrorResponse(ErrorResponse::ERROR_CODE_METHOD_NOT_FOUND, sprintf('%s does not exist', $request['method'])); } $service = $this->container->get($serviceId); $result = call_user_func_array(array($service, $request['method']), $request['params']); return new SuccessResponse($request['id'], $result); }
/** * @param callable[] $decoders */ public static function decode(Request $request, array $decoders = null) { if (null === $decoders) { $decoders = ['json' => function ($content) { $encoder = new JsonEncoder(); return $encoder->decode($content, 'json'); }, 'xml' => function ($content) { $encoder = new XmlEncoder(); return $encoder->decode($content, 'xml'); }]; } if (!self::isDecodeable($request)) { return; } $contentType = $request->headers->get('Content-Type'); $format = null === $contentType ? $request->getRequestFormat() : $request->getFormat($contentType); if (!$format || !isset($decoders[$format])) { return; } if (!is_callable($decoders[$format])) { return; } $content = $request->getContent(); if (!$content) { return; } try { $data = call_user_func($decoders[$format], $content); } catch (\Exception $e) { throw new BadRequestHttpException('Invalid ' . $format . ' message received', $e); } if (!is_array($data)) { throw new BadRequestHttpException('Invalid ' . $format . ' message received'); } $request->request->replace($data); }
/** * * Read configuration from file * * @param $file * @return array * @throws InputOperationException */ public function readFromFile($file) { $fs = new Filesystem(); if (!$fs->exists($file)) { throw new InputOperationException("File '{$file}' not found."); } $serialized = $this->getContents($file); if ($this->getFormat() == 'yaml') { $yaml = new Yaml(); $data = $yaml->parse($serialized); } elseif ($this->getFormat() == 'json') { $encoder = new JsonEncoder(); $data = $encoder->decode($serialized, $encoder::FORMAT); } else { throw new InputOperationException("Invalid configuration format {$this->format}."); } $this->setConfig($data); return $this->getConfig(); }