Skip to content

Commit 2d3f198

Browse files
authored
make cache purge environment aware (#267)
1 parent 20da7aa commit 2d3f198

File tree

3 files changed

+169
-5
lines changed

3 files changed

+169
-5
lines changed

src/Endpoints/Zones.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Created by PhpStorm.
45
* User: junade
@@ -216,9 +217,18 @@ public function setCachingLevel(string $zoneID, string $level = 'aggressive'): b
216217
* Purge Everything
217218
* @param string $zoneID
218219
* @return bool
220+
*
221+
* @SuppressWarnings(PHPMD)
219222
*/
220-
public function cachePurgeEverything(string $zoneID): bool
223+
public function cachePurgeEverything(string $zoneID, bool $includeEnvironments = false): bool
221224
{
225+
if ($includeEnvironments) {
226+
$env = $this->adapter->get("zones/$zoneID/environments");
227+
$envs = json_decode($env->getBody(), true);
228+
foreach ($envs["result"]["environments"] as $env) {
229+
$this->adapter->post("zones/$zoneID/environments/{$env["ref"]}/purge_cache", ['purge_everything' => true]);
230+
}
231+
}
222232
$user = $this->adapter->post('zones/' . $zoneID . '/purge_cache', ['purge_everything' => true]);
223233

224234
$this->body = json_decode($user->getBody());
@@ -230,7 +240,10 @@ public function cachePurgeEverything(string $zoneID): bool
230240
return false;
231241
}
232242

233-
public function cachePurge(string $zoneID, array $files = null, array $tags = null, array $hosts = null): bool
243+
/**
244+
* @SuppressWarnings(PHPMD)
245+
*/
246+
public function cachePurge(string $zoneID, array $files = null, array $tags = null, array $hosts = null, bool $includeEnvironments = false): bool
234247
{
235248
if ($files === null && $tags === null && $hosts === null) {
236249
throw new EndpointException('No files, tags or hosts to purge.');
@@ -249,6 +262,14 @@ public function cachePurge(string $zoneID, array $files = null, array $tags = nu
249262
$options['hosts'] = $hosts;
250263
}
251264

265+
if ($includeEnvironments) {
266+
$env = $this->adapter->get("zones/$zoneID/environments");
267+
$envs = json_decode($env->getBody(), true);
268+
foreach ($envs["result"]["environments"] as $env) {
269+
$this->adapter->post("zones/$zoneID/environments/{$env["ref"]}/purge_cache", $options);
270+
}
271+
}
272+
252273
$user = $this->adapter->post('zones/' . $zoneID . '/purge_cache', $options);
253274

254275
$this->body = json_decode($user->getBody());

tests/Endpoints/ZoneCacheTest.php

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ public function testCachePurge()
6161
->method('post')
6262
->with(
6363
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
64-
$this->equalTo(['files' => [
65-
'https://example.com/file.jpg',
66-
]
64+
$this->equalTo([
65+
'files' => [
66+
'https://example.com/file.jpg',
67+
]
6768
])
6869
);
6970

@@ -75,4 +76,106 @@ public function testCachePurge()
7576
$this->assertTrue($result);
7677
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
7778
}
79+
80+
public function testCachePurgeIncludingEnvironments()
81+
{
82+
$envResp = $this->getPsr7JsonResponseForFixture('Endpoints/getEnvironments.json');
83+
$cacheResp = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurge.json');
84+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
85+
86+
$mock->expects($this->once())
87+
->method('get')
88+
->willReturn($envResp)
89+
->with(
90+
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments')
91+
);
92+
93+
$mock->expects($this->exactly(4))
94+
->method('post')
95+
->willReturn($cacheResp)
96+
->withConsecutive(
97+
[
98+
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/first/purge_cache'),
99+
$this->equalTo([
100+
'files' => [
101+
'https://example.com/file.jpg',
102+
]
103+
])
104+
],
105+
[
106+
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/second/purge_cache'),
107+
$this->equalTo([
108+
'files' => [
109+
'https://example.com/file.jpg',
110+
]
111+
])
112+
],
113+
[
114+
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/third/purge_cache'),
115+
$this->equalTo([
116+
'files' => [
117+
'https://example.com/file.jpg',
118+
]
119+
])
120+
],
121+
[
122+
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
123+
$this->equalTo([
124+
'files' => [
125+
'https://example.com/file.jpg',
126+
]
127+
])
128+
]
129+
);
130+
131+
$zones = new \Cloudflare\API\Endpoints\Zones($mock);
132+
$result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [
133+
'https://example.com/file.jpg',
134+
], null, null, true);
135+
136+
$this->assertTrue($result);
137+
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
138+
}
139+
140+
public function testCachePurgeEverythingIncludingEnvironments()
141+
{
142+
$envResp = $this->getPsr7JsonResponseForFixture('Endpoints/getEnvironments.json');
143+
$cacheResp = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgeEverything.json');
144+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
145+
146+
$mock->expects($this->once())
147+
->method('get')
148+
->willReturn($envResp)
149+
->with(
150+
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments')
151+
);
152+
153+
$mock->expects($this->exactly(4))
154+
->method('post')
155+
->willReturn($cacheResp)
156+
->withConsecutive(
157+
[
158+
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/first/purge_cache'),
159+
$this->equalTo(['purge_everything' => true])
160+
],
161+
[
162+
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/second/purge_cache'),
163+
$this->equalTo(['purge_everything' => true])
164+
],
165+
[
166+
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/third/purge_cache'),
167+
$this->equalTo(['purge_everything' => true])
168+
],
169+
[
170+
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
171+
$this->equalTo(['purge_everything' => true])
172+
]
173+
);
174+
175+
$zones = new \Cloudflare\API\Endpoints\Zones($mock);
176+
$result = $zones->cachePurgeEverything('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', true);
177+
178+
$this->assertTrue($result);
179+
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
180+
}
78181
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"result": {
3+
"environments": [
4+
{
5+
"name": "Production",
6+
"ref": "first",
7+
"version": 0,
8+
"expression": "expression_1",
9+
"locked_on_deployment": false,
10+
"position": {
11+
"before": "second"
12+
}
13+
},
14+
{
15+
"name": "Staging",
16+
"ref": "second",
17+
"version": 0,
18+
"expression": "expression_2",
19+
"locked_on_deployment": false,
20+
"position": {
21+
"before": "third",
22+
"after": "first"
23+
}
24+
},
25+
{
26+
"name": "Development",
27+
"ref": "third",
28+
"version": 0,
29+
"expression": "expression_3",
30+
"locked_on_deployment": false,
31+
"position": {
32+
"after": "second"
33+
}
34+
}
35+
]
36+
},
37+
"success": true,
38+
"errors": [],
39+
"messages": []
40+
}

0 commit comments

Comments
 (0)