|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ApiRateLimitBundle |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Indra Gunawan <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Indragunawan\ApiRateLimitBundle\DependencyInjection; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
|
15
|
|
|
use Symfony\Component\Config\FileLocator; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
20
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The extension of this bundle. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Indra Gunawan <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
final class IndragunawanApiRateLimitExtension extends Extension |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
5 |
|
public function load(array $configs, ContainerBuilder $container) |
|
33
|
|
|
{ |
|
34
|
5 |
|
$configuration = new Configuration(); |
|
35
|
5 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
36
|
|
|
|
|
37
|
5 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
38
|
5 |
|
$loader->load('services.xml'); |
|
39
|
|
|
|
|
40
|
5 |
|
$this->registerEventListenerConfig($container, $config); |
|
41
|
5 |
|
$this->registerServiceConfig($container, $config); |
|
42
|
5 |
|
} |
|
43
|
|
|
|
|
44
|
5 |
|
private function registerEventListenerConfig(ContainerBuilder $container, array $config) |
|
45
|
|
|
{ |
|
46
|
5 |
|
$container->getDefinition('indragunawan_api_rate_limit.event_listener.rate_limit') |
|
47
|
5 |
|
->replaceArgument(0, $config['enabled']) |
|
48
|
5 |
|
->replaceArgument(2, $config['exception']); |
|
49
|
|
|
|
|
50
|
5 |
|
$container->getDefinition('indragunawan_api_rate_limit.event_listener.header_modification') |
|
51
|
5 |
|
->replaceArgument(0, $config['header']); |
|
52
|
5 |
|
} |
|
53
|
|
|
|
|
54
|
5 |
|
private function registerServiceConfig(ContainerBuilder $container, array $config) |
|
55
|
|
|
{ |
|
56
|
5 |
|
if (null !== $config['cache']) { |
|
57
|
1 |
|
$cache = new Reference($config['cache']); |
|
58
|
|
|
} else { |
|
59
|
4 |
|
$cache = new Definition(FilesystemAdapter::class, ['api_rate_limit', 0, $container->getParameter('kernel.cache_dir')]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
5 |
|
if ('rate-limit-asc' === $config['throttle']['sort']) { |
|
63
|
1 |
|
uasort($config['throttle']['roles'], function (array $a, array $b) { |
|
64
|
1 |
|
return ($a['limit'] / $a['period']) <=> ($b['limit'] / $b['period']); |
|
65
|
1 |
|
}); |
|
66
|
4 |
|
} elseif ('rate-limit-desc' === $config['throttle']['sort']) { |
|
67
|
3 |
|
uasort($config['throttle']['roles'], function (array $a, array $b) { |
|
68
|
1 |
|
return ($b['limit'] / $b['period']) <=> ($a['limit'] / $a['period']); |
|
69
|
3 |
|
}); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
5 |
|
$container->getDefinition('indragunawan_api_rate_limit.service.rate_limit_handler') |
|
73
|
5 |
|
->replaceArgument(0, $cache) |
|
74
|
5 |
|
->replaceArgument(3, $config['throttle']); |
|
75
|
5 |
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|