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