Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
Client.php
001 <?php
002 namespace GuzzleHttp;
003
004 use GuzzleHttp\Event\HasEmitterTrait;
005 use GuzzleHttp\Message\MessageFactory;
006 use GuzzleHttp\Message\MessageFactoryInterface;
007 use GuzzleHttp\Message\RequestInterface;
008 use GuzzleHttp\Message\FutureResponse;
009 use GuzzleHttp\Ring\Core;
010 use GuzzleHttp\Ring\Future\FutureInterface;
011 use GuzzleHttp\Exception\RequestException;
012 use React\Promise\FulfilledPromise;
013 use React\Promise\RejectedPromise;
014
015 /**
016 * HTTP client
017 */
018 class Client implements ClientInterface
019 {
020 use HasEmitterTrait;
021
022 /** @var MessageFactoryInterface Request factory used by the client */
023 private $messageFactory;
024
025 /** @var Url Base URL of the client */
026 private $baseUrl;
027
028 /** @var array Default request options */
029 private $defaults;
030
031 /** @var callable Request state machine */
032 private $fsm;
033
034 /**
035 * Clients accept an array of constructor parameters.
036 *
037 * Here's an example of creating a client using an URI template for the
038 * client's base_url and an array of default request options to apply
039 * to each request:
040 *
041 * $client = new Client([
042 * 'base_url' => [
043 * 'http://www.foo.com/{version}/',
044 * ['version' => '123']
045 * ],
046 * 'defaults' => [
047 * 'timeout' => 10,
048 * 'allow_redirects' => false,
049 * 'proxy' => '192.168.16.1:10'
050 * ]
051 * ]);
052 *
053 * @param array $config Client configuration settings
054 * - base_url: Base URL of the client that is merged into relative URLs.
055 * Can be a string or an array that contains a URI template followed
056 * by an associative array of expansion variables to inject into the
057 * URI template.
058 * - handler: callable RingPHP handler used to transfer requests
059 * - message_factory: Factory used to create request and response object
060 * - defaults: Default request options to apply to each request
061 * - emitter: Event emitter used for request events
062 * - fsm: (internal use only) The request finite state machine. A
063 * function that accepts a transaction and optional final state. The
064 * function is responsible for transitioning a request through its
065 * lifecycle events.
066 */
067 public function __construct(array $config = [])
068 {
069 $this->configureBaseUrl($config);
070 $this->configureDefaults($config);
071
072 if (isset($config['emitter'])) {
073 $this->emitter = $config['emitter'];
074 }
075
076 $this->messageFactory = isset($config['message_factory'])
077 ? $config['message_factory']
078 : new MessageFactory();
079
080 if (isset($config['fsm'])) {
081 $this->fsm = $config['fsm'];
082 } else {
083 if (isset($config['handler'])) {
084 $handler = $config['handler'];
085 } elseif (isset($config['adapter'])) {
086 $handler = $config['adapter'];
087 } else {
088 $handler = Utils::getDefaultHandler();
089 }
090 $this->fsm = new RequestFsm($handler, $this->messageFactory);
091 }
092 }
093
094 public function getDefaultOption($keyOrPath = null)
095 {
096 return $keyOrPath === null
097 ? $this->defaults
098 : Utils::getPath($this->defaults, $keyOrPath);
099 }
100
101 public function setDefaultOption($keyOrPath, $value)
102 {
103 Utils::setPath($this->defaults, $keyOrPath, $value);
104 }
105
106 public function getBaseUrl()
107 {
108 return (string) $this->baseUrl;
109 }
110
111 public function createRequest($method, $url = null, array $options = [])
112 {
113 $options = $this->mergeDefaults($options);
114 // Use a clone of the client's emitter
115 $options['config']['emitter'] = clone $this->getEmitter();
116 $url = $url || (is_string($url) && strlen($url))
117 ? $this->buildUrl($url)
118 : (string) $this->baseUrl;
119
120 return $this->messageFactory->createRequest($method, $url, $options);
121 }
122
123 public function get($url = null, $options = [])
124 {
125 return $this->send($this->createRequest('GET', $url, $options));
126 }
127
128 public function head($url = null, array $options = [])
129 {
130 return $this->send($this->createRequest('HEAD', $url, $options));
131 }
132
133 public function delete($url = null, array $options = [])
134 {
135 return $this->send($this->createRequest('DELETE', $url, $options));
136 }
137
138 public function put($url = null, array $options = [])
139 {
140 return $this->send($this->createRequest('PUT', $url, $options));
141 }
142
143 public function patch($url = null, array $options = [])
144 {
145 return $this->send($this->createRequest('PATCH', $url, $options));
146 }
147
148 public function post($url = null, array $options = [])
149 {
150 return $this->send($this->createRequest('POST', $url, $options));
151 }
152
153 public function options($url = null, array $options = [])
154 {
155 return $this->send($this->createRequest('OPTIONS', $url, $options));
156 }
157
158 public function send(RequestInterface $request)
159 {
160 $isFuture = $request->getConfig()->get('future');
161 $trans = new Transaction($this, $request, $isFuture);
162 $fn = $this->fsm;
163
164 try {
165 $fn($trans);
166 if ($isFuture) {
167 // Turn the normal response into a future if needed.
168 return $trans->response instanceof FutureInterface
169 ? $trans->response
170 : new FutureResponse(new FulfilledPromise($trans->response));
171 }
172 // Resolve deep futures if this is not a future
173 // transaction. This accounts for things like retries
174 // that do not have an immediate side-effect.
175 while ($trans->response instanceof FutureInterface) {
176 $trans->response = $trans->response->wait();
177 }
178 return $trans->response;
179 } catch (\Exception $e) {
180 if ($isFuture) {
181 // Wrap the exception in a promise
182 return new FutureResponse(new RejectedPromise($e));
183 }
184 throw RequestException::wrapException($trans->request, $e);
185 }
186 }
187
188 /**
189 * Get an array of default options to apply to the client
190 *
191 * @return array
192 */
193 protected function getDefaultOptions()
194 {
195 $settings = [
196 'allow_redirects' => true,
197 'exceptions' => true,
198 'decode_content' => true,
199 'verify' => true
200 ];
201
202 // Use the standard Linux HTTP_PROXY and HTTPS_PROXY if set.
203 // We can only trust the HTTP_PROXY environment variable in a CLI
204 // process due to the fact that PHP has no reliable mechanism to
205 // get environment variables that start with "HTTP_".
206 if (php_sapi_name() == 'cli' && getenv('HTTP_PROXY')) {
207 $settings['proxy']['http'] = getenv('HTTP_PROXY');
208 }
209
210 if ($proxy = getenv('HTTPS_PROXY')) {
211 $settings['proxy']['https'] = $proxy;
212 }
213
214 return $settings;
215 }
216
217 /**
218 * Expand a URI template and inherit from the base URL if it's relative
219 *
220 * @param string|array $url URL or an array of the URI template to expand
221 * followed by a hash of template varnames.
222 * @return string
223 * @throws \InvalidArgumentException
224 */
225 private function buildUrl($url)
226 {
227 // URI template (absolute or relative)
228 if (!is_array($url)) {
229 return strpos($url, '://')
230 ? (string) $url
231 : (string) $this->baseUrl->combine($url);
232 }
233
234 if (!isset($url[1])) {
235 throw new \InvalidArgumentException('You must provide a hash of '
236 . 'varname options in the second element of a URL array.');
237 }
238
239 // Absolute URL
240 if (strpos($url[0], '://')) {
241 return Utils::uriTemplate($url[0], $url[1]);
242 }
243
244 // Combine the relative URL with the base URL
245 return (string) $this->baseUrl->combine(
246 Utils::uriTemplate($url[0], $url[1])
247 );
248 }
249
250 private function configureBaseUrl(&$config)
251 {
252 if (!isset($config['base_url'])) {
253 $this->baseUrl = new Url('', '');
254 } elseif (!is_array($config['base_url'])) {
255 $this->baseUrl = Url::fromString($config['base_url']);
256 } elseif (count($config['base_url']) < 2) {
257 throw new \InvalidArgumentException('You must provide a hash of '
258 . 'varname options in the second element of a base_url array.');
259 } else {
260 $this->baseUrl = Url::fromString(
261 Utils::uriTemplate(
262 $config['base_url'][0],
263 $config['base_url'][1]
264 )
265 );
266 $config['base_url'] = (string) $this->baseUrl;
267 }
268 }
269
270 private function configureDefaults($config)
271 {
272 if (!isset($config['defaults'])) {
273 $this->defaults = $this->getDefaultOptions();
274 } else {
275 $this->defaults = array_replace(
276 $this->getDefaultOptions(),
277 $config['defaults']
278 );
279 }
280
281 // Add the default user-agent header
282 if (!isset($this->defaults['headers'])) {
283 $this->defaults['headers'] = [
284 'User-Agent' => Utils::getDefaultUserAgent()
285 ];
286 } elseif (!Core::hasHeader($this->defaults, 'User-Agent')) {
287 // Add the User-Agent header if one was not already set
288 $this->defaults['headers']['User-Agent'] = Utils::getDefaultUserAgent();
289 }
290 }
291
292 /**
293 * Merges default options into the array passed by reference.
294 *
295 * @param array $options Options to modify by reference
296 *
297 * @return array
298 */
299 private function mergeDefaults($options)
300 {
301 $defaults = $this->defaults;
302
303 // Case-insensitively merge in default headers if both defaults and
304 // options have headers specified.
305 if (!empty($defaults['headers']) && !empty($options['headers'])) {
306 // Create a set of lowercased keys that are present.
307 $lkeys = [];
308 foreach (array_keys($options['headers']) as $k) {
309 $lkeys[strtolower($k)] = true;
310 }
311 // Merge in lowercase default keys when not present in above set.
312 foreach ($defaults['headers'] as $key => $value) {
313 if (!isset($lkeys[strtolower($key)])) {
314 $options['headers'][$key] = $value;
315 }
316 }
317 // No longer need to merge in headers.
318 unset($defaults['headers']);
319 }
320
321 $result = array_replace_recursive($defaults, $options);
322 foreach ($options as $k => $v) {
323 if ($v === null) {
324 unset($result[$k]);
325 }
326 }
327
328 return $result;
329 }
330
331 /**
332 * @deprecated Use {@see GuzzleHttp\Pool} instead.
333 * @see GuzzleHttp\Pool
334 */
335 public function sendAll($requests, array $options = [])
336 {
337 Pool::send($this, $requests, $options);
338 }
339
340 /**
341 * @deprecated Use GuzzleHttp\Utils::getDefaultHandler
342 */
343 public static function getDefaultHandler()
344 {
345 return Utils::getDefaultHandler();
346 }
347
348 /**
349 * @deprecated Use GuzzleHttp\Utils::getDefaultUserAgent
350 */
351 public static function getDefaultUserAgent()
352 {
353 return Utils::getDefaultUserAgent();
354 }
355 }
356