|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mouf\Mvc\Splash\Services; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Utility class for filters. |
|
7
|
|
|
*/ |
|
8
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
9
|
|
|
use Mouf\Mvc\Splash\Controllers\Controller; |
|
10
|
|
|
use ReflectionMethod; |
|
11
|
|
|
|
|
12
|
|
|
class FilterUtils |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Returns a list of filters instances, order by priority (higher priority first). |
|
16
|
|
|
* Note: a filter is an annotation that is also a middleware. |
|
17
|
|
|
* |
|
18
|
|
|
* The middleware must have a __invoke method with signature: |
|
19
|
|
|
* |
|
20
|
|
|
* __invoke(ServerRequestInterface $request, $next, ContainerInterface $container) |
|
21
|
|
|
* |
|
22
|
|
|
* @param ReflectionMethod $refMethod the reference method extended object. |
|
23
|
|
|
* @param object $controller the controller the annotation was in. |
|
24
|
|
|
* @param AnnotationReader $reader |
|
25
|
|
|
* |
|
26
|
|
|
* @return array Array of filter instances sorted by priority. |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function getFilters(ReflectionMethod $refMethod, $controller, AnnotationReader $reader) : array |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
$filterArray = array(); |
|
31
|
|
|
|
|
32
|
|
|
$refClass = $refMethod->getDeclaringClass(); |
|
33
|
|
|
|
|
34
|
|
|
$parentsArray = array(); |
|
35
|
|
|
$parentClass = $refClass; |
|
36
|
|
|
while ($parentClass !== false) { |
|
37
|
|
|
$parentsArray[] = $parentClass; |
|
38
|
|
|
$parentClass = $parentClass->getParentClass(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// Start with the most parent class and goes to the target class: |
|
42
|
|
|
for ($i = count($parentsArray) - 1; $i >= 0; --$i) { |
|
43
|
|
|
$class = $parentsArray[$i]; |
|
44
|
|
|
/* @var $class ReflectionClass */ |
|
45
|
|
|
$annotations = $reader->getClassAnnotations($class); |
|
46
|
|
|
|
|
47
|
|
|
foreach ($annotations as $annotation) { |
|
48
|
|
|
if (is_callable($annotation)) { |
|
49
|
|
|
$filterArray[] = $annotation; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Continue with the method (and eventually override class parameters) |
|
55
|
|
|
$annotations = $reader->getMethodAnnotations($refMethod); |
|
56
|
|
|
|
|
57
|
|
|
foreach ($annotations as $annotation) { |
|
58
|
|
|
if (is_callable($annotation)) { |
|
59
|
|
|
$filterArray[] = $annotation; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $filterArray; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.