1 | <?php |
||
12 | final class SimpleRegistry implements EntityManagerRegistry |
||
13 | { |
||
14 | const DEFAULT_CONNECTION_NAME = 'default'; |
||
15 | const DEFAULT_MANAGER_NAME = 'default'; |
||
16 | |||
17 | /** |
||
18 | * @var Connection[] |
||
19 | */ |
||
20 | private $connections = []; |
||
21 | /** |
||
22 | * @var EntityManagerInterface[] |
||
23 | */ |
||
24 | private $managers = []; |
||
25 | /** |
||
26 | * Custom Entity=>Manager config |
||
27 | * @var array |
||
28 | */ |
||
29 | private $entityManagerMapping = []; |
||
30 | |||
31 | 134 | public function __construct( |
|
40 | |||
41 | /** |
||
42 | * @inheritdoc |
||
43 | */ |
||
44 | 2 | public function getDefaultConnectionName(): string |
|
45 | { |
||
46 | 2 | return self::DEFAULT_CONNECTION_NAME; |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * @inheritdoc |
||
51 | */ |
||
52 | 2 | public function getConnection($name = null): Connection |
|
53 | { |
||
54 | 2 | if ($name === null) { |
|
55 | 2 | $name = $this->getDefaultConnectionName(); |
|
56 | } |
||
57 | |||
58 | 2 | if (isset($this->connections[$name])) { |
|
59 | 2 | return $this->connections[$name]; |
|
60 | } |
||
61 | |||
62 | throw new \InvalidArgumentException(sprintf('Could not find Doctrine connection with name %s', $name)); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | * @return Connection[] |
||
68 | */ |
||
69 | 2 | public function getConnections(): array |
|
70 | { |
||
71 | 2 | return $this->connections; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @inheritdoc |
||
76 | */ |
||
77 | public function getConnectionNames(): array |
||
81 | |||
82 | /** |
||
83 | * @inheritdoc |
||
84 | */ |
||
85 | 136 | public function getDefaultManagerName(): string |
|
89 | |||
90 | /** |
||
91 | * @inheritdoc |
||
92 | * @throws \Doctrine\ORM\ORMException |
||
93 | */ |
||
94 | 138 | public function getManager($name = null): EntityManagerInterface |
|
95 | { |
||
96 | 138 | if ($name === null) { |
|
97 | 2 | $name = $this->getDefaultManagerName(); |
|
98 | } |
||
99 | |||
100 | 138 | if (isset($this->managers[$name])) { |
|
101 | 136 | $manager = $this->managers[$name]; |
|
102 | 136 | if (!$manager->isOpen()) { |
|
103 | 2 | return $this->resetManager($name); |
|
104 | } |
||
105 | 136 | return $manager; |
|
106 | } |
||
107 | |||
108 | 2 | throw new \InvalidArgumentException(sprintf('Could not find Doctrine manager with name %s', $name)); |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | * @return EntityManagerInterface[] |
||
114 | */ |
||
115 | 2 | public function getManagers(): array |
|
116 | { |
||
117 | 2 | return $this->managers; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * @inheritdoc |
||
122 | * @throws \Doctrine\ORM\ORMException |
||
123 | */ |
||
124 | 2 | public function resetManager($name = null): EntityManagerInterface |
|
125 | { |
||
126 | 2 | if ($name === null) { |
|
127 | $name = $this->getDefaultManagerName(); |
||
128 | } |
||
129 | |||
130 | 2 | if (!isset($this->managers[$name])) { |
|
131 | throw new \InvalidArgumentException(sprintf('Could not find Doctrine manager with name %s', $name)); |
||
132 | } |
||
133 | |||
134 | 2 | $manager = $this->managers[$name]; |
|
135 | 2 | $this->managers[$name] = EntityManager::create( |
|
136 | 2 | $manager->getConnection(), |
|
137 | 2 | $manager->getConfiguration(), |
|
138 | 2 | $manager->getEventManager() |
|
139 | ); |
||
140 | |||
141 | 2 | return $this->getManager($name); |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * @inheritdoc |
||
146 | * @unsupported |
||
147 | */ |
||
148 | public function getAliasNamespace($alias): string |
||
152 | |||
153 | /** |
||
154 | * @inheritdoc |
||
155 | */ |
||
156 | public function getManagerNames(): array |
||
160 | |||
161 | /** |
||
162 | * @inheritdoc |
||
163 | * @throws \Doctrine\ORM\ORMException |
||
164 | */ |
||
165 | 134 | public function getRepository($persistentObject, $persistentManagerName = null): ObjectRepository |
|
174 | |||
175 | /** |
||
176 | * @inheritdoc |
||
177 | * @throws \Doctrine\ORM\ORMException |
||
178 | */ |
||
179 | 134 | public function getManagerForClass($class): EntityManagerInterface |
|
185 | } |
||
186 |