1 | <?php |
||
20 | final class GuzzleHttpClient implements HttpClientInterface |
||
21 | { |
||
22 | 26 | public function __construct(ClientInterface $client = null) |
|
23 | { |
||
24 | 26 | $this->client = $client ?: new Client([ |
|
|
|||
25 | 26 | 'handler' => new CurlMultiHandler(), |
|
26 | ]); |
||
27 | 26 | } |
|
28 | |||
29 | public static function withFilesystemCache($path) |
||
30 | { |
||
31 | return self::withDoctrineCache( |
||
32 | new FilesystemCache($path) |
||
33 | ); |
||
34 | } |
||
35 | |||
36 | public static function withApcCache() |
||
37 | { |
||
38 | return self::withDoctrineCache( |
||
39 | new ApcCache() |
||
40 | ); |
||
41 | } |
||
42 | |||
43 | public static function withDoctrineCache(Cache $doctrineCache) |
||
44 | { |
||
45 | $stack = new HandlerStack(new CurlMultiHandler()); |
||
46 | |||
47 | $stack->push( |
||
48 | new CacheMiddleware( |
||
49 | new PublicCacheStrategy( |
||
50 | new DoctrineCacheStorage( |
||
51 | $doctrineCache |
||
52 | ) |
||
53 | ) |
||
54 | ), |
||
55 | 'cache' |
||
56 | ); |
||
57 | |||
58 | $client = new Client([ |
||
59 | 'handler' => $stack |
||
60 | ]); |
||
61 | |||
62 | return new self($client); |
||
63 | } |
||
64 | |||
65 | 7 | public function sendRequestBatch(array $requests) |
|
66 | { |
||
67 | 7 | $responses = Pool::batch( |
|
68 | 7 | $this->client, |
|
69 | $requests |
||
70 | ); |
||
71 | |||
72 | 7 | foreach ($responses as $response) { |
|
73 | 7 | if ($response instanceof PhpException) { |
|
74 | 7 | throw $response; |
|
75 | } |
||
76 | } |
||
77 | |||
78 | 7 | return $responses; |
|
79 | } |
||
80 | |||
81 | 26 | public function send(RequestInterface $request) |
|
91 | } |
||
92 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: