AbstractManagerRegistry.php 6.5KB

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