Skip to content

Commit db52ebc

Browse files
committed
Merge pull request #2 from loostro/widen-noroute-annotation
Widen noroute annotation
2 parents 076c3af + a623ba4 commit db52ebc

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

Controller/Annotations/NoRoute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* No Route annotation class
1616
* @Annotation
17-
* @Target("METHOD")
17+
* @Target({"METHOD","CLASS"})
1818
*/
1919
class NoRoute extends Route
2020
{

Routing/Loader/Reader/RestActionReader.php

100644100755
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,16 @@ private function isMethodReadable(\ReflectionMethod $method)
235235
if ('_' === substr($method->getName(), 0, 1)) {
236236
return false;
237237
}
238-
239-
// if method has NoRoute annotation - skip
240-
if ($this->readMethodAnnotation($method, 'NoRoute')) {
238+
239+
$hasNoRouteMethod = (bool) $this->readMethodAnnotation($method, 'NoRoute');
240+
$hasNoRouteClass = (bool) $this->readClassAnnotation($method->getDeclaringClass(), 'NoRoute');
241+
242+
$hasNoRoute = $hasNoRoute = $hasNoRouteMethod || $hasNoRouteClass;
243+
// since NoRoute extends Route we need to exclude all the method NoRoute annotations
244+
$hasRoute = (bool) $this->readMethodAnnotation($method, 'Route') && !$hasNoRouteMethod;
245+
246+
// if method has NoRoute annotation and does not have Route annotation - skip
247+
if ($hasNoRoute && !$hasRoute) {
241248
return false;
242249
}
243250

@@ -426,6 +433,23 @@ private function readRouteAnnotation(\ReflectionMethod $reflection)
426433
}
427434
}
428435

436+
/**
437+
* Reads class annotations.
438+
*
439+
* @param ReflectionClass $reflection controller class
440+
* @param string $annotationName annotation name
441+
*
442+
* @return Annotation|null
443+
*/
444+
private function readClassAnnotation(\ReflectionClass $reflection, $annotationName)
445+
{
446+
$annotationClass = "FOS\\RestBundle\\Controller\\Annotations\\$annotationName";
447+
448+
if ($annotation = $this->annotationReader->getClassAnnotation($reflection, $annotationClass)) {
449+
return $annotation;
450+
}
451+
}
452+
429453
/**
430454
* Reads method annotations.
431455
*

Routing/Loader/Reader/RestControllerReader.php

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function read(\ReflectionClass $reflection)
100100
}
101101

102102
/**
103-
* Reads
103+
* Reads class annotations.
104104
*
105105
* @param ReflectionClass $reflection controller class
106106
* @param string $annotationName annotation name

0 commit comments

Comments
 (0)