-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUnleashExtension.php
81 lines (67 loc) · 2.68 KB
/
UnleashExtension.php
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Stogon\UnleashBundle\DependencyInjection;
use Stogon\UnleashBundle\Repository\FeatureRepository;
use Stogon\UnleashBundle\Strategy\StrategyInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Reference;
class UnleashExtension extends Extension implements PrependExtensionInterface
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new PhpFileLoader(
$container,
new FileLocator(__DIR__.'/../../config/')
);
$loader->load('services.php');
$container->registerForAutoconfiguration(StrategyInterface::class)->addTag('unleash.strategy');
$definition = $container->getDefinition(FeatureRepository::class);
$definition->replaceArgument('$cache', new Reference($container->getParameter('unleash.cache.service')));
}
public function prepend(ContainerBuilder $container): void
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $container->getExtensionConfig($this->getAlias()));
$container->setParameter('unleash.api_url', $config['api_url']);
$container->setParameter('unleash.auth_token', $config['auth_token'] ?? null);
$container->setParameter('unleash.instance_id', $config['instance_id']);
$container->setParameter('unleash.environment', $config['environment']);
$container->setParameter('unleash.cache.service', $config['cache']['service']);
$container->setParameter('unleash.cache.ttl', $config['cache']['enabled'] ? $config['cache']['ttl'] : 0);
$container->prependExtensionConfig('framework', [
'http_client' => [
'scoped_clients' => [
'unleash.client' => [
'base_uri' => '%unleash.api_url%',
'headers' => [
'Accept' => 'application/json',
'UNLEASH-APPNAME' => '%unleash.environment%',
'UNLEASH-INSTANCEID' => '%unleash.instance_id%',
'Authorization' => '%unleash.auth_token%',
],
],
],
],
]);
if ($config['cache']['enabled'] && $config['cache']['service'] === null) {
$container->prependExtensionConfig('framework', [
'cache' => [
'pools' => [
'cache.unleash.strategies' => null,
],
],
]);
$config['cache']['service'] = 'cache.unleash.strategies';
$container->setParameter('unleash.cache.service', $config['cache']['service']);
} else {
$container->setParameter('unleash.cache.service', 'cache.app');
}
}
public function getAlias(): string
{
return 'unleash';
}
}