1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\GridBundle\Manager; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Reader; |
6
|
|
|
use Doctrine\Common\Persistence\AbstractManagerRegistry; |
7
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
8
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
9
|
|
|
use Doctrine\ORM\EntityManager; |
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
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
|
|
|
protected $cacheDir; |
22
|
|
|
protected $debug; |
23
|
|
|
|
24
|
|
|
/** @var AbstractManagerRegistry */ |
25
|
|
|
protected $registry; |
26
|
|
|
|
27
|
|
|
/** @var AbstractManagerRegistry */ |
28
|
|
|
protected $mongodbRegistry; |
29
|
|
|
|
30
|
|
|
protected $customManagerMappings; |
31
|
|
|
|
32
|
|
|
protected $extraGridSources; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* GridSourceManager constructor. |
36
|
|
|
* |
37
|
|
|
* @param string $cacheDir |
38
|
|
|
* @param bool $debug |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Reader $reader, $cacheDir, $debug = false) |
41
|
|
|
{ |
42
|
|
|
$this->cacheDir = $cacheDir; |
43
|
|
|
$this->reader = $reader; |
44
|
|
|
$this->debug = $debug; |
45
|
|
|
$this->sources = array(); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setRegistry(AbstractManagerRegistry $registry) |
49
|
|
|
{ |
50
|
|
|
$this->registry = $registry; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setMongodbRegistry(AbstractManagerRegistry $registry) |
54
|
|
|
{ |
55
|
|
|
$this->mongodbRegistry = $registry; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
protected function getCacheDir() |
62
|
|
|
{ |
63
|
|
|
return $this->cacheDir; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
protected function getDebug() |
70
|
|
|
{ |
71
|
|
|
return $this->debug; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function add($id, GridSourceInterface $gridSource) |
75
|
|
|
{ |
76
|
|
|
$this->extraGridSources[$id] = $gridSource; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param ObjectManager $manager |
81
|
|
|
* @param $entityOrDocument |
82
|
|
|
* |
83
|
|
|
* @return DocumentGridSource|EntityGridSource|null |
84
|
|
|
*/ |
85
|
|
|
protected function getGridSource($manager, $entityOrDocument) |
86
|
|
|
{ |
87
|
|
|
$repository = $manager->getRepository($entityOrDocument); |
88
|
|
|
if ($repository) { |
89
|
|
|
$className = $repository->getClassName(); |
90
|
|
|
$classMetadata = $manager->getClassMetadata($className); |
91
|
|
|
$name = $classMetadata->getName(); |
92
|
|
|
$reflectionClass = $classMetadata->getReflectionClass(); |
93
|
|
|
$annotation = $this->reader->getClassAnnotation($reflectionClass, 'Dtc\GridBundle\Annotation\Grid'); |
94
|
|
|
if (!$annotation) { |
95
|
|
|
throw new \Exception("GridSource requested for '$entityOrDocument' but no Grid annotation found"); |
96
|
|
|
} |
97
|
|
|
if ($manager instanceof EntityManagerInterface) { |
98
|
|
|
$gridSource = new EntityGridSource($manager, $name); |
99
|
|
|
} else { |
100
|
|
|
$gridSource = new DocumentGridSource($manager, $name); |
101
|
|
|
} |
102
|
|
|
$gridSource->setAnnotationReader($this->reader); |
103
|
|
|
$gridSource->setCacheDir($this->cacheDir); |
104
|
|
|
|
105
|
|
|
$gridSource->setDebug($this->debug); |
106
|
|
|
$gridSource->autoDiscoverColumns(); |
107
|
|
|
$this->sourcesByName[$name] = $gridSource; |
108
|
|
|
$this->sourcesByClass[$className] = $gridSource; |
109
|
|
|
$gridSource->setId($className); |
110
|
|
|
|
111
|
|
|
return $gridSource; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get a gridsource. |
119
|
|
|
* |
120
|
|
|
* @param string $id Entity or Document |
121
|
|
|
* @param EntityManager|DocumentManager|null $manager (optional) Entity or Document manager to use (overrides default) |
122
|
|
|
* |
123
|
|
|
* @return GridSourceInterface|null |
124
|
|
|
* |
125
|
|
|
* @throws \Exception |
126
|
|
|
*/ |
127
|
|
|
public function get($entityOrDocumentNameOrId) |
128
|
|
|
{ |
129
|
|
|
// @Support legacy method of adding/removing grid sources |
130
|
|
|
if (isset($this->extraGridSources[$entityOrDocumentNameOrId])) { |
131
|
|
|
return $this->extraGridSources[$entityOrDocumentNameOrId]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (isset($this->sourcesByClass[$entityOrDocumentNameOrId])) { |
135
|
|
|
return $this->sourcesByClass[$entityOrDocumentNameOrId]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (isset($this->sourcesByName[$entityOrDocumentNameOrId])) { |
139
|
|
|
return $this->sourcesByName[$entityOrDocumentNameOrId]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
try { |
143
|
|
View Code Duplication |
if ($this->registry && ($manager = $this->registry->getManagerForClass($entityOrDocumentNameOrId)) && |
|
|
|
|
144
|
|
|
$gridSource = $this->getGridSource($manager, $entityOrDocumentNameOrId)) { |
145
|
|
|
return $gridSource; |
146
|
|
|
} |
147
|
|
|
} catch (\ReflectionException $exception) { |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
View Code Duplication |
if ($this->mongodbRegistry && ($manager = $this->mongodbRegistry->getManagerForClass($entityOrDocumentNameOrId)) && |
|
|
|
|
151
|
|
|
$gridSource = $this->getGridSource($manager, $entityOrDocumentNameOrId)) { |
152
|
|
|
return $gridSource; |
153
|
|
|
} |
154
|
|
|
throw new \Exception("Can't find grid source for $entityOrDocumentNameOrId"); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function all() |
158
|
|
|
{ |
159
|
|
|
return isset($this->sourcesByName) ? array_values($this->sourcesByName) : []; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function setEntityManager(EntityManager $entityManager) |
163
|
|
|
{ |
164
|
|
|
$this->entityManager = $entityManager; |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function setDocumentManager(DocumentManager $documentManager) |
168
|
|
|
{ |
169
|
|
|
$this->documentManager = $documentManager; |
|
|
|
|
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|