/** * @param string $relation * @param array $parameters * @throws ServiceUnavailableHttpException * @return Navigator|mixed */ public function command($relation, array $parameters = [], $files = []) { $templatedUrl = $this->getRelation($relation); // expand template url + remove templated params from parameters $url = $this->renderUri($templatedUrl, $parameters); $templatedParameters = $this->uriTemplater->extract($templatedUrl, $url); $parameters = array_diff_key($parameters, $templatedParameters); try { $headers = []; if (!$files) { $headers = ['Content-Type' => 'application/json']; $parameters = json_encode($parameters); } $request = $this->guzzleClient->post($url, $headers, $parameters); $request->addPostFiles($files); $command = $this->guzzleClient->send($request); } catch (ClientErrorResponseException $e) { throw $e; } switch ($command->getContentType()) { case 'application/hal+json': $response = new Navigator($command->json()); break; case 'application/json': $response = $command->json(); break; default: $response = $command->getBody(true); break; } return $response; }
/** * Find the operationId associated to a given path and method * * @todo Implement a less expensive finder * @param string $method An HTTP method * @param string $path A path (ex: /foo/1) * * @return string The operationId */ public function findOperationId($method, $path) { $uriTemplateManager = new UriTemplate(); foreach ($this->requestDefinitions as $requestDefinition) { if ($requestDefinition->getMethod() !== $method) { continue; } $params = $uriTemplateManager->extract($requestDefinition->getPathTemplate(), $path, true); if ($params !== null) { return $requestDefinition->getOperationId(); } } throw new \InvalidArgumentException('Unable to resolve the operationId for path ' . $path); }
public function route($method, $path) { $parser = new UriTemplate(); $results = []; $routes = $this->table[strtoupper($method)]; foreach ($routes as $route) { $args = $parser->extract($route[0], $path, true); if (isset($args)) { // reflect this function as late as possible, since *most* routes shouldn't be called $ref = new \ReflectionFunction($route[1]); $results[] = \Closure::bind(function () use($ref, $args) { return $ref->invokeArgs($args); }, null); } } // always return a callable function inside an array return empty($results) ? [$this->emptyRoute] : $results; }