1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Abstract Request |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace IBM\Watson\Common\Message; |
7
|
|
|
|
8
|
|
|
use GuzzleHttp\ClientInterface; |
9
|
|
|
use IBM\Watson\Common\Exception\RuntimeException; |
10
|
|
|
use IBM\Watson\Common\Helper; |
11
|
|
|
use Omnipay\Common\Message\ResponseInterface; |
|
|
|
|
12
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Abstract Request |
17
|
|
|
* |
18
|
|
|
* This abstract class implements RequestInterface and defines a basic |
19
|
|
|
* set of functions that all Watson Request are intended to include. |
20
|
|
|
* |
21
|
|
|
* @see RequestInterface |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractRequest implements RequestInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The request parameters |
27
|
|
|
* |
28
|
|
|
* @var \Symfony\Component\HttpFoundation\ParameterBag |
29
|
|
|
*/ |
30
|
|
|
protected $parameters; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The request client |
34
|
|
|
* |
35
|
|
|
* @var \Guzzle\Http\ClientInterface |
36
|
|
|
*/ |
37
|
|
|
protected $httpClient; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The HTTP request object |
41
|
|
|
* |
42
|
|
|
* @var \Symfony\Component\HttpFoundation\Request |
43
|
|
|
*/ |
44
|
|
|
protected $httpRequest; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* An associated ResponseInterface |
48
|
|
|
* |
49
|
|
|
* @var ResponseInterface |
50
|
|
|
*/ |
51
|
|
|
protected $response; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create a new Request |
55
|
|
|
* |
56
|
|
|
* @param ClientInterface $httpClient A Guzzle client to make API calls with |
57
|
|
|
* @param Request $httpRequest A Symfony HTTP requet object |
58
|
|
|
*/ |
59
|
18 |
|
public function __construct(ClientInterface $httpClient, Request $httpRequest) |
|
|
|
|
60
|
|
|
{ |
61
|
18 |
|
$this->httpClient = $httpClient; |
|
|
|
|
62
|
18 |
|
$this->httpRequest = $httpRequest; |
63
|
18 |
|
$this->initialize(); |
64
|
18 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Initialize the request with parameters |
68
|
|
|
* |
69
|
|
|
* @param array $parameters Associative array of parameters |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
* @throws RuntimeException |
73
|
|
|
*/ |
74
|
21 |
|
public function initialize(array $parameters = []) |
75
|
|
|
{ |
76
|
21 |
|
if (null !== $this->response) { |
77
|
3 |
|
throw new RuntimeException('Request cannot be modified after it has been sent!'); |
78
|
|
|
} |
79
|
|
|
|
80
|
21 |
|
$this->parameters = new ParameterBag; |
81
|
|
|
|
82
|
21 |
|
Helper::initialize($this, $parameters); |
83
|
|
|
|
84
|
21 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get all parameters |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
6 |
|
public function getParameters() |
93
|
|
|
{ |
94
|
6 |
|
return $this->parameters->all(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get a parameter |
99
|
|
|
* |
100
|
|
|
* @param string $key The parameter key |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
3 |
|
public function getParameter($key) |
104
|
|
|
{ |
105
|
3 |
|
return $this->parameters->get($key); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the API username |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
3 |
|
public function getUsername() |
114
|
|
|
{ |
115
|
3 |
|
return $this->getParameter('username'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set the API username |
120
|
|
|
* |
121
|
|
|
* @param string $value |
122
|
|
|
* |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
9 |
|
public function setUsername($value) |
126
|
|
|
{ |
127
|
9 |
|
return $this->setParameter('username', $value); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the API password |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
3 |
|
public function getPassword() |
136
|
|
|
{ |
137
|
3 |
|
return $this->getParameter('password'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set the API password |
142
|
|
|
* |
143
|
|
|
* @param string $value |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
6 |
|
public function setPassword($value) |
148
|
|
|
{ |
149
|
6 |
|
return $this->setParameter('password', $value); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the associated Response |
154
|
|
|
* |
155
|
|
|
* @return ResponseInterface |
156
|
|
|
* @throws RuntimeException |
157
|
|
|
*/ |
158
|
6 |
|
public function getResponse() |
159
|
|
|
{ |
160
|
6 |
|
if (null === $this->response) { |
161
|
3 |
|
throw new RuntimeException('You must call send() before accessing the Response!'); |
162
|
|
|
} |
163
|
|
|
|
164
|
3 |
|
return $this->response; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Send the request |
169
|
|
|
* |
170
|
|
|
* @return ResponseInterface |
171
|
|
|
*/ |
172
|
9 |
|
public function send() |
173
|
|
|
{ |
174
|
9 |
|
$data = $this->getData(); |
175
|
|
|
|
176
|
9 |
|
return $this->sendData($data); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Set a parameter on the request |
181
|
|
|
* |
182
|
|
|
* @param string $key |
183
|
|
|
* @param mixed $value |
184
|
|
|
* @return $this |
185
|
|
|
* @throws RuntimeException |
186
|
|
|
*/ |
187
|
9 |
|
protected function setParameter($key, $value) |
188
|
|
|
{ |
189
|
9 |
|
if (null !== $this->response) { |
190
|
3 |
|
throw new RuntimeException('Request cannot be modified after it has been sent!'); |
191
|
|
|
} |
192
|
|
|
|
193
|
6 |
|
$this->parameters->set($key, $value); |
194
|
|
|
|
195
|
6 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: