|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Noxlogic\RateLimitBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Config\FileLocator; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
9
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This is the class that loads and manages your bundle configuration |
|
13
|
|
|
* |
|
14
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
|
15
|
|
|
*/ |
|
16
|
|
|
class NoxlogicRateLimitExtension extends Extension |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritDoc} |
|
20
|
|
|
*/ |
|
21
|
3 |
|
public function load(array $configs, ContainerBuilder $container) |
|
22
|
|
|
{ |
|
23
|
3 |
|
$configuration = new Configuration(); |
|
24
|
3 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
25
|
3 |
|
$this->loadServices($container, $config); |
|
26
|
|
|
|
|
27
|
3 |
|
} |
|
28
|
|
|
|
|
29
|
3 |
|
private function loadServices(ContainerBuilder $container, array $config) |
|
30
|
|
|
{ |
|
31
|
3 |
|
$container->setParameter('noxlogic_rate_limit.enabled', $config['enabled']); |
|
32
|
|
|
|
|
33
|
3 |
|
$container->setParameter('noxlogic_rate_limit.rate_response_exception', $config['rate_response_exception']); |
|
34
|
3 |
|
$container->setParameter('noxlogic_rate_limit.rate_response_code', $config['rate_response_code']); |
|
35
|
3 |
|
$container->setParameter('noxlogic_rate_limit.rate_response_message', $config['rate_response_message']); |
|
36
|
|
|
|
|
37
|
3 |
|
$container->setParameter('noxlogic_rate_limit.display_headers', $config['display_headers']); |
|
38
|
3 |
|
$container->setParameter('noxlogic_rate_limit.headers.limit.name', $config['headers']['limit']); |
|
39
|
3 |
|
$container->setParameter('noxlogic_rate_limit.headers.remaining.name', $config['headers']['remaining']); |
|
40
|
3 |
|
$container->setParameter('noxlogic_rate_limit.headers.reset.name', $config['headers']['reset']); |
|
41
|
|
|
|
|
42
|
3 |
|
$container->setParameter('noxlogic_rate_limit.path_limits', $config['path_limits']); |
|
43
|
|
|
|
|
44
|
3 |
|
switch ($config['storage_engine']) { |
|
45
|
3 |
|
case 'memcache': |
|
46
|
|
|
$container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Memcache'); |
|
47
|
|
|
break; |
|
48
|
3 |
|
case 'redis': |
|
49
|
3 |
|
$container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Redis'); |
|
50
|
3 |
|
break; |
|
51
|
|
|
case 'doctrine': |
|
52
|
|
|
$container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\DoctrineCache'); |
|
53
|
|
|
break; |
|
54
|
|
|
case 'php_redis'; |
|
55
|
|
|
$container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\PhpRedis'); |
|
56
|
|
|
break; |
|
57
|
|
|
case 'simple_cache': |
|
58
|
|
|
$container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\SimpleCache'); |
|
59
|
|
|
break; |
|
60
|
|
|
case 'cache': |
|
61
|
|
|
$container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\PsrCache'); |
|
62
|
|
|
break; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
|
66
|
3 |
|
$loader->load('services.xml'); |
|
67
|
|
|
|
|
68
|
3 |
|
switch ($config['storage_engine']) { |
|
69
|
3 |
|
case 'memcache': |
|
70
|
|
|
if (isset($config['memcache_client'])) { |
|
71
|
|
|
$service = 'memcache.' . $config['memcache_client']; |
|
72
|
|
|
} else { |
|
73
|
|
|
$service = $config['memcache_service']; |
|
74
|
|
|
} |
|
75
|
|
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
|
76
|
|
|
0, |
|
77
|
|
|
new Reference($service) |
|
78
|
|
|
); |
|
79
|
|
|
break; |
|
80
|
3 |
|
case 'redis': |
|
81
|
3 |
|
if (isset($config['redis_client'])) { |
|
82
|
3 |
|
$service = 'snc_redis.' . $config['redis_client']; |
|
83
|
|
|
} else { |
|
84
|
|
|
$service = $config['redis_service']; |
|
85
|
|
|
} |
|
86
|
3 |
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
|
87
|
3 |
|
0, |
|
88
|
3 |
|
new Reference($service) |
|
89
|
|
|
); |
|
90
|
3 |
|
break; |
|
91
|
|
|
case 'doctrine': |
|
92
|
|
|
if (isset($config['doctrine_provider'])) { |
|
93
|
|
|
$service = 'doctrine_cache.providers.' . $config['doctrine_provider']; |
|
94
|
|
|
} else { |
|
95
|
|
|
$service = $config['doctrine_service']; |
|
96
|
|
|
} |
|
97
|
|
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
|
98
|
|
|
0, |
|
99
|
|
|
new Reference($service) |
|
100
|
|
|
); |
|
101
|
|
|
break; |
|
102
|
|
|
case 'php_redis': |
|
103
|
|
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
|
104
|
|
|
0, |
|
105
|
|
|
new Reference($config['php_redis_service']) |
|
106
|
|
|
); |
|
107
|
|
|
break; |
|
108
|
|
|
case 'simple_cache': |
|
109
|
|
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
|
110
|
|
|
0, |
|
111
|
|
|
new Reference($config['simple_cache_service']) |
|
112
|
|
|
); |
|
113
|
|
|
break; |
|
114
|
|
|
case 'cache': |
|
115
|
|
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
|
116
|
|
|
0, |
|
117
|
|
|
new Reference($config['cache_service']) |
|
118
|
|
|
); |
|
119
|
|
|
break; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
3 |
|
if ($config['fos_oauth_key_listener']) { |
|
123
|
|
|
// Set the SecurityContext for Symfony < 2.6 |
|
124
|
|
|
// Replace with xml when < 2.6 is dropped. |
|
125
|
3 |
|
if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) { |
|
126
|
3 |
|
$tokenStorageReference = new Reference('security.token_storage'); |
|
127
|
|
|
} else { |
|
128
|
|
|
$tokenStorageReference = new Reference('security.context'); |
|
129
|
|
|
} |
|
130
|
3 |
|
$container->getDefinition('noxlogic_rate_limit.oauth_key_generate_listener')->replaceArgument(0, $tokenStorageReference); |
|
131
|
|
|
} else { |
|
132
|
|
|
$container->removeDefinition('noxlogic_rate_limit.oauth_key_generate_listener'); |
|
133
|
|
|
} |
|
134
|
3 |
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|