1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\QueryRepository\Cdn; |
6
|
|
|
|
7
|
|
|
use BEAR\QueryRepository\CdnCacheControlHeaderSetterInterface; |
8
|
|
|
use BEAR\QueryRepository\PurgerInterface; |
9
|
|
|
use Fastly\Api\PurgeApi; |
10
|
|
|
use Fastly\Configuration; |
11
|
|
|
use GuzzleHttp\Client; |
12
|
|
|
use GuzzleHttp\ClientInterface; |
13
|
|
|
use Ray\Di\AbstractModule; |
14
|
|
|
use ReflectionException; |
15
|
|
|
|
16
|
|
|
class FastlyModule extends AbstractModule |
17
|
|
|
{ |
18
|
|
|
private string $fastlyApiKey; |
19
|
|
|
private string $fastlyServiceId; |
20
|
|
|
private bool $enablePurge; |
21
|
|
|
private bool $enableSoftPurge; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
string $fastlyApiKey, |
25
|
|
|
string $fastlyServiceId, |
26
|
|
|
bool $enablePurge = false, |
27
|
|
|
bool $enableSoftPurge = true, |
28
|
|
|
?AbstractModule $module = null |
29
|
|
|
) { |
30
|
|
|
$this->fastlyApiKey = $fastlyApiKey; |
31
|
|
|
$this->fastlyServiceId = $fastlyServiceId; |
32
|
|
|
$this->enablePurge = $enablePurge; |
33
|
|
|
$this->enableSoftPurge = $enableSoftPurge; |
34
|
|
|
|
35
|
|
|
parent::__construct($module); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @throws ReflectionException |
40
|
|
|
*/ |
41
|
|
|
protected function configure(): void |
42
|
|
|
{ |
43
|
|
|
$this->bind(Configuration::class)->annotatedWith(Configuration::class)->toInstance( |
44
|
|
|
Configuration::getDefaultConfiguration()->setApiToken($this->fastlyApiKey) |
45
|
|
|
); |
46
|
|
|
$this->bind(PurgeApi::class)->toConstructor(PurgeApi::class, [ |
47
|
|
|
'config' => Configuration::class, |
48
|
|
|
]); |
49
|
|
|
$this->bind()->annotatedWith('FASTLY_SERVICE_ID')->toInstance($this->fastlyServiceId); |
50
|
|
|
$this->bind()->annotatedWith('FASTLY_ENABLE_PURGE')->toInstance($this->enablePurge); |
51
|
|
|
$this->bind()->annotatedWith('FASTLY_ENABLE_SOFT_PURGE')->toInstance($this->enableSoftPurge); |
52
|
|
|
$this->bind(ClientInterface::class)->annotatedWith('fastly') |
53
|
|
|
->toConstructor(Client::class, ['config' => 'fastly_http_client_options']); |
54
|
|
|
$this->bind(PurgerInterface::class)->to(FastlyCachePurger::class); |
55
|
|
|
$this->bind(CdnCacheControlHeaderSetterInterface::class)->to(FastlyCacheControlHeaderSetter::class); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|