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 |
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
32
|
|
|
$loader->load('services.xml'); |
33
|
3 |
|
|
34
|
3 |
|
$container->setParameter('noxlogic_rate_limit.enabled', $config['enabled']); |
35
|
3 |
|
|
36
|
|
|
$container->setParameter('noxlogic_rate_limit.rate_response_exception', $config['rate_response_exception']); |
37
|
3 |
|
$container->setParameter('noxlogic_rate_limit.rate_response_code', $config['rate_response_code']); |
38
|
3 |
|
$container->setParameter('noxlogic_rate_limit.rate_response_message', $config['rate_response_message']); |
39
|
3 |
|
|
40
|
3 |
|
$container->setParameter('noxlogic_rate_limit.display_headers', $config['display_headers']); |
41
|
|
|
$container->setParameter('noxlogic_rate_limit.headers.limit.name', $config['headers']['limit']); |
42
|
3 |
|
$container->setParameter('noxlogic_rate_limit.headers.remaining.name', $config['headers']['remaining']); |
43
|
|
|
$container->setParameter('noxlogic_rate_limit.headers.reset.name', $config['headers']['reset']); |
44
|
3 |
|
|
45
|
3 |
|
$container->setParameter('noxlogic_rate_limit.path_limits', $config['path_limits']); |
46
|
|
|
|
47
|
|
|
switch ($config['storage_engine']) { |
48
|
3 |
View Code Duplication |
case 'memcache': |
|
|
|
|
49
|
3 |
|
$container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Memcache'); |
50
|
3 |
|
if (isset($config['memcache_client'])) { |
51
|
|
|
$service = 'memcache.' . $config['memcache_client']; |
52
|
|
|
} else { |
53
|
|
|
$service = $config['memcache_service']; |
54
|
|
|
} |
55
|
|
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
56
|
|
|
0, |
57
|
|
|
new Reference($service) |
58
|
|
|
); |
59
|
|
|
break; |
60
|
|
View Code Duplication |
case 'redis': |
|
|
|
|
61
|
|
|
$container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Redis'); |
62
|
|
|
if (isset($config['redis_client'])) { |
63
|
|
|
$service = 'snc_redis.' . $config['redis_client']; |
64
|
|
|
} else { |
65
|
3 |
|
$service = $config['redis_service']; |
66
|
3 |
|
} |
67
|
|
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
68
|
3 |
|
0, |
69
|
3 |
|
new Reference($service) |
70
|
|
|
); |
71
|
|
|
break; |
72
|
|
View Code Duplication |
case 'doctrine': |
|
|
|
|
73
|
|
|
$container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\DoctrineCache'); |
74
|
|
|
if (isset($config['doctrine_provider'])) { |
75
|
|
|
$service = 'doctrine_cache.providers.' . $config['doctrine_provider']; |
76
|
|
|
} else { |
77
|
|
|
$service = $config['doctrine_service']; |
78
|
|
|
} |
79
|
|
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
80
|
3 |
|
0, |
81
|
3 |
|
new Reference($service) |
82
|
3 |
|
); |
83
|
|
|
break; |
84
|
|
|
case 'php_redis': |
85
|
|
|
$container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\PhpRedis'); |
86
|
3 |
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
87
|
3 |
|
0, |
88
|
3 |
|
new Reference($config['php_redis_service']) |
89
|
|
|
); |
90
|
3 |
|
break; |
91
|
|
|
case 'simple_cache': |
92
|
|
|
$container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\SimpleCache'); |
93
|
|
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
94
|
|
|
0, |
95
|
|
|
new Reference($config['simple_cache_service']) |
96
|
|
|
); |
97
|
|
|
break; |
98
|
|
|
case 'cache': |
99
|
|
|
$container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\PsrCache'); |
100
|
|
|
$container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( |
101
|
|
|
0, |
102
|
|
|
new Reference($config['cache_service']) |
103
|
|
|
); |
104
|
|
|
break; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($config['fos_oauth_key_listener']) { |
108
|
|
|
// Set the SecurityContext for Symfony < 2.6 |
109
|
|
|
// Replace with xml when < 2.6 is dropped. |
110
|
|
|
if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) { |
111
|
|
|
$tokenStorageReference = new Reference('security.token_storage'); |
112
|
|
|
} else { |
113
|
|
|
$tokenStorageReference = new Reference('security.context'); |
114
|
|
|
} |
115
|
|
|
$container->getDefinition('noxlogic_rate_limit.oauth_key_generate_listener')->replaceArgument(0, $tokenStorageReference); |
116
|
|
|
} else { |
117
|
|
|
$container->removeDefinition('noxlogic_rate_limit.oauth_key_generate_listener'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.