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