1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Noxlogic\RateLimitBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
6
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
7
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This is the class that validates and merges configuration from your app/config files |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class Configuration implements ConfigurationInterface |
14
|
|
|
{ |
15
|
|
|
const HTTP_TOO_MANY_REQUESTS = 429; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* {@inheritDoc} |
19
|
|
|
*/ |
20
|
11 |
|
public function getConfigTreeBuilder() |
21
|
|
|
{ |
22
|
11 |
|
$treeBuilder = new TreeBuilder('noxlogic_rate_limit'); |
23
|
|
|
// Keep compatibility with symfony/config < 4.2 |
24
|
11 |
|
if (\method_exists($treeBuilder, 'getRootNode')) { |
25
|
11 |
|
$rootNode = $treeBuilder->getRootNode(); |
26
|
|
|
} else { |
27
|
|
|
$rootNode = $treeBuilder->root('noxlogic_rate_limit'); |
28
|
|
|
} |
29
|
|
|
$rootNode |
30
|
11 |
|
->canBeDisabled() |
31
|
11 |
|
->children() |
32
|
11 |
|
->enumNode('storage_engine') |
33
|
11 |
|
->values(array('redis','memcache','doctrine', 'php_redis', 'simple_cache', 'cache')) |
34
|
11 |
|
->defaultValue('redis') |
35
|
11 |
|
->info('The storage engine where all the rates will be stored') |
36
|
11 |
|
->end() |
37
|
11 |
|
->scalarNode('redis_client') |
38
|
11 |
|
->defaultValue('default_client') |
39
|
11 |
|
->info('The redis client to use for the redis storage engine') |
40
|
11 |
|
->end() |
41
|
11 |
|
->scalarNode('redis_service') |
42
|
11 |
|
->defaultNull() |
43
|
11 |
|
->info('The Redis service to use for the redis storage engine, should be instance of \\Predis\\Client') |
44
|
11 |
|
->example('project.predis') |
45
|
11 |
|
->end() |
46
|
11 |
|
->scalarNode('php_redis_service') |
47
|
11 |
|
->defaultNull() |
48
|
11 |
|
->info('Service id of a php redis, should be an instance of \\Redis') |
49
|
11 |
|
->example('project.redis') |
50
|
11 |
|
->end() |
51
|
11 |
|
->scalarNode('memcache_client') |
52
|
11 |
|
->defaultValue('default') |
53
|
11 |
|
->info('The memcache client to use for the memcache storage engine') |
54
|
11 |
|
->end() |
55
|
11 |
|
->scalarNode('memcache_service') |
56
|
11 |
|
->defaultNull() |
57
|
11 |
|
->info('The Memcached service to use for the memcache storage engine, should be instance of \\Memcached') |
58
|
11 |
|
->example('project.memcached') |
59
|
11 |
|
->end() |
60
|
11 |
|
->scalarNode('doctrine_provider') |
61
|
11 |
|
->defaultNull() |
62
|
11 |
|
->info('The Doctrine Cache provider to use for the doctrine storage engine') |
63
|
11 |
|
->example('my_apc_cache') |
64
|
11 |
|
->end() |
65
|
11 |
|
->scalarNode('doctrine_service') |
66
|
11 |
|
->defaultNull() |
67
|
11 |
|
->info('The Doctrine Cache service to use for the doctrine storage engine') |
68
|
11 |
|
->example('project.my_apc_cache') |
69
|
11 |
|
->end() |
70
|
11 |
|
->scalarNode('simple_cache_service') |
71
|
11 |
|
->defaultNull() |
72
|
11 |
|
->info('Service id of a simple cache, should be an instance of \\Psr\\SimpleCache\\CacheInterface') |
73
|
11 |
|
->example('project.cache') |
74
|
11 |
|
->end() |
75
|
11 |
|
->scalarNode('cache_service') |
76
|
11 |
|
->defaultNull() |
77
|
11 |
|
->info('Service id of a cache, should be an instance of \\Psr\\Cache\\CacheItemPoolInterface') |
78
|
11 |
|
->example('project.cache') |
79
|
11 |
|
->end() |
80
|
11 |
|
->integerNode('rate_response_code') |
81
|
11 |
|
->min(400) |
82
|
11 |
|
->max(499) |
83
|
11 |
|
->defaultValue(static::HTTP_TOO_MANY_REQUESTS) |
84
|
11 |
|
->info('The HTTP status code to return when a client hits the rate limit') |
85
|
11 |
|
->end() |
86
|
11 |
|
->scalarNode('rate_response_exception') |
87
|
11 |
|
->defaultNull() |
88
|
11 |
|
->info('Optional exception class that will be returned when a client hits the rate limit') |
89
|
11 |
|
->validate() |
90
|
11 |
|
->always(function ($item) { |
91
|
3 |
|
if ($item && !is_subclass_of($item, '\Exception')) { |
|
|
|
|
92
|
1 |
|
throw new InvalidConfigurationException(sprintf("'%s' must inherit the \\Exception class", $item)); |
93
|
|
|
} |
94
|
2 |
|
return $item; |
95
|
11 |
|
}) |
96
|
|
|
->end() |
97
|
|
|
->end() |
98
|
|
|
->scalarNode('rate_response_message') |
99
|
|
|
->defaultValue('You exceeded the rate limit') |
100
|
|
|
->info('The HTTP message to return when a client hits the rate limit') |
101
|
|
|
->end() |
102
|
|
|
->booleanNode('display_headers') |
103
|
|
|
->defaultTrue() |
104
|
|
|
->info('Should the ratelimit headers be automatically added to the response?') |
105
|
|
|
->end() |
106
|
|
|
->arrayNode('headers') |
107
|
|
|
->addDefaultsIfNotSet() |
108
|
|
|
->info('What are the different header names to add') |
109
|
|
|
->children() |
110
|
|
|
->scalarNode('limit')->defaultValue('X-RateLimit-Limit')->end() |
111
|
|
|
->scalarNode('remaining')->defaultValue('X-RateLimit-Remaining')->end() |
112
|
|
|
->scalarNode('reset')->defaultValue('X-RateLimit-Reset')->end() |
113
|
|
|
->end() |
114
|
|
|
->end() |
115
|
|
|
->arrayNode('path_limits') |
116
|
|
|
->defaultValue(array()) |
117
|
|
|
->info('Rate limits for paths') |
118
|
|
|
->prototype('array') |
119
|
|
|
->children() |
120
|
|
|
->scalarNode('path') |
121
|
|
|
->isRequired() |
122
|
|
|
->end() |
123
|
|
|
->arrayNode('methods') |
124
|
|
|
->prototype('enum') |
125
|
|
|
->values(array('*', 'GET', 'POST', 'PUT', 'DELETE', 'PATCH')) |
126
|
|
|
->end() |
127
|
|
|
->requiresAtLeastOneElement() |
128
|
|
|
->defaultValue(array('*')) |
129
|
|
|
->end() |
130
|
|
|
->integerNode('limit') |
131
|
|
|
->isRequired() |
132
|
|
|
->min(0) |
133
|
|
|
->end() |
134
|
|
|
->integerNode('period') |
135
|
|
|
->isRequired() |
136
|
|
|
->min(0) |
137
|
|
|
->end() |
138
|
|
|
->end() |
139
|
|
|
->end() |
140
|
|
|
->end() |
141
|
|
|
->booleanNode('fos_oauth_key_listener') |
142
|
|
|
->defaultTrue() |
143
|
|
|
->info('Enabled the FOS OAuthServerBundle listener') |
144
|
|
|
->end() |
145
|
|
|
->end() |
146
|
|
|
; |
147
|
|
|
|
148
|
|
|
return $treeBuilder; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|