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
|
10 |
|
public function getConfigTreeBuilder() |
30
|
|
|
{ |
31
|
10 |
|
if (method_exists(TreeBuilder::class, 'getRootNode')) { |
32
|
10 |
|
$treeBuilder = new TreeBuilder('indragunawan_api_rate_limit'); |
33
|
10 |
|
$rootNode = $treeBuilder->getRootNode(); |
34
|
|
|
} else { |
35
|
|
|
$treeBuilder = new TreeBuilder(); |
|
|
|
|
36
|
|
|
$rootNode = $treeBuilder->root('indragunawan_api_rate_limit'); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$rootNode |
40
|
10 |
|
->children() |
41
|
10 |
|
->booleanNode('enabled')->defaultTrue()->end() |
42
|
10 |
|
->scalarNode('cache')->defaultNull()->cannotBeEmpty()->end() |
43
|
10 |
|
->arrayNode('header') |
44
|
10 |
|
->addDefaultsIfNotSet() |
45
|
10 |
|
->children() |
46
|
10 |
|
->booleanNode('display')->defaultTrue()->end() |
47
|
10 |
|
->arrayNode('names') |
48
|
10 |
|
->addDefaultsIfNotSet() |
49
|
10 |
|
->children() |
50
|
10 |
|
->scalarNode('limit')->cannotBeEmpty()->defaultValue('X-RateLimit-Limit')->end() |
51
|
10 |
|
->scalarNode('remaining')->cannotBeEmpty()->defaultValue('X-RateLimit-Remaining')->end() |
52
|
10 |
|
->scalarNode('reset')->cannotBeEmpty()->defaultValue('X-RateLimit-Reset')->end() |
53
|
10 |
|
->end() |
54
|
10 |
|
->end() |
55
|
10 |
|
->end() |
56
|
10 |
|
->end() |
57
|
10 |
|
->arrayNode('throttle') |
58
|
10 |
|
->addDefaultsIfNotSet() |
59
|
10 |
|
->children() |
60
|
10 |
|
->arrayNode('default') |
61
|
10 |
|
->addDefaultsIfNotSet() |
62
|
10 |
|
->children() |
63
|
10 |
|
->integerNode('limit')->min(1)->defaultValue(60)->end() |
64
|
10 |
|
->integerNode('period')->min(1)->defaultValue(60)->end() |
65
|
10 |
|
->end() |
66
|
10 |
|
->end() |
67
|
10 |
|
->arrayNode('roles') |
68
|
10 |
|
->useAttributeAsKey('name') |
69
|
10 |
|
->prototype('array') |
70
|
10 |
|
->children() |
71
|
10 |
|
->integerNode('limit')->isRequired()->min(1)->end() |
72
|
10 |
|
->integerNode('period')->isRequired()->min(1)->end() |
73
|
10 |
|
->end() |
74
|
10 |
|
->end() |
75
|
10 |
|
->end() |
76
|
10 |
|
->enumNode('sort') |
77
|
10 |
|
->values(['first-match', 'rate-limit-asc', 'rate-limit-desc']) |
78
|
10 |
|
->defaultValue('rate-limit-desc') |
79
|
10 |
|
->end() |
80
|
10 |
|
->end() |
81
|
10 |
|
->end() |
82
|
10 |
|
->arrayNode('exception') |
83
|
10 |
|
->addDefaultsIfNotSet() |
84
|
10 |
|
->children() |
85
|
10 |
|
->integerNode('status_code') |
86
|
10 |
|
->defaultValue(Response::HTTP_TOO_MANY_REQUESTS) |
87
|
10 |
|
->validate() |
88
|
10 |
|
->ifNotInArray(array_keys(Response::$statusTexts)) |
89
|
10 |
|
->thenInvalid('Invalid status code "%s"') |
90
|
10 |
|
->end() |
91
|
10 |
|
->end() |
92
|
10 |
|
->scalarNode('message')->cannotBeEmpty()->defaultValue('API rate limit exceeded for %s.')->end() |
93
|
10 |
|
->scalarNode('custom_exception') |
94
|
10 |
|
->cannotBeEmpty() |
95
|
10 |
|
->defaultNull() |
96
|
10 |
|
->validate() |
97
|
10 |
|
->ifTrue(function ($v) { |
98
|
3 |
|
if (!class_exists($v)) { |
99
|
1 |
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
if (!is_subclass_of($v, RateLimitExceededException::class)) { |
103
|
1 |
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return false; |
107
|
10 |
|
}) |
108
|
10 |
|
->thenInvalid('The class %s does not exist or not extend "Indragunawan\ApiRateLimitBundle\Exception\RateLimitExceededException" class.') |
109
|
10 |
|
->end() |
110
|
10 |
|
->end() |
111
|
10 |
|
->end() |
112
|
10 |
|
->end() |
113
|
10 |
|
->end(); |
114
|
|
|
|
115
|
10 |
|
return $treeBuilder; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.