ReverseProxy.php
61 lines
| 1 | <?php |
| 2 | namespace NitroPack\SDK\Integrations; |
| 3 | use \NitroPack\HttpClient\HttpClient; |
| 4 | use \NitroPack\HttpClient\HttpClientMulti; |
| 5 | |
| 6 | class ReverseProxy { |
| 7 | |
| 8 | protected $serverList; |
| 9 | protected $purgeMethod; |
| 10 | protected $headers; |
| 11 | |
| 12 | public function __construct($serverList=null, $purgeMethod="PURGE", $headers = []) { |
| 13 | $this->serverList = $serverList; |
| 14 | $this->purgeMethod = $purgeMethod; |
| 15 | $this->headers = $headers; |
| 16 | } |
| 17 | |
| 18 | public function setServerList($serverList=null) { |
| 19 | $this->serverList = $serverList; |
| 20 | } |
| 21 | |
| 22 | public function setPurgeMethod($method) { |
| 23 | $this->purgeMethod = $method; |
| 24 | } |
| 25 | |
| 26 | public function setHeader($name, $value) { |
| 27 | $this->headers[$name] = $value; |
| 28 | } |
| 29 | |
| 30 | public function purge($url, $newPurgeScheme = false) { |
| 31 | if ($newPurgeScheme) { |
| 32 | $client = new HttpClient($url); |
| 33 | $client->doNotDownload = true; |
| 34 | |
| 35 | foreach ($this->headers as $name => $value) { |
| 36 | $client->setHeader($name, $value); |
| 37 | } |
| 38 | |
| 39 | $client->fetch(true, $this->purgeMethod); |
| 40 | |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | if (empty($this->serverList)) return false; |
| 45 | |
| 46 | $httpMulti = new HttpClientMulti(); |
| 47 | foreach ($this->serverList as $server) { |
| 48 | $client = new HttpClient($url); |
| 49 | $client->hostOverride($client->host, $server); |
| 50 | $client->doNotDownload = true; |
| 51 | |
| 52 | foreach ($this->headers as $name => $value) { |
| 53 | $client->setHeader($name, $value); |
| 54 | } |
| 55 | |
| 56 | $httpMulti->push($client); |
| 57 | } |
| 58 | |
| 59 | $httpMulti->fetchAll(true, $this->purgeMethod); |
| 60 | } |
| 61 | } |