AbstractManagerRegistry.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 LGPL. 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. private $name;
  34. private $connections;
  35. private $managers;
  36. private $defaultConnection;
  37. private $defaultManager;
  38. private $proxyInterfaceName;
  39. public function __construct($name, array $connections, array $managers, $defaultConnection, $defaultManager, $proxyInterfaceName)
  40. {
  41. $this->name = $name;
  42. $this->connections = $connections;
  43. $this->managers = $managers;
  44. $this->defaultConnection = $defaultConnection;
  45. $this->defaultManager = $defaultManager;
  46. $this->proxyInterfaceName = $proxyInterfaceName;
  47. }
  48. /**
  49. * Fetches/creates the given services
  50. *
  51. * A service in this context is connection or a manager instance
  52. *
  53. * @param string $name name of the service
  54. * @return object instance of the given service
  55. */
  56. abstract protected function getService($name);
  57. /**
  58. * Resets the given services
  59. *
  60. * A service in this context is connection or a manager instance
  61. *
  62. * @param string $name name of the service
  63. * @return void
  64. */
  65. abstract protected function resetService($name);
  66. /**
  67. * Get the name of the registry
  68. *
  69. * @return string
  70. */
  71. public function getName()
  72. {
  73. return $this->name;
  74. }
  75. /**
  76. * @inheritdoc
  77. */
  78. public function getConnection($name = null)
  79. {
  80. if (null === $name) {
  81. $name = $this->defaultConnection;
  82. }
  83. if (!isset($this->connections[$name])) {
  84. throw new \InvalidArgumentException(sprintf('Doctrine %s Connection named "%s" does not exist.', $this->name, $name));
  85. }
  86. return $this->getService($this->connections[$name]);
  87. }
  88. /**
  89. * @inheritdoc
  90. */
  91. public function getConnectionNames()
  92. {
  93. return $this->connections;
  94. }
  95. /**
  96. * @inheritdoc
  97. */
  98. public function getConnections()
  99. {
  100. $connections = array();
  101. foreach ($this->connections as $name => $id) {
  102. $connections[$name] = $this->getService($id);
  103. }
  104. return $connections;
  105. }
  106. /**
  107. * @inheritdoc
  108. */
  109. public function getDefaultConnectionName()
  110. {
  111. return $this->defaultConnection;
  112. }
  113. /**
  114. * @inheritdoc
  115. */
  116. public function getDefaultManagerName()
  117. {
  118. return $this->defaultManager;
  119. }
  120. /**
  121. * @inheritdoc
  122. */
  123. public function getManager($name = null)
  124. {
  125. if (null === $name) {
  126. $name = $this->defaultManager;
  127. }
  128. if (!isset($this->managers[$name])) {
  129. throw new \InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
  130. }
  131. return $this->getService($this->managers[$name]);
  132. }
  133. /**
  134. * @inheritdoc
  135. */
  136. public function getManagerForClass($class)
  137. {
  138. $proxyClass = new \ReflectionClass($class);
  139. if ($proxyClass->implementsInterface($this->proxyInterfaceName)) {
  140. $class = $proxyClass->getParentClass()->getName();
  141. }
  142. foreach ($this->managers as $id) {
  143. $manager = $this->getService($id);
  144. if (!$manager->getMetadataFactory()->isTransient($class)) {
  145. return $manager;
  146. }
  147. }
  148. }
  149. /**
  150. * @inheritdoc
  151. */
  152. public function getManagerNames()
  153. {
  154. return $this->managers;
  155. }
  156. /**
  157. * @inheritdoc
  158. */
  159. public function getManagers()
  160. {
  161. $dms = array();
  162. foreach ($this->managers as $name => $id) {
  163. $dms[$name] = $this->getService($id);
  164. }
  165. return $dms;
  166. }
  167. /**
  168. * @inheritdoc
  169. */
  170. public function getRepository($persistentObjectName, $persistentManagerName = null)
  171. {
  172. return $this->getManager($persistentManagerName)->getRepository($persistentObjectName);
  173. }
  174. /**
  175. * @inheritdoc
  176. */
  177. public function resetManager($name = null)
  178. {
  179. if (null === $name) {
  180. $name = $this->defaultManager;
  181. }
  182. if (!isset($this->managers[$name])) {
  183. throw new \InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
  184. }
  185. // force the creation of a new document manager
  186. // if the current one is closed
  187. $this->resetService($this->managers[$name]);
  188. }
  189. }