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
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
57
|
3 |
|
$loader->load('services.xml'); |
58
|
|
|
|
59
|
3 |
|
switch ($config['storage_engine']) { |
60
|
3 |
|
case 'memcache': |
61
|
|
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
62
|
|
|
0, |
63
|
|
|
new Reference('memcache.' . $config['memcache_client']) |
64
|
|
|
); |
65
|
|
|
break; |
66
|
3 |
|
case 'redis': |
67
|
3 |
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
68
|
3 |
|
0, |
69
|
3 |
|
new Reference('snc_redis.' . $config['redis_client']) |
70
|
|
|
); |
71
|
3 |
|
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
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Set the SecurityContext for Symfony < 2.6 |
81
|
|
|
// Replace with xml when < 2.6 is dropped. |
82
|
3 |
|
if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) { |
83
|
3 |
|
$tokenStorageReference = new Reference('security.token_storage'); |
84
|
|
|
} else { |
85
|
|
|
$tokenStorageReference = new Reference('security.context'); |
86
|
|
|
} |
87
|
3 |
|
$container->getDefinition('noxlogic_rate_limit.rate_limit_service')->replaceArgument(0, $tokenStorageReference); |
88
|
3 |
|
$container->getDefinition('noxlogic_rate_limit.oauth_key_generate_listener')->replaceArgument(0, $tokenStorageReference); |
89
|
3 |
|
} |
90
|
|
|
} |
91
|
|
|
|