1<?php
2namespace GuzzleHttp\Ring\Client;
3
4/**
5 * Provides basic middleware wrappers.
6 *
7 * If a middleware is more complex than a few lines of code, then it should
8 * be implemented in a class rather than a static method.
9 */
10class Middleware
11{
12    /**
13     * Sends future requests to a future compatible handler while sending all
14     * other requests to a default handler.
15     *
16     * When the "future" option is not provided on a request, any future responses
17     * are automatically converted to synchronous responses and block.
18     *
19     * @param callable $default Handler used for non-streaming responses
20     * @param callable $future  Handler used for future responses
21     *
22     * @return callable Returns the composed handler.
23     */
24    public static function wrapFuture(
25        callable $default,
26        callable $future
27    ) {
28        return function (array $request) use ($default, $future) {
29            return empty($request['client']['future'])
30                ? $default($request)
31                : $future($request);
32        };
33    }
34
35    /**
36     * Sends streaming requests to a streaming compatible handler while sendin
37     * all other requests to a default handler.
38     *
39     * This, for example, could be useful for taking advantage of the
40     * performance benefits of curl while still supporting true streaming
41     * through the StreamHandler.
42     *
43     * @param callable $default   Handler used for non-streaming responses
44     * @param callable $streaming Handler used for streaming responses
45     *
46     * @return callable Returns the composed handler.
47     */
48    public static function wrapStreaming(
49        callable $default,
50        callable $streaming
51    ) {
52        return function (array $request) use ($default, $streaming) {
53            return empty($request['client']['stream'])
54                ? $default($request)
55                : $streaming($request);
56        };
57    }
58}
59