Chris@0
|
1 <?php
|
Chris@0
|
2 namespace GuzzleHttp\Handler;
|
Chris@0
|
3
|
Chris@0
|
4 use GuzzleHttp\RequestOptions;
|
Chris@0
|
5 use Psr\Http\Message\RequestInterface;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Provides basic proxies for handlers.
|
Chris@0
|
9 */
|
Chris@0
|
10 class Proxy
|
Chris@0
|
11 {
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Sends synchronous requests to a specific handler while sending all other
|
Chris@0
|
14 * requests to another handler.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @param callable $default Handler used for normal responses
|
Chris@0
|
17 * @param callable $sync Handler used for synchronous responses.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @return callable Returns the composed handler.
|
Chris@0
|
20 */
|
Chris@0
|
21 public static function wrapSync(
|
Chris@0
|
22 callable $default,
|
Chris@0
|
23 callable $sync
|
Chris@0
|
24 ) {
|
Chris@0
|
25 return function (RequestInterface $request, array $options) use ($default, $sync) {
|
Chris@0
|
26 return empty($options[RequestOptions::SYNCHRONOUS])
|
Chris@0
|
27 ? $default($request, $options)
|
Chris@0
|
28 : $sync($request, $options);
|
Chris@0
|
29 };
|
Chris@0
|
30 }
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Sends streaming requests to a streaming compatible handler while sending
|
Chris@0
|
34 * all other requests to a default handler.
|
Chris@0
|
35 *
|
Chris@0
|
36 * This, for example, could be useful for taking advantage of the
|
Chris@0
|
37 * performance benefits of curl while still supporting true streaming
|
Chris@0
|
38 * through the StreamHandler.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @param callable $default Handler used for non-streaming responses
|
Chris@0
|
41 * @param callable $streaming Handler used for streaming responses
|
Chris@0
|
42 *
|
Chris@0
|
43 * @return callable Returns the composed handler.
|
Chris@0
|
44 */
|
Chris@0
|
45 public static function wrapStreaming(
|
Chris@0
|
46 callable $default,
|
Chris@0
|
47 callable $streaming
|
Chris@0
|
48 ) {
|
Chris@0
|
49 return function (RequestInterface $request, array $options) use ($default, $streaming) {
|
Chris@0
|
50 return empty($options['stream'])
|
Chris@0
|
51 ? $default($request, $options)
|
Chris@0
|
52 : $streaming($request, $options);
|
Chris@0
|
53 };
|
Chris@0
|
54 }
|
Chris@0
|
55 }
|