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