Skip to content

Commit 171fab1

Browse files
committed
Add caching support
1 parent faa8ff8 commit 171fab1

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Ivan770\HttpClient\Exceptions\Cache;
4+
5+
use Exception;
6+
7+
class BrowserKitCache extends Exception
8+
{
9+
protected $message = "BrowserKit caching is not supported";
10+
11+
public function __toString() {
12+
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
13+
}
14+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Ivan770\HttpClient\Exceptions\Cache;
4+
5+
use Exception;
6+
7+
class NullRepository extends Exception
8+
{
9+
protected $message = "Cache repository was not provided";
10+
11+
public function __toString() {
12+
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
13+
}
14+
}

src/Request/Request.php

+55-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33

44
namespace Ivan770\HttpClient\Request;
55

6+
use Illuminate\Contracts\Cache\Repository;
67
use Illuminate\Contracts\Support\Arrayable;
78
use Illuminate\Support\Collection;
89
use Ivan770\HttpClient\Contracts\PassToBrowserKit;
910
use Ivan770\HttpClient\Contracts\Request as RequestContract;
11+
use Ivan770\HttpClient\Exceptions\Cache\BrowserKitCache;
12+
use Ivan770\HttpClient\Exceptions\Cache\NullRepository;
1013
use Ivan770\HttpClient\HttpClient;
14+
use Ivan770\HttpClient\Response\Response;
1115
use Symfony\Component\BrowserKit\CookieJar;
1216
use Symfony\Component\BrowserKit\History;
1317
use Symfony\Component\DomCrawler\Crawler;
@@ -35,6 +39,13 @@ abstract class Request extends BrowserKitRequest implements RequestContract
3539
*/
3640
protected $client;
3741

42+
/**
43+
* Cache repository instance
44+
*
45+
* @var Repository
46+
*/
47+
protected $repository;
48+
3849
/**
3950
* Request URL
4051
*
@@ -49,12 +60,21 @@ abstract class Request extends BrowserKitRequest implements RequestContract
4960
*/
5061
protected $method = 'GET';
5162

63+
/**
64+
* Request cache key
65+
*
66+
* @var mixed
67+
*/
68+
protected $cacheKey;
69+
5270
/**
5371
* @param HttpClient $client
72+
* @param Repository|null $repository
5473
*/
55-
public function __construct(HttpClient $client)
74+
public function __construct(HttpClient $client, Repository $repository = null)
5675
{
5776
$this->client = $client;
77+
$this->repository = $repository;
5878

5979
$this->defaultAttach($this->client);
6080
}
@@ -113,6 +133,16 @@ public function getResource()
113133
return $this->resource;
114134
}
115135

136+
/**
137+
* Cache key getter
138+
*
139+
* @return mixed
140+
*/
141+
public function getCacheKey()
142+
{
143+
return $this->cacheKey;
144+
}
145+
116146
/**
117147
* Attach builder properties. HttpClient instance is passed into Closure
118148
*
@@ -140,7 +170,7 @@ public function mock($test)
140170
/**
141171
* Run request
142172
*
143-
* @return \Ivan770\HttpClient\Response\Response|Crawler
173+
* @return Response|Crawler
144174
*/
145175
public function execute()
146176
{
@@ -168,6 +198,29 @@ public function get()
168198
return $this->execute()->getContent();
169199
}
170200

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+
171224
public function __call($name, $arguments)
172225
{
173226
$this->client->$name(...$arguments);

0 commit comments

Comments
 (0)