Serializer.php
5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?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 GuzzleHttp\Psr7\UriResolver;
use GuzzleHttp\UriTemplate\UriTemplate;
use Psr\Http\Message\RequestInterface;
/**
* Serializes requests for a given command.
*/
class Serializer
{
/** @var RequestLocationInterface[] */
private $locations;
/** @var DescriptionInterface */
private $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;
}
/**
* @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 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
*
* @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() ?: 'GET',
$this->description->getBaseUri()
);
}
return $this->createCommandWithUri($operation, $command);
}
/**
* Create a request for an operation with a uri merged onto a base URI
*
* @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 = new Uri(UriTemplate::expand($operation->getUri(), $variables));
return new Request(
$operation->getHttpMethod() ?: 'GET',
UriResolver::resolve($this->description->getBaseUri(), $uri)
);
}
}