3
3
4
4
namespace Ivan770 \HttpClient \Request ;
5
5
6
+ use Illuminate \Contracts \Cache \Repository ;
6
7
use Illuminate \Contracts \Support \Arrayable ;
7
8
use Illuminate \Support \Collection ;
8
9
use Ivan770 \HttpClient \Contracts \PassToBrowserKit ;
9
10
use Ivan770 \HttpClient \Contracts \Request as RequestContract ;
11
+ use Ivan770 \HttpClient \Exceptions \Cache \BrowserKitCache ;
12
+ use Ivan770 \HttpClient \Exceptions \Cache \NullRepository ;
10
13
use Ivan770 \HttpClient \HttpClient ;
14
+ use Ivan770 \HttpClient \Response \Response ;
11
15
use Symfony \Component \BrowserKit \CookieJar ;
12
16
use Symfony \Component \BrowserKit \History ;
13
17
use Symfony \Component \DomCrawler \Crawler ;
@@ -35,6 +39,13 @@ abstract class Request extends BrowserKitRequest implements RequestContract
35
39
*/
36
40
protected $ client ;
37
41
42
+ /**
43
+ * Cache repository instance
44
+ *
45
+ * @var Repository
46
+ */
47
+ protected $ repository ;
48
+
38
49
/**
39
50
* Request URL
40
51
*
@@ -49,12 +60,21 @@ abstract class Request extends BrowserKitRequest implements RequestContract
49
60
*/
50
61
protected $ method = 'GET ' ;
51
62
63
+ /**
64
+ * Request cache key
65
+ *
66
+ * @var mixed
67
+ */
68
+ protected $ cacheKey ;
69
+
52
70
/**
53
71
* @param HttpClient $client
72
+ * @param Repository|null $repository
54
73
*/
55
- public function __construct (HttpClient $ client )
74
+ public function __construct (HttpClient $ client, Repository $ repository = null )
56
75
{
57
76
$ this ->client = $ client ;
77
+ $ this ->repository = $ repository ;
58
78
59
79
$ this ->defaultAttach ($ this ->client );
60
80
}
@@ -113,6 +133,16 @@ public function getResource()
113
133
return $ this ->resource ;
114
134
}
115
135
136
+ /**
137
+ * Cache key getter
138
+ *
139
+ * @return mixed
140
+ */
141
+ public function getCacheKey ()
142
+ {
143
+ return $ this ->cacheKey ;
144
+ }
145
+
116
146
/**
117
147
* Attach builder properties. HttpClient instance is passed into Closure
118
148
*
@@ -140,7 +170,7 @@ public function mock($test)
140
170
/**
141
171
* Run request
142
172
*
143
- * @return \Ivan770\HttpClient\Response\ Response|Crawler
173
+ * @return Response|Crawler
144
174
*/
145
175
public function execute ()
146
176
{
@@ -168,6 +198,29 @@ public function get()
168
198
return $ this ->execute ()->getContent ();
169
199
}
170
200
201
+ /**
202
+ * Get cached response, or run request and save response contents to cache
203
+ *
204
+ * @param $ttl
205
+ * @return mixed
206
+ * @throws BrowserKitCache
207
+ * @throws NullRepository
208
+ */
209
+ public function remember ($ ttl )
210
+ {
211
+ if ($ this ->browserKit ()) {
212
+ throw new BrowserKitCache ();
213
+ }
214
+
215
+ if ($ this ->repository === null ) {
216
+ throw new NullRepository ();
217
+ }
218
+
219
+ return $ this ->repository ->remember ($ this ->getCacheKey (), $ ttl , function () {
220
+ return $ this ->get ();
221
+ });
222
+ }
223
+
171
224
public function __call ($ name , $ arguments )
172
225
{
173
226
$ this ->client ->$ name (...$ arguments );
0 commit comments