Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / lib / Doctrine / Common / Persistence / AbstractManagerRegistry.php
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 MIT license. 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     /**
38      * @var string
39      */
40     private $name;
41
42     /**
43      * @var array
44      */
45     private $connections;
46
47     /**
48      * @var array
49      */
50     private $managers;
51
52     /**
53      * @var string
54      */
55     private $defaultConnection;
56
57     /**
58      * @var string
59      */
60     private $defaultManager;
61
62     /**
63      * @var string
64      */
65     private $proxyInterfaceName;
66
67     /**
68      * Constructor
69      *
70      * @param string $name
71      * @param array $connections
72      * @param array $managers
73      * @param string $defaultConnection
74      * @param string $defaultManager
75      * @param string $proxyInterfaceName
76      */
77     public function __construct($name, array $connections, array $managers, $defaultConnection, $defaultManager, $proxyInterfaceName)
78     {
79         $this->name = $name;
80         $this->connections = $connections;
81         $this->managers = $managers;
82         $this->defaultConnection = $defaultConnection;
83         $this->defaultManager = $defaultManager;
84         $this->proxyInterfaceName = $proxyInterfaceName;
85     }
86
87     /**
88      * Fetches/creates the given services
89      *
90      * A service in this context is connection or a manager instance
91      *
92      * @param string $name name of the service
93      * @return object instance of the given service
94      */
95     abstract protected function getService($name);
96
97     /**
98      * Resets the given services
99      *
100      * A service in this context is connection or a manager instance
101      *
102      * @param string $name name of the service
103      * @return void
104      */
105     abstract protected function resetService($name);
106
107     /**
108      * Get the name of the registry
109      *
110      * @return string
111      */
112     public function getName()
113     {
114         return $this->name;
115     }
116
117     /**
118      * {@inheritdoc}
119      */
120     public function getConnection($name = null)
121     {
122         if (null === $name) {
123             $name = $this->defaultConnection;
124         }
125
126         if (!isset($this->connections[$name])) {
127             throw new \InvalidArgumentException(sprintf('Doctrine %s Connection named "%s" does not exist.', $this->name, $name));
128         }
129
130         return $this->getService($this->connections[$name]);
131     }
132
133     /**
134      * {@inheritdoc}
135      */
136     public function getConnectionNames()
137     {
138         return $this->connections;
139     }
140
141     /**
142      * {@inheritdoc}
143      */
144     public function getConnections()
145     {
146         $connections = array();
147         foreach ($this->connections as $name => $id) {
148             $connections[$name] = $this->getService($id);
149         }
150
151         return $connections;
152     }
153
154     /**
155      * {@inheritdoc}
156      */
157     public function getDefaultConnectionName()
158     {
159         return $this->defaultConnection;
160     }
161
162     /**
163      * {@inheritdoc}
164      */
165     public function getDefaultManagerName()
166     {
167         return $this->defaultManager;
168     }
169
170     /**
171      * {@inheritdoc}
172      *
173      * @throws \InvalidArgumentException
174      */
175     public function getManager($name = null)
176     {
177         if (null === $name) {
178             $name = $this->defaultManager;
179         }
180
181         if (!isset($this->managers[$name])) {
182             throw new \InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
183         }
184
185         return $this->getService($this->managers[$name]);
186     }
187
188     /**
189      * {@inheritdoc}
190      */
191     public function getManagerForClass($class)
192     {
193         // Check for namespace alias
194         if (strpos($class, ':') !== false) {
195             list($namespaceAlias, $simpleClassName) = explode(':', $class);
196             $class = $this->getAliasNamespace($namespaceAlias) . '\\' . $simpleClassName;
197         }
198
199         $proxyClass = new \ReflectionClass($class);
200         if ($proxyClass->implementsInterface($this->proxyInterfaceName)) {
201             $class = $proxyClass->getParentClass()->getName();
202         }
203
204         foreach ($this->managers as $id) {
205             $manager = $this->getService($id);
206
207             if (!$manager->getMetadataFactory()->isTransient($class)) {
208                 return $manager;
209             }
210         }
211     }
212
213     /**
214      * {@inheritdoc}
215      */
216     public function getManagerNames()
217     {
218         return $this->managers;
219     }
220
221     /**
222      * {@inheritdoc}
223      */
224     public function getManagers()
225     {
226         $dms = array();
227         foreach ($this->managers as $name => $id) {
228             $dms[$name] = $this->getService($id);
229         }
230
231         return $dms;
232     }
233
234     /**
235      * {@inheritdoc}
236      */
237     public function getRepository($persistentObjectName, $persistentManagerName = null)
238     {
239         return $this->getManager($persistentManagerName)->getRepository($persistentObjectName);
240     }
241
242     /**
243      * {@inheritdoc}
244      */
245     public function resetManager($name = null)
246     {
247         if (null === $name) {
248             $name = $this->defaultManager;
249         }
250
251         if (!isset($this->managers[$name])) {
252             throw new \InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
253         }
254
255         // force the creation of a new document manager
256         // if the current one is closed
257         $this->resetService($this->managers[$name]);
258     }
259 }