AbstractManagerRegistry.php 6.5 KB

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