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