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 Indragunawan\ApiRateLimitBundle\Exception\RateLimitExceededException; |
15
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
16
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The configuration of the bundle. |
21
|
|
|
* |
22
|
|
|
* @author Indra Gunawan <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class Configuration implements ConfigurationInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
12 |
|
public function getConfigTreeBuilder() |
30
|
|
|
{ |
31
|
12 |
|
$treeBuilder = new TreeBuilder(); |
32
|
12 |
|
$rootNode = $treeBuilder->root('indragunawan_api_rate_limit'); |
33
|
|
|
|
34
|
|
|
$rootNode |
35
|
12 |
|
->children() |
36
|
12 |
|
->booleanNode('enabled')->defaultTrue()->end() |
37
|
12 |
|
->scalarNode('storage')->defaultNull()->cannotBeEmpty()->end() |
38
|
12 |
|
->scalarNode('cache')->defaultNull()->cannotBeEmpty()->end() |
39
|
12 |
|
->arrayNode('header') |
40
|
12 |
|
->addDefaultsIfNotSet() |
41
|
12 |
|
->children() |
42
|
12 |
|
->booleanNode('display')->defaultTrue()->end() |
43
|
12 |
|
->arrayNode('names') |
44
|
12 |
|
->addDefaultsIfNotSet() |
45
|
12 |
|
->children() |
46
|
12 |
|
->scalarNode('limit')->cannotBeEmpty()->defaultValue('X-RateLimit-Limit')->end() |
47
|
12 |
|
->scalarNode('remaining')->cannotBeEmpty()->defaultValue('X-RateLimit-Remaining')->end() |
48
|
12 |
|
->scalarNode('reset')->cannotBeEmpty()->defaultValue('X-RateLimit-Reset')->end() |
49
|
12 |
|
->end() |
50
|
12 |
|
->end() |
51
|
12 |
|
->end() |
52
|
12 |
|
->end() |
53
|
12 |
|
->arrayNode('throttle') |
54
|
12 |
|
->beforeNormalization() |
55
|
|
|
->ifTrue(function ($v) { return is_array($v) && (isset($v['limit']) || isset($v['period'])); }) |
56
|
|
|
->then(function ($v) { |
57
|
1 |
|
$v['default'] = []; |
58
|
1 |
|
if (isset($v['limit'])) { |
59
|
1 |
|
@trigger_error('The indragunawan_api_rate_limit.throttle.limit configuration key is deprecated since version v0.2.0 and will be removed in v0.3.0. Use the indragunawan_api_rate_limit.throttle.default.limit configuration key instead.', E_USER_DEPRECATED); |
|
|
|
|
60
|
|
|
|
61
|
1 |
|
$v['default']['limit'] = $v['limit']; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
if (isset($v['period'])) { |
65
|
1 |
|
@trigger_error('The indragunawan_api_rate_limit.throttle.period configuration key is deprecated since version v0.2.0 and will be removed in v0.3.0. Use the indragunawan_api_rate_limit.throttle.default.period configuration key instead.', E_USER_DEPRECATED); |
|
|
|
|
66
|
|
|
|
67
|
1 |
|
$v['default']['period'] = $v['period']; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return $v; |
71
|
12 |
|
}) |
72
|
12 |
|
->end() |
73
|
12 |
|
->addDefaultsIfNotSet() |
74
|
12 |
|
->children() |
75
|
12 |
|
->integerNode('limit')->min(1)->defaultValue(60)->end() |
76
|
12 |
|
->integerNode('period')->min(1)->defaultValue(60)->end() |
77
|
12 |
|
->arrayNode('default') |
78
|
12 |
|
->addDefaultsIfNotSet() |
79
|
12 |
|
->children() |
80
|
12 |
|
->integerNode('limit')->min(1)->defaultValue(60)->end() |
81
|
12 |
|
->integerNode('period')->min(1)->defaultValue(60)->end() |
82
|
12 |
|
->end() |
83
|
12 |
|
->end() |
84
|
12 |
|
->arrayNode('roles') |
85
|
12 |
|
->useAttributeAsKey('name') |
86
|
12 |
|
->prototype('array') |
87
|
12 |
|
->children() |
88
|
12 |
|
->integerNode('limit')->isRequired()->min(1)->end() |
89
|
12 |
|
->integerNode('period')->isRequired()->min(1)->end() |
90
|
12 |
|
->end() |
91
|
12 |
|
->end() |
92
|
12 |
|
->end() |
93
|
12 |
|
->enumNode('sort') |
94
|
12 |
|
->values(['first-match', 'rate-limit-asc', 'rate-limit-desc']) |
95
|
12 |
|
->defaultValue('rate-limit-desc') |
96
|
12 |
|
->end() |
97
|
12 |
|
->end() |
98
|
12 |
|
->end() |
99
|
12 |
|
->arrayNode('exception') |
100
|
12 |
|
->addDefaultsIfNotSet() |
101
|
12 |
|
->children() |
102
|
12 |
|
->integerNode('status_code') |
103
|
12 |
|
->defaultValue(Response::HTTP_TOO_MANY_REQUESTS) |
104
|
12 |
|
->validate() |
105
|
12 |
|
->ifNotInArray(array_keys(Response::$statusTexts)) |
106
|
12 |
|
->thenInvalid('Invalid status code "%s"') |
107
|
12 |
|
->end() |
108
|
12 |
|
->end() |
109
|
12 |
|
->scalarNode('message')->cannotBeEmpty()->defaultValue('API rate limit exceeded for %s.')->end() |
110
|
12 |
|
->scalarNode('custom_exception') |
111
|
12 |
|
->cannotBeEmpty() |
112
|
12 |
|
->defaultNull() |
113
|
12 |
|
->validate() |
114
|
12 |
|
->ifTrue(function ($v) { |
115
|
3 |
|
if (!class_exists($v)) { |
116
|
1 |
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
if (!is_subclass_of($v, RateLimitExceededException::class)) { |
|
|
|
|
120
|
1 |
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
return false; |
124
|
12 |
|
}) |
125
|
12 |
|
->thenInvalid('The class %s does not exist or not extend "Indragunawan\ApiRateLimitBundle\Exception\RateLimitExceededException" class.') |
126
|
12 |
|
->end() |
127
|
12 |
|
->end() |
128
|
12 |
|
->end() |
129
|
12 |
|
->end() |
130
|
12 |
|
->end(); |
131
|
|
|
|
132
|
12 |
|
return $treeBuilder; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: