|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace KunicMarko\SonataAnnotationBundle\Reader; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use KunicMarko\SonataAnnotationBundle\Annotation\AbstractAction; |
|
10
|
|
|
use KunicMarko\SonataAnnotationBundle\Exception\MissingAnnotationArgumentException; |
|
11
|
|
|
use ReflectionClass; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Action reader main class. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Marko Kunic <[email protected]> |
|
17
|
|
|
* @author Mathieu Wambre <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class AbstractActionReader extends AbstractReader |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Associated annotation class. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string|null |
|
26
|
|
|
*/ |
|
27
|
|
|
protected ?string $annotationClass; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param Reader $annotationReader Doctrine annotation reader. |
|
31
|
|
|
* @param ?string $annotationClass Associated annotation class. |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct( |
|
34
|
|
|
Reader $annotationReader, |
|
35
|
|
|
?string $annotationClass = null |
|
36
|
|
|
) { |
|
37
|
|
|
parent::__construct($annotationReader); |
|
38
|
|
|
$this->annotationClass = $annotationClass; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get action list. |
|
43
|
|
|
* |
|
44
|
|
|
* @param ReflectionClass $class Entity class. |
|
45
|
|
|
* @param array $actions Action list. |
|
46
|
|
|
* |
|
47
|
|
|
* @return array |
|
48
|
|
|
* @throws MissingAnnotationArgumentException if a template attribute is |
|
49
|
|
|
* missing. |
|
50
|
|
|
* @throws Exception if an appropriate source of randomness cannot be |
|
51
|
|
|
* found. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getActions(ReflectionClass $class, array $actions): array |
|
54
|
|
|
{ |
|
55
|
|
|
foreach ($this->getClassAnnotations($class) as $annotation) { |
|
56
|
|
|
if ($this->isSupported($annotation)) { |
|
57
|
|
|
if (!isset($annotation->template)) { |
|
58
|
|
|
throw new MissingAnnotationArgumentException( |
|
59
|
|
|
$annotation, |
|
60
|
|
|
'template', |
|
61
|
|
|
$class |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** @var AbstractAction $annotation */ |
|
66
|
|
|
$actions[random_int( |
|
67
|
|
|
-99999, |
|
68
|
|
|
99999 |
|
69
|
|
|
)]['template'] = $annotation->template; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $actions; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Is the given annotation supported. |
|
78
|
|
|
* |
|
79
|
|
|
* @param object $annotation Annotation. |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function isSupported(object $annotation): bool |
|
84
|
|
|
{ |
|
85
|
|
|
return $annotation instanceof $this->annotationClass; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|