|
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\ListAssociationField; |
|
9
|
|
|
use KunicMarko\SonataAnnotationBundle\Annotation\ListField; |
|
10
|
|
|
use Sonata\AdminBundle\Datagrid\ListMapper; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Marko Kunic <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class ListReader |
|
16
|
|
|
{ |
|
17
|
|
|
use AnnotationReaderTrait; |
|
18
|
|
|
|
|
19
|
|
|
public function configureFields(\ReflectionClass $class, ListMapper $listMapper): void |
|
20
|
|
|
{ |
|
21
|
|
|
foreach ($class->getProperties() as $property) { |
|
22
|
|
|
foreach ($this->getPropertyAnnotations($property) as $annotation) { |
|
23
|
|
|
if ($annotation instanceof ListAssociationField) { |
|
24
|
|
|
$this->addField( |
|
25
|
|
|
$property->getName() . '.' . $annotation->getField(), |
|
26
|
|
|
$annotation, |
|
27
|
|
|
$listMapper |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
continue; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if ($annotation instanceof ListField) { |
|
34
|
|
|
$this->addField($property->getName(), $annotation, $listMapper); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
foreach ($class->getMethods() as $method) { |
|
40
|
|
|
if ($annotation = $this->getMethodAnnotation($method, ListField::class)) { |
|
41
|
|
|
$this->addField($method->getName(), $annotation, $listMapper); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if ($actions = $this->getListActions($this->getClassAnnotations($class))) { |
|
46
|
|
|
$listMapper->add('_action', null, [ |
|
47
|
|
|
'actions' => $actions |
|
48
|
|
|
]); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function addField(string $name, ListField $annotation, ListMapper $listMapper): void |
|
53
|
|
|
{ |
|
54
|
|
|
if ($annotation->identifier) { |
|
55
|
|
|
$listMapper->addIdentifier($name, ...$annotation->getSettings()); |
|
|
|
|
|
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$listMapper->add($name, ...$annotation->getSettings()); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function getListActions(array $annotations): array |
|
63
|
|
|
{ |
|
64
|
|
|
$actions = []; |
|
65
|
|
|
|
|
66
|
|
|
foreach ($annotations as $annotation) { |
|
67
|
|
|
if ($annotation instanceof ListAction) { |
|
68
|
|
|
$actions[$annotation->getName()] = $annotation->options; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $actions; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|