1 | <?php |
||
2 | |||
3 | namespace Dtc\GridBundle\Manager; |
||
4 | |||
5 | use Doctrine\Common\Annotations\Reader; |
||
6 | use Doctrine\ODM\MongoDB\DocumentManager; |
||
7 | use Doctrine\ORM\EntityManager; |
||
8 | use Doctrine\ORM\EntityManagerInterface; |
||
9 | use Dtc\GridBundle\Grid\Source\ColumnSource; |
||
10 | use Dtc\GridBundle\Grid\Source\ColumnSourceInfo; |
||
11 | use Dtc\GridBundle\Grid\Source\DocumentGridSource; |
||
12 | use Dtc\GridBundle\Grid\Source\EntityGridSource; |
||
13 | use Dtc\GridBundle\Grid\Source\GridSourceInterface; |
||
14 | |||
15 | class GridSourceManager |
||
16 | { |
||
17 | protected $sourcesByClass; |
||
18 | protected $sourcesByName; |
||
19 | |||
20 | protected $reader; |
||
21 | |||
22 | protected $registry; |
||
23 | |||
24 | protected $mongodbRegistry; |
||
25 | |||
26 | protected $customManagerMappings; |
||
27 | |||
28 | protected $extraGridSources; |
||
29 | |||
30 | protected $columnSource; |
||
31 | /** |
||
32 | * @var array|null Null means all entities allowed, empty array means no entities allowed |
||
33 | */ |
||
34 | protected $reflectionAllowedEntities; |
||
35 | |||
36 | /** |
||
37 | * GridSourceManager constructor. |
||
38 | */ |
||
39 | public function __construct(ColumnSource $columnSource) |
||
40 | { |
||
41 | $this->columnSource = $columnSource; |
||
42 | $this->reflectionAllowedEntities = []; |
||
43 | $this->sources = []; |
||
44 | } |
||
45 | |||
46 | public function setReader(Reader $reader) |
||
47 | { |
||
48 | $this->reader = $reader; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param array|string $allowedEntities Array of allowed entities or string '*' to allow all entities. Use empty array to specify no entities allowed for reflection. |
||
53 | */ |
||
54 | public function setReflectionAllowedEntities($allowedEntities) |
||
55 | { |
||
56 | $this->reflectionAllowedEntities = is_array($allowedEntities) ? array_flip($allowedEntities) : ('*' === $allowedEntities ? null : []); |
||
57 | } |
||
58 | |||
59 | public function setRegistry($registry) |
||
60 | { |
||
61 | $this->registry = $registry; |
||
62 | } |
||
63 | |||
64 | public function setMongodbRegistry($registry) |
||
65 | { |
||
66 | $this->mongodbRegistry = $registry; |
||
67 | } |
||
68 | |||
69 | public function add($id, GridSourceInterface $gridSource) |
||
70 | { |
||
71 | $this->extraGridSources[$id] = $gridSource; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param $entityOrDocument |
||
76 | *z |
||
77 | * |
||
78 | * @return DocumentGridSource|EntityGridSource|null |
||
79 | * |
||
80 | * @throws \Exception |
||
81 | */ |
||
82 | protected function getGridSource($manager, $entityOrDocument) |
||
83 | { |
||
84 | $repository = $manager->getRepository($entityOrDocument); |
||
85 | if ($repository) { |
||
86 | $className = $repository->getClassName(); |
||
87 | $classMetadata = $manager->getClassMetadata($className); |
||
88 | $params = [$manager, $entityOrDocument, null === $this->reflectionAllowedEntities || isset($this->reflectionAllowedEntities[$entityOrDocument])]; |
||
89 | if ($this->reader) { |
||
90 | $params[] = $this->reader; |
||
91 | } |
||
92 | $columnSourceInfo = call_user_func_array([$this->columnSource, 'getColumnSourceInfo'], $params); |
||
93 | $name = $classMetadata->getName(); |
||
94 | if ($columnSourceInfo) { |
||
95 | return $this->getGridSourceFromColumnSourceInfo($manager, $className, $name, $columnSourceInfo); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | return null; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param $manager |
||
104 | * @param $className |
||
105 | * @param $name |
||
106 | * |
||
107 | * @return DocumentGridSource|EntityGridSource |
||
108 | * |
||
109 | * @throws \Exception |
||
110 | */ |
||
111 | private function getGridSourceFromColumnSourceInfo($manager, $className, $name, ColumnSourceInfo $columnSourceInfo) |
||
112 | { |
||
113 | if ($manager instanceof EntityManagerInterface) { |
||
114 | $gridSource = new EntityGridSource($manager, $name); |
||
115 | } else { |
||
116 | if (!$manager instanceof DocumentManager) { |
||
117 | throw new \Exception('Unknown ObjectManager type: '.get_class($manager)); |
||
118 | } |
||
119 | $gridSource = new DocumentGridSource($manager, $name); |
||
120 | } |
||
121 | $gridSource->setIdColumn($columnSourceInfo->idColumn); |
||
122 | $gridSource->setColumns($columnSourceInfo->columns); |
||
123 | $this->sourcesByName[$name] = $gridSource; |
||
124 | $this->sourcesByClass[$className] = $gridSource; |
||
125 | $gridSource->setId($className); |
||
126 | $gridSource->setDefaultSort($columnSourceInfo->sort); |
||
127 | |||
128 | return $gridSource; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Get a gridsource. |
||
133 | * |
||
134 | * @param string $id Entity or Document |
||
135 | * @param EntityManager|DocumentManager|null $manager (optional) Entity or Document manager to use (overrides default) |
||
136 | * |
||
137 | * @return GridSourceInterface|null |
||
138 | * |
||
139 | * @throws \Exception |
||
140 | */ |
||
141 | public function get($entityOrDocumentNameOrId) |
||
142 | { |
||
143 | // @Support legacy method of adding/removing grid sources |
||
144 | if (isset($this->extraGridSources[$entityOrDocumentNameOrId])) { |
||
145 | return $this->extraGridSources[$entityOrDocumentNameOrId]; |
||
146 | } |
||
147 | |||
148 | if (isset($this->sourcesByClass[$entityOrDocumentNameOrId])) { |
||
149 | return $this->sourcesByClass[$entityOrDocumentNameOrId]; |
||
150 | } |
||
151 | |||
152 | if (isset($this->sourcesByName[$entityOrDocumentNameOrId])) { |
||
153 | return $this->sourcesByName[$entityOrDocumentNameOrId]; |
||
154 | } |
||
155 | |||
156 | try { |
||
157 | if ($this->registry && ($manager = $this->registry->getManagerForClass($entityOrDocumentNameOrId)) && |
||
158 | $gridSource = $this->getGridSource($manager, $entityOrDocumentNameOrId)) { |
||
159 | return $gridSource; |
||
160 | } |
||
161 | } catch (\ReflectionException $exception) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
![]() |
|||
162 | } |
||
163 | |||
164 | if ($this->mongodbRegistry && ($manager = $this->mongodbRegistry->getManagerForClass($entityOrDocumentNameOrId)) && |
||
165 | $gridSource = $this->getGridSource($manager, $entityOrDocumentNameOrId)) { |
||
166 | return $gridSource; |
||
167 | } |
||
168 | throw new \Exception("Can't find grid source for $entityOrDocumentNameOrId"); |
||
169 | } |
||
170 | |||
171 | public function all() |
||
172 | { |
||
173 | return isset($this->sourcesByName) ? array_values($this->sourcesByName) : []; |
||
174 | } |
||
175 | |||
176 | public function setEntityManager(EntityManager $entityManager) |
||
177 | { |
||
178 | $this->entityManager = $entityManager; |
||
179 | } |
||
180 | |||
181 | public function setDocumentManager(DocumentManager $documentManager) |
||
182 | { |
||
183 | $this->documentManager = $documentManager; |
||
184 | } |
||
185 | } |
||
186 |