1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace KunicMarko\SonataAnnotationBundle\Reader; |
6
|
|
|
|
7
|
|
|
use KunicMarko\SonataAnnotationBundle\Annotation\ListAction; |
8
|
|
|
use KunicMarko\SonataAnnotationBundle\Annotation\ListField; |
9
|
|
|
use Sonata\AdminBundle\Mapper\BaseMapper; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Marko Kunic <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class ListReader extends AbstractReader |
15
|
|
|
{ |
16
|
|
|
public function configureFields(\ReflectionClass $class, BaseMapper $baseMapper): void |
17
|
|
|
{ |
18
|
|
|
parent::configureFields($class, $baseMapper); |
19
|
|
|
|
20
|
|
|
if ($actions = $this->getListActions($this->getClassAnnotations($class))) { |
21
|
|
|
$baseMapper->add('_action', null, [ |
|
|
|
|
22
|
|
|
'actions' => $actions |
23
|
|
|
]); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private function getListActions(array $annotations): array |
28
|
|
|
{ |
29
|
|
|
$actions = []; |
30
|
|
|
|
31
|
|
|
foreach($annotations as $annotation) { |
32
|
|
|
if (!$this->isSupported($annotation)) { |
33
|
|
|
continue; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$actions[$annotation->name] = $annotation->options; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $actions; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function isSupported($annotation): bool |
43
|
|
|
{ |
44
|
|
|
return $annotation instanceof ListAction; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function findProperties(\ReflectionClass $class): array |
48
|
|
|
{ |
49
|
|
|
$fields = parent::findProperties($class); |
50
|
|
|
|
51
|
|
|
foreach ($class->getMethods() as $method) { |
52
|
|
|
if ($annotation = $this->getMethodAnnotation($method, $this->getAnnotation())) { |
53
|
|
|
$fields[$method->getName()] = $annotation; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $fields; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function addPropertiesToMapper(array $properties, BaseMapper $baseMapper): void |
61
|
|
|
{ |
62
|
|
|
foreach ($properties as $name => $annotation) { |
63
|
|
|
$this->addField($name, $annotation, $baseMapper); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function addField(string $name, ListField $annotation, BaseMapper $baseMapper): void |
68
|
|
|
{ |
69
|
|
|
if ($annotation->identifier) { |
70
|
|
|
$baseMapper->addIdentifier($name, ...$annotation->getSettings()); |
|
|
|
|
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$baseMapper->add($name, ...$annotation->getSettings()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function getAnnotation(): string |
78
|
|
|
{ |
79
|
|
|
return ListField::class; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|