handle optional annotation parameters

Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
This commit is contained in:
Bjoern Schiessle 2017-01-17 14:36:44 +01:00
parent 29a0a23918
commit 32e0ec3e58
No known key found for this signature in database
GPG Key ID: 2378A753E2BF04F6
2 changed files with 20 additions and 3 deletions

View File

@ -192,7 +192,7 @@ class SecurityMiddleware extends Middleware {
}
if($this->reflector->hasAnnotation('BruteForceProtection')) {
$action = $this->request->getRequestUri();
$action = $this->reflector->getAnnotationParameter('BruteForceProtection');
$this->throttler->sleepDelay($this->request->getRemoteAddress(), $action);
$this->throttler->registerAttempt($action, $this->request->getRemoteAddress());
}

View File

@ -56,7 +56,9 @@ class ControllerMethodReflector implements IControllerMethodReflector{
// extract everything prefixed by @ and first letter uppercase
preg_match_all('/^\h+\*\h+@(?P<annotation>[A-Z]\w+)(\h+(?P<parameter>\w+))?$/m', $docs, $matches);
$this->annotations = $matches[1];
foreach($matches['annotation'] as $key => $annontation) {
$this->annotations[$annontation] = $matches['parameter'][$key];
}
// extract type parameter information
preg_match_all('/@param\h+(?P<type>\w+)\h+\$(?P<var>\w+)/', $docs, $matches);
@ -112,7 +114,22 @@ class ControllerMethodReflector implements IControllerMethodReflector{
* @return bool true if the annotation is found
*/
public function hasAnnotation($name){
return in_array($name, $this->annotations);
return array_key_exists($name, $this->annotations);
}
/**
* Get optional annotation parameter
* @param string $name the name of the annotation
* @return string
*/
public function getAnnotationParameter($name){
$parameter = '';
if($this->hasAnnotation($name)) {
$parameter = $this->annotations[$name];
}
return $parameter;
}