-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCloudFlareProvider.php
More file actions
49 lines (40 loc) · 1.23 KB
/
CloudFlareProvider.php
File metadata and controls
49 lines (40 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
namespace Worksome\CdnHeaders\Providers\CloudFlare;
use Worksome\CdnHeaders\Contracts\CdnHeadersProvider;
use Worksome\CdnHeaders\ServerHeadersRepository;
class CloudFlareProvider implements CdnHeadersProvider
{
public function __construct(
private array $config,
private ServerHeadersRepository $serverHeaders,
) {
}
public function getCountryCode(): string|null
{
return $this->serverHeaders->get('HTTP_CF_IPCOUNTRY') ?? $this->config['default-country'] ?? null;
}
public function hasCountryCode(): bool
{
return (bool) $this->getCountryCode();
}
public function getConnectingIp(): string|null
{
return $this->serverHeaders->get('HTTP_CF_CONNECTING_IP');
}
/**
* @link https://support.cloudflare.com/hc/en-us/articles/203118044-What-is-the-CF-RAY-header-#h_f7a7396f-ec41-4c52-abf5-a110cadaca7c
*
* @return non-empty-string|null
*/
public function getTraceId(): string|null
{
return $this->serverHeaders->get('HTTP_CF_RAY');
}
public function boot(): void
{
if ($ip = $this->getConnectingIp()) {
$this->serverHeaders->set('REMOTE_ADDR', $ip);
}
}
}