123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace GuzzleHttp\Command\Guzzle;
- use GuzzleHttp\Command\CommandInterface;
- use GuzzleHttp\Command\Guzzle\RequestLocation\BodyLocation;
- use GuzzleHttp\Command\Guzzle\RequestLocation\FormParamLocation;
- use GuzzleHttp\Command\Guzzle\RequestLocation\HeaderLocation;
- use GuzzleHttp\Command\Guzzle\RequestLocation\JsonLocation;
- use GuzzleHttp\Command\Guzzle\RequestLocation\MultiPartLocation;
- use GuzzleHttp\Command\Guzzle\RequestLocation\QueryLocation;
- use GuzzleHttp\Command\Guzzle\RequestLocation\RequestLocationInterface;
- use GuzzleHttp\Command\Guzzle\RequestLocation\XmlLocation;
- use GuzzleHttp\Psr7\Request;
- use GuzzleHttp\Psr7\Uri;
- use Psr\Http\Message\RequestInterface;
- /**
- * Serializes requests for a given command.
- */
- class Serializer
- {
- /** @var RequestLocationInterface[] */
- private $locations;
- /** @var DescriptionInterface */
- private $description;
- /**
- * @param DescriptionInterface $description
- * @param RequestLocationInterface[] $requestLocations Extra request locations
- */
- public function __construct(
- DescriptionInterface $description,
- array $requestLocations = []
- ) {
- static $defaultRequestLocations;
- if (!$defaultRequestLocations) {
- $defaultRequestLocations = [
- 'body' => new BodyLocation(),
- 'query' => new QueryLocation(),
- 'header' => new HeaderLocation(),
- 'json' => new JsonLocation(),
- 'xml' => new XmlLocation(),
- 'formParam' => new FormParamLocation(),
- 'multipart' => new MultiPartLocation(),
- ];
- }
- $this->locations = $requestLocations + $defaultRequestLocations;
- $this->description = $description;
- }
- /**
- * @param CommandInterface $command
- * @return RequestInterface
- */
- public function __invoke(CommandInterface $command)
- {
- $request = $this->createRequest($command);
- return $this->prepareRequest($command, $request);
- }
- /**
- * Prepares a request for sending using location visitors
- *
- * @param CommandInterface $command
- * @param RequestInterface $request Request being created
- * @return RequestInterface
- * @throws \RuntimeException If a location cannot be handled
- */
- protected function prepareRequest(
- CommandInterface $command,
- RequestInterface $request
- ) {
- $visitedLocations = [];
- $operation = $this->description->getOperation($command->getName());
- // Visit each actual parameter
- foreach ($operation->getParams() as $name => $param) {
- /* @var Parameter $param */
- $location = $param->getLocation();
- // Skip parameters that have not been set or are URI location
- if ($location == 'uri' || !$command->hasParam($name)) {
- continue;
- }
- if (!isset($this->locations[$location])) {
- throw new \RuntimeException("No location registered for $name");
- }
- $visitedLocations[$location] = true;
- $request = $this->locations[$location]->visit($command, $request, $param);
- }
- // Ensure that the after() method is invoked for additionalParameters
- /** @var Parameter $additional */
- if ($additional = $operation->getAdditionalParameters()) {
- $visitedLocations[$additional->getLocation()] = true;
- }
- // Call the after() method for each visited location
- foreach (array_keys($visitedLocations) as $location) {
- $request = $this->locations[$location]->after($command, $request, $operation);
- }
- return $request;
- }
- /**
- * Create a request for the command and operation
- *
- * @param CommandInterface $command
- *
- * @return RequestInterface
- * @throws \RuntimeException
- */
- protected function createRequest(CommandInterface $command)
- {
- $operation = $this->description->getOperation($command->getName());
- // If command does not specify a template, assume the client's base URL.
- if (null === $operation->getUri()) {
- return new Request(
- $operation->getHttpMethod(),
- $this->description->getBaseUri()
- );
- }
- return $this->createCommandWithUri($operation, $command);
- }
- /**
- * Create a request for an operation with a uri merged onto a base URI
- *
- * @param \GuzzleHttp\Command\Guzzle\Operation $operation
- * @param \GuzzleHttp\Command\CommandInterface $command
- *
- * @return \GuzzleHttp\Psr7\Request
- */
- private function createCommandWithUri(
- Operation $operation,
- CommandInterface $command
- ) {
- // Get the path values and use the client config settings
- $variables = [];
- foreach ($operation->getParams() as $name => $arg) {
- /* @var Parameter $arg */
- if ($arg->getLocation() == 'uri') {
- if (isset($command[$name])) {
- $variables[$name] = $arg->filter($command[$name]);
- if (!is_array($variables[$name])) {
- $variables[$name] = (string) $variables[$name];
- }
- }
- }
- }
- // Expand the URI template.
- $uri = \GuzzleHttp\uri_template($operation->getUri(), $variables);
- return new Request(
- $operation->getHttpMethod(),
- Uri::resolve($this->description->getBaseUri(), $uri)
- );
- }
- }
|