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->field, |
26
|
|
|
$annotation, |
27
|
|
|
$listMapper |
28
|
|
|
); |
29
|
|
|
continue; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($annotation instanceof ListField) { |
33
|
|
|
$this->addField($property->getName(), $annotation, $listMapper); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
foreach ($class->getMethods() as $method) { |
39
|
|
|
if ($annotation = $this->getMethodAnnotation($method, ListField::class)) { |
40
|
|
|
$this->addField($property->getName(), $annotation, $listMapper); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($actions = $this->getListActions($this->getClassAnnotations($class))) { |
45
|
|
|
$listMapper->add('_action', null, [ |
46
|
|
|
'actions' => $actions |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function addField(string $name, ListField $annotation, ListMapper $listMapper): void |
52
|
|
|
{ |
53
|
|
|
if ($annotation->identifier) { |
54
|
|
|
$listMapper->addIdentifier($name, ...$annotation->getSettings()); |
|
|
|
|
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$listMapper->add($name, ...$annotation->getSettings()); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function getListActions(array $annotations): array |
62
|
|
|
{ |
63
|
|
|
$actions = []; |
64
|
|
|
|
65
|
|
|
foreach ($annotations as $annotation) { |
66
|
|
|
if ($annotation instanceof ListAction) { |
67
|
|
|
$actions[$annotation->name] = $annotation->options; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $actions; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|