|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\Imagine\Cache\Resolver; |
|
13
|
|
|
|
|
14
|
|
|
use Liip\ImagineBundle\Binary\BinaryInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* ProxyResolver. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Robert Schönthal <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class ProxyResolver implements ResolverInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ResolverInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $resolver; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* a list of proxy hosts (picks a random one for each generation to seed browser requests among multiple hosts). |
|
30
|
|
|
* |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $hosts = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string[] $hosts |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(ResolverInterface $resolver, array $hosts) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->resolver = $resolver; |
|
41
|
|
|
$this->hosts = $hosts; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function resolve($path, $filter) |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->rewriteUrl($this->resolver->resolve($path, $filter)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function store(BinaryInterface $binary, $targetPath, $filter) |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->resolver->store($binary, $targetPath, $filter); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function isStored($path, $filter) |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->resolver->isStored($path, $filter); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function remove(array $paths, array $filters) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->resolver->remove($paths, $filters); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param $url |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function rewriteUrl($url) |
|
82
|
|
|
{ |
|
83
|
|
|
if (empty($this->hosts)) { |
|
84
|
|
|
return $url; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$randKey = array_rand($this->hosts, 1); |
|
88
|
|
|
|
|
89
|
|
|
// BC |
|
90
|
|
|
if (is_numeric($randKey)) { |
|
91
|
|
|
$host = parse_url($url, PHP_URL_SCHEME).'://'.parse_url($url, PHP_URL_HOST); |
|
92
|
|
|
$proxyHost = $this->hosts[$randKey]; |
|
93
|
|
|
|
|
94
|
|
|
return str_replace($host, $proxyHost, $url); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (0 === mb_strpos($randKey, 'regexp/')) { |
|
98
|
|
|
$regExp = mb_substr($randKey, 6); |
|
99
|
|
|
|
|
100
|
|
|
return preg_replace($regExp, $this->hosts[$randKey], $url); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return str_replace($randKey, $this->hosts[$randKey], $url); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|