|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSHttpCache package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace FOS\HttpCache\ProxyClient; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable; |
|
15
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\RefreshCapable; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* NGINX HTTP cache invalidator. |
|
19
|
|
|
* |
|
20
|
|
|
* Additional constructor options: |
|
21
|
|
|
* - purge_location: Path location that triggers purging. String, or set to |
|
22
|
|
|
* boolean false for same location purging. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Simone Fumagalli <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class Nginx extends HttpProxyClient implements PurgeCapable, RefreshCapable |
|
27
|
|
|
{ |
|
28
|
|
|
public const HTTP_METHOD_PURGE = 'PURGE'; |
|
29
|
|
|
|
|
30
|
|
|
public const HTTP_METHOD_REFRESH = 'GET'; |
|
31
|
|
|
|
|
32
|
|
|
public const HTTP_HEADER_REFRESH = 'X-Refresh'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function refresh($url, array $headers = []) |
|
38
|
|
|
{ |
|
39
|
1 |
|
$headers = array_merge($headers, [self::HTTP_HEADER_REFRESH => '1']); |
|
40
|
1 |
|
$this->queueRequest(self::HTTP_METHOD_REFRESH, $url, $headers); |
|
41
|
|
|
|
|
42
|
1 |
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
4 |
|
public function purge($url, array $headers = []) |
|
49
|
|
|
{ |
|
50
|
4 |
|
$purgeUrl = $this->buildPurgeUrl($url); |
|
51
|
4 |
|
$this->queueRequest(self::HTTP_METHOD_PURGE, $purgeUrl, $headers); |
|
52
|
|
|
|
|
53
|
4 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
5 |
|
protected function configureOptions() |
|
60
|
|
|
{ |
|
61
|
5 |
|
$resolver = parent::configureOptions(); |
|
62
|
5 |
|
$resolver->setDefaults(['purge_location' => false]); |
|
63
|
|
|
|
|
64
|
5 |
|
return $resolver; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Create the correct URL to purge a resource. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $url URL |
|
71
|
|
|
* |
|
72
|
|
|
* @return string Rewritten URL |
|
73
|
|
|
*/ |
|
74
|
4 |
|
private function buildPurgeUrl($url) |
|
75
|
|
|
{ |
|
76
|
4 |
|
if (!$this->options['purge_location']) { |
|
77
|
2 |
|
return $url; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
$urlParts = parse_url($url); |
|
81
|
|
|
|
|
82
|
2 |
|
if (isset($urlParts['host'])) { |
|
83
|
1 |
|
$pathStartAt = strpos($url, $urlParts['path']); |
|
84
|
1 |
|
$purgeUrl = substr($url, 0, $pathStartAt).$this->options['purge_location'].substr($url, $pathStartAt); |
|
85
|
|
|
} else { |
|
86
|
1 |
|
$purgeUrl = $this->options['purge_location'].$url; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
return $purgeUrl; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|