1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Noxlogic\RateLimitBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
6
|
|
|
use Symfony\Component\Config\FileLocator; |
7
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
8
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
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
|
|
|
|
26
|
3 |
|
if ($config['enabled'] === true) { |
27
|
2 |
|
$this->loadServices($container, $config); |
28
|
|
|
} |
29
|
1 |
|
} |
30
|
|
|
|
31
|
2 |
|
private function loadServices(ContainerBuilder $container, array $config) |
32
|
|
|
{ |
33
|
2 |
|
$container->setParameter('noxlogic_rate_limit.rate_response_exception', $config['rate_response_exception']); |
34
|
2 |
|
$container->setParameter('noxlogic_rate_limit.rate_response_code', $config['rate_response_code']); |
35
|
2 |
|
$container->setParameter('noxlogic_rate_limit.rate_response_message', $config['rate_response_message']); |
36
|
|
|
|
37
|
2 |
|
$container->setParameter('noxlogic_rate_limit.display_headers', $config['display_headers']); |
38
|
2 |
|
$container->setParameter('noxlogic_rate_limit.headers.limit.name', $config['headers']['limit']); |
39
|
2 |
|
$container->setParameter('noxlogic_rate_limit.headers.remaining.name', $config['headers']['remaining']); |
40
|
2 |
|
$container->setParameter('noxlogic_rate_limit.headers.reset.name', $config['headers']['reset']); |
41
|
|
|
|
42
|
2 |
|
$container->setParameter('noxlogic_rate_limit.path_limits', $config['path_limits']); |
43
|
|
|
|
44
|
2 |
|
switch ($config['storage_engine']) { |
45
|
2 |
|
case 'memcache': |
46
|
|
|
$container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Memcache'); |
47
|
|
|
break; |
48
|
2 |
|
case 'redis': |
49
|
2 |
|
$container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Redis'); |
50
|
2 |
|
break; |
51
|
|
|
case 'doctrine': |
52
|
|
|
$container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\DoctrineCache'); |
53
|
|
|
break; |
54
|
2 |
|
} |
55
|
|
|
|
56
|
2 |
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
57
|
2 |
|
$loader->load('services.xml'); |
58
|
|
|
|
59
|
2 |
|
switch ($config['storage_engine']) { |
60
|
2 |
|
case 'memcache': |
61
|
|
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
62
|
|
|
0, |
63
|
|
|
new Reference('memcache.' . $config['memcache_client']) |
64
|
|
|
); |
65
|
|
|
break; |
66
|
2 |
|
case 'redis': |
67
|
2 |
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
68
|
2 |
|
0, |
69
|
2 |
|
new Reference('snc_redis.' . $config['redis_client']) |
70
|
2 |
|
); |
71
|
2 |
|
break; |
72
|
|
|
case 'doctrine': |
73
|
|
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
74
|
|
|
0, |
75
|
|
|
new Reference('doctrine_cache.providers.' . $config['doctrine_provider']) |
76
|
|
|
); |
77
|
|
|
break; |
78
|
2 |
|
} |
79
|
|
|
|
80
|
2 |
|
$container->getDefinition('noxlogic_rate_limit.rate_limit_service')->replaceArgument(0, $$container->getDefinition('noxlogic_rate_limit.storage')); |
81
|
|
|
$container->getDefinition('noxlogic_rate_limit.oauth_key_generate_listener')->replaceArgument(0, $$container->getDefinition('noxlogic_rate_limit.storage')); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|