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