1 | <?php |
||
29 | abstract class AbstractProxyClient implements ProxyClientInterface |
||
30 | { |
||
31 | /** |
||
32 | * HTTP client adapter. |
||
33 | * |
||
34 | * @var HttpAdapter |
||
35 | */ |
||
36 | protected $httpAdapter; |
||
37 | |||
38 | /** |
||
39 | * @var MessageFactory |
||
40 | */ |
||
41 | protected $messageFactory; |
||
42 | |||
43 | /** |
||
44 | * The options configured in the constructor argument or default values. |
||
45 | * |
||
46 | * @var array The resolved options. |
||
47 | */ |
||
48 | protected $options; |
||
49 | |||
50 | /** |
||
51 | * Constructor |
||
52 | * |
||
53 | * Supported options: |
||
54 | * |
||
55 | * - base_uri Default application hostname, optionally including base URL, |
||
56 | * for purge and refresh requests (optional). This is required if you |
||
57 | * purge and refresh paths instead of absolute URLs. |
||
58 | * |
||
59 | * @param array $servers Caching proxy server hostnames or IP |
||
60 | * addresses, including port if not port 80. |
||
61 | * E.g. ['127.0.0.1:6081'] |
||
62 | * @param array $options List of options for the client. |
||
63 | * @param HttpAsyncClient|null $httpClient Client capable of sending HTTP requests. If no |
||
64 | * client is supplied, a default one is created. |
||
65 | * @param MessageFactory|null $messageFactory Factory for PSR-7 messages. If none supplied, |
||
66 | * a default one is created. |
||
67 | * @param UriFactory|null $uriFactory Factory for PSR-7 URIs. If not specified, a |
||
68 | * default one is created. |
||
69 | */ |
||
70 | 45 | public function __construct( |
|
71 | array $servers, |
||
72 | array $options = [], |
||
73 | HttpAsyncClient $httpClient = null, |
||
74 | MessageFactory $messageFactory = null, |
||
75 | UriFactory $uriFactory = null |
||
76 | ) { |
||
77 | 45 | if ((!$httpClient || !$messageFactory || !$uriFactory) && !class_exists('Http\Discovery\HttpAsyncClientDiscovery')) { |
|
78 | throw new \LogicException('Either specify the client and the message and uri factories or include php-http/discovery in your project'); |
||
79 | } |
||
80 | |||
81 | 45 | if (!$httpClient) { |
|
82 | 26 | $httpClient = HttpAsyncClientDiscovery::find(); |
|
83 | 26 | } |
|
84 | 45 | if (!$uriFactory) { |
|
85 | 45 | $uriFactory = UriFactoryDiscovery::find(); |
|
86 | 45 | } |
|
87 | |||
88 | 45 | $this->options = $this->getDefaultOptions()->resolve($options); |
|
89 | 45 | $this->httpAdapter = new HttpAdapter($servers, $this->options['base_uri'], $httpClient, $uriFactory); |
|
90 | 42 | $this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find(); |
|
91 | 42 | } |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | 38 | public function flush() |
|
100 | |||
101 | /** |
||
102 | * Get options resolver with default settings. |
||
103 | * |
||
104 | * @return OptionsResolver |
||
105 | */ |
||
106 | 45 | protected function getDefaultOptions() |
|
113 | |||
114 | /** |
||
115 | * Create a request and queue it with the HttpAdapter. |
||
116 | * |
||
117 | * @param string $method |
||
118 | * @param string|UriInterface $url |
||
119 | * @param array $headers |
||
120 | */ |
||
121 | 13 | protected function queueRequest($method, $url, array $headers) |
|
127 | |||
128 | /** |
||
129 | * Make sure that the tags are valid. |
||
130 | * |
||
131 | * Reusable function for proxy clients. |
||
132 | * Escapes `,` and `\n` (newline) characters. |
||
133 | * |
||
134 | * @param array $tags The tags to escape. |
||
135 | * |
||
136 | * @return array Sane tags. |
||
137 | */ |
||
138 | protected function escapeTags(array $tags) |
||
146 | } |
||
147 |