Skip to content

Commit efc0fb9

Browse files
committed
Updated composer added disable option
1 parent a17c6bb commit efc0fb9

3 files changed

Lines changed: 71 additions & 8 deletions

File tree

composer.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@
1818
"minimum-stability": "stable",
1919
"require": {
2020
"php": "^8.0",
21-
"sammyjo20/saloon": "^1.0",
21+
"sammyjo20/saloon": "^1.0.1",
2222
"nesbot/carbon": "^2.57"
2323
},
2424
"require-dev": {
25-
"friendsofphp/php-cs-fixer": "^3.5",
26-
"pestphp/pest": "^1.21",
27-
"spatie/ray": "^1.33",
25+
"friendsofphp/php-cs-fixer": "^3.8",
26+
"pestphp/pest": "^1.21.2",
27+
"spatie/ray": "^1.34.2",
2828
"league/flysystem": "^3.0",
29-
"illuminate/cache": "^8.0|^9.0",
30-
"orchestra/testbench": "^7.4"
29+
"orchestra/testbench": "^7.0"
3130
},
3231
"scripts": {
3332
"test": [

src/Traits/AlwaysCacheResponses.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ trait AlwaysCacheResponses
1515
*
1616
* @var array
1717
*/
18-
private array $safeMethods = [
18+
private array $safeCacheMethods = [
1919
Saloon::GET,
2020
Saloon::OPTIONS,
2121
];
2222

23+
/**
24+
* Is caching enabled?
25+
*
26+
* @var bool
27+
*/
28+
protected bool $cachingEnabled = true;
29+
2330
/**
2431
* Boot the Saloon plugin
2532
*
@@ -30,10 +37,14 @@ trait AlwaysCacheResponses
3037
*/
3138
public function bootAlwaysCacheResponses(SaloonRequest $request): void
3239
{
40+
if ($this->cachingEnabled === false) {
41+
return;
42+
}
43+
3344
// We should only cache on "read-only" methods and not on methods
3445
// like POST, PUT.
3546

36-
if (! in_array($request->getMethod(), $this->safeMethods, true)) {
47+
if (! in_array($request->getMethod(), $this->safeCacheMethods, true)) {
3748
return;
3849
}
3950

@@ -86,6 +97,30 @@ public function generateCacheKey(SaloonRequest $request, array $headers): string
8697
return hash('sha256', $this->cacheKey($request, $headers));
8798
}
8899

100+
/**
101+
* Enable caching for the request.
102+
*
103+
* @return $this
104+
*/
105+
public function enableCaching(): self
106+
{
107+
$this->cachingEnabled = true;
108+
109+
return $this;
110+
}
111+
112+
/**
113+
* Disable caching for the request.
114+
*
115+
* @return $this
116+
*/
117+
public function disableCaching(): self
118+
{
119+
$this->cachingEnabled = false;
120+
121+
return $this;
122+
}
123+
89124
/**
90125
* Return an instance of the cache driver that should be used.
91126
*

tests/Feature/CacheTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,32 @@
172172
expect($filesystem->directoryExists('custom'))->toBeTrue();
173173
expect(count($filesystem->listContents('custom')->toArray()))->toEqual(1);
174174
});
175+
176+
test('you can disable the cache', function () {
177+
$mockClient = new MockClient([
178+
MockResponse::make(['name' => 'Sam']),
179+
MockResponse::make(['name' => 'Gareth']),
180+
MockResponse::make(['name' => 'Michael']),
181+
]);
182+
183+
$requestA = new CachedUserRequest();
184+
$responseA = $requestA->send($mockClient);
185+
186+
expect($responseA->isCached())->toBeFalse();
187+
expect($responseA->json())->toEqual(['name' => 'Sam']);
188+
189+
$requestB = new CachedUserRequest();
190+
$responseB = $requestB->send($mockClient);
191+
192+
expect($responseB->isCached())->toBeTrue();
193+
expect($responseB->header('X-Saloon-Cache'))->toEqual('Cached');
194+
expect($responseB->json())->toEqual(['name' => 'Sam']);
195+
196+
$requestC = new CachedUserRequest();
197+
$requestC->disableCaching();
198+
199+
$responseC = $requestC->send($mockClient);
200+
201+
expect($responseC->isCached())->toBeFalse();
202+
expect($responseC->json())->toEqual(['name' => 'Michael']);
203+
});

0 commit comments

Comments
 (0)