1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
5 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
6 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
7 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
8 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
9 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
10 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
11 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
12 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
13 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
14 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
15 | * |
---|
16 | * This software consists of voluntary contributions made by many individuals |
---|
17 | * and is licensed under the LGPL. For more information, see |
---|
18 | * <http://www.doctrine-project.org>. |
---|
19 | */ |
---|
20 | |
---|
21 | namespace Doctrine\Common\Persistence; |
---|
22 | |
---|
23 | use Doctrine\Common\Persistence\ManagerRegistry; |
---|
24 | |
---|
25 | /** |
---|
26 | * Abstract implementation of the ManagerRegistry contract. |
---|
27 | * |
---|
28 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
---|
29 | * @link www.doctrine-project.org |
---|
30 | * @since 2.2 |
---|
31 | * @author Fabien Potencier <fabien@symfony.com> |
---|
32 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
33 | * @author Lukas Kahwe Smith <smith@pooteeweet.org> |
---|
34 | */ |
---|
35 | abstract class AbstractManagerRegistry implements ManagerRegistry |
---|
36 | { |
---|
37 | private $name; |
---|
38 | private $connections; |
---|
39 | private $managers; |
---|
40 | private $defaultConnection; |
---|
41 | private $defaultManager; |
---|
42 | private $proxyInterfaceName; |
---|
43 | |
---|
44 | public function __construct($name, array $connections, array $managers, $defaultConnection, $defaultManager, $proxyInterfaceName) |
---|
45 | { |
---|
46 | $this->name = $name; |
---|
47 | $this->connections = $connections; |
---|
48 | $this->managers = $managers; |
---|
49 | $this->defaultConnection = $defaultConnection; |
---|
50 | $this->defaultManager = $defaultManager; |
---|
51 | $this->proxyInterfaceName = $proxyInterfaceName; |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Fetches/creates the given services |
---|
56 | * |
---|
57 | * A service in this context is connection or a manager instance |
---|
58 | * |
---|
59 | * @param string $name name of the service |
---|
60 | * @return object instance of the given service |
---|
61 | */ |
---|
62 | abstract protected function getService($name); |
---|
63 | |
---|
64 | /** |
---|
65 | * Resets the given services |
---|
66 | * |
---|
67 | * A service in this context is connection or a manager instance |
---|
68 | * |
---|
69 | * @param string $name name of the service |
---|
70 | * @return void |
---|
71 | */ |
---|
72 | abstract protected function resetService($name); |
---|
73 | |
---|
74 | /** |
---|
75 | * Get the name of the registry |
---|
76 | * |
---|
77 | * @return string |
---|
78 | */ |
---|
79 | public function getName() |
---|
80 | { |
---|
81 | return $this->name; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * @inheritdoc |
---|
86 | */ |
---|
87 | public function getConnection($name = null) |
---|
88 | { |
---|
89 | if (null === $name) { |
---|
90 | $name = $this->defaultConnection; |
---|
91 | } |
---|
92 | |
---|
93 | if (!isset($this->connections[$name])) { |
---|
94 | throw new \InvalidArgumentException(sprintf('Doctrine %s Connection named "%s" does not exist.', $this->name, $name)); |
---|
95 | } |
---|
96 | |
---|
97 | return $this->getService($this->connections[$name]); |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * @inheritdoc |
---|
102 | */ |
---|
103 | public function getConnectionNames() |
---|
104 | { |
---|
105 | return $this->connections; |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * @inheritdoc |
---|
110 | */ |
---|
111 | public function getConnections() |
---|
112 | { |
---|
113 | $connections = array(); |
---|
114 | foreach ($this->connections as $name => $id) { |
---|
115 | $connections[$name] = $this->getService($id); |
---|
116 | } |
---|
117 | |
---|
118 | return $connections; |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * @inheritdoc |
---|
123 | */ |
---|
124 | public function getDefaultConnectionName() |
---|
125 | { |
---|
126 | return $this->defaultConnection; |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * @inheritdoc |
---|
131 | */ |
---|
132 | public function getDefaultManagerName() |
---|
133 | { |
---|
134 | return $this->defaultManager; |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * @inheritdoc |
---|
139 | */ |
---|
140 | public function getManager($name = null) |
---|
141 | { |
---|
142 | if (null === $name) { |
---|
143 | $name = $this->defaultManager; |
---|
144 | } |
---|
145 | |
---|
146 | if (!isset($this->managers[$name])) { |
---|
147 | throw new \InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name)); |
---|
148 | } |
---|
149 | |
---|
150 | return $this->getService($this->managers[$name]); |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * @inheritdoc |
---|
155 | */ |
---|
156 | public function getManagerForClass($class) |
---|
157 | { |
---|
158 | $proxyClass = new \ReflectionClass($class); |
---|
159 | if ($proxyClass->implementsInterface($this->proxyInterfaceName)) { |
---|
160 | $class = $proxyClass->getParentClass()->getName(); |
---|
161 | } |
---|
162 | |
---|
163 | foreach ($this->managers as $id) { |
---|
164 | $manager = $this->getService($id); |
---|
165 | |
---|
166 | if (!$manager->getMetadataFactory()->isTransient($class)) { |
---|
167 | return $manager; |
---|
168 | } |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * @inheritdoc |
---|
174 | */ |
---|
175 | public function getManagerNames() |
---|
176 | { |
---|
177 | return $this->managers; |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * @inheritdoc |
---|
182 | */ |
---|
183 | public function getManagers() |
---|
184 | { |
---|
185 | $dms = array(); |
---|
186 | foreach ($this->managers as $name => $id) { |
---|
187 | $dms[$name] = $this->getService($id); |
---|
188 | } |
---|
189 | |
---|
190 | return $dms; |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * @inheritdoc |
---|
195 | */ |
---|
196 | public function getRepository($persistentObjectName, $persistentManagerName = null) |
---|
197 | { |
---|
198 | return $this->getManager($persistentManagerName)->getRepository($persistentObjectName); |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * @inheritdoc |
---|
203 | */ |
---|
204 | public function resetManager($name = null) |
---|
205 | { |
---|
206 | if (null === $name) { |
---|
207 | $name = $this->defaultManager; |
---|
208 | } |
---|
209 | |
---|
210 | if (!isset($this->managers[$name])) { |
---|
211 | throw new \InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name)); |
---|
212 | } |
---|
213 | |
---|
214 | // force the creation of a new document manager |
---|
215 | // if the current one is closed |
---|
216 | $this->resetService($this->managers[$name]); |
---|
217 | } |
---|
218 | } |
---|