@@ -27,11 +27,12 @@ class CachedHttpClient extends HttpClient
27
27
private $ lastCachedResponse ;
28
28
29
29
/**
30
- * $path + query parameter(s) if they exist.
30
+ * Identifier used for the cache file(s).
31
+ * $path + encoded query parameter(s) if they exist.
31
32
*
32
33
* @var string
33
34
*/
34
- private $ path ;
35
+ private $ id ;
35
36
36
37
/**
37
38
* @return CacheInterface
@@ -58,18 +59,16 @@ public function setCache(CacheInterface $cache)
58
59
*/
59
60
public function request ($ path , $ body = null , $ httpMethod = 'GET ' , array $ headers = array (), array $ options = array ())
60
61
{
61
- $ this ->formatPath ($ path , $ options );
62
-
63
62
$ response = parent ::request ($ path , $ body , $ httpMethod , $ headers , $ options );
64
63
65
64
if (304 == $ response ->getStatusCode ()) {
66
- $ cacheResponse = $ this ->getCache ()->get ($ this ->path );
65
+ $ cacheResponse = $ this ->getCache ()->get ($ this ->id );
67
66
$ this ->lastCachedResponse = $ cacheResponse ;
68
67
69
68
return $ cacheResponse ;
70
69
}
71
70
72
- $ this ->getCache ()->set ($ this ->path , $ response );
71
+ $ this ->getCache ()->set ($ this ->id , $ response );
73
72
74
73
return $ response ;
75
74
}
@@ -82,8 +81,14 @@ public function request($path, $body = null, $httpMethod = 'GET', array $headers
82
81
protected function createRequest ($ httpMethod , $ path , $ body = null , array $ headers = array (), array $ options = array ())
83
82
{
84
83
$ request = parent ::createRequest ($ httpMethod , $ path , $ body , $ headers , $ options );
84
+
85
+ $ this ->id = $ path ;
86
+
87
+ if (array_key_exists ('query ' , $ options ) && !empty ($ options ['query ' ])) {
88
+ $ this ->id .= '? ' . $ request ->getQuery ();
89
+ }
85
90
86
- if ($ modifiedAt = $ this ->getCache ()->getModifiedSince ($ this ->path )) {
91
+ if ($ modifiedAt = $ this ->getCache ()->getModifiedSince ($ this ->id )) {
87
92
$ modifiedAt = new \DateTime ('@ ' .$ modifiedAt );
88
93
$ modifiedAt ->setTimezone (new \DateTimeZone ('GMT ' ));
89
94
@@ -92,7 +97,7 @@ protected function createRequest($httpMethod, $path, $body = null, array $header
92
97
sprintf ('%s GMT ' , $ modifiedAt ->format ('l, d-M-y H:i:s ' ))
93
98
);
94
99
}
95
- if ($ etag = $ this ->getCache ()->getETag ($ this ->path )) {
100
+ if ($ etag = $ this ->getCache ()->getETag ($ this ->id )) {
96
101
$ request ->addHeader (
97
102
'If-None-Match ' ,
98
103
$ etag
@@ -114,31 +119,4 @@ public function getLastResponse($force = false)
114
119
115
120
return ($ force ) ? $ lastResponse : $ this ->lastCachedResponse ;
116
121
}
117
-
118
- /**
119
- * Format the path and add query parameters if they exist.
120
- *
121
- * @param string $path
122
- * @param array $options
123
- * @return void
124
- */
125
- private function formatPath ($ path , array $ options )
126
- {
127
- $ this ->path = $ path ;
128
-
129
- if (array_key_exists ('query ' , $ options ) && !empty ($ options ['query ' ])) {
130
- $ this ->path .= '? ' ;
131
-
132
- $ i = 0 ;
133
- foreach ($ options ['query ' ] as $ key => $ value ) {
134
- if ($ i > 0 ) {
135
- $ this ->path .= '& ' ;
136
- }
137
-
138
- $ this ->path .= $ key . '= ' . $ value ;
139
-
140
- $ i ++;
141
- }
142
- }
143
- }
144
122
}
0 commit comments