ReferenceRepository.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace Doctrine\Common\DataFixtures;
  3. use Doctrine\Common\Persistence\ObjectManager;
  4. use Doctrine\ODM\PHPCR\DocumentManager as PhpcrDocumentManager;
  5. /**
  6. * ReferenceRepository class manages references for
  7. * fixtures in order to easily support the relations
  8. * between fixtures
  9. *
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. */
  12. class ReferenceRepository
  13. {
  14. /**
  15. * List of named references to the fixture objects
  16. * gathered during loads of fixtures
  17. *
  18. * @var array
  19. */
  20. private $references = [];
  21. /**
  22. * List of identifiers stored for references
  23. * in case if reference gets unmanaged, it will
  24. * use a proxy referenced by this identity
  25. *
  26. * @var array
  27. */
  28. private $identities = [];
  29. /**
  30. * Currently used object manager
  31. *
  32. * @var ObjectManager
  33. */
  34. private $manager;
  35. public function __construct(ObjectManager $manager)
  36. {
  37. $this->manager = $manager;
  38. }
  39. /**
  40. * Get identifier for a unit of work
  41. *
  42. * @param object $reference Reference object
  43. * @param object $uow Unit of work
  44. *
  45. * @return array
  46. */
  47. protected function getIdentifier($reference, $uow)
  48. {
  49. // In case Reference is not yet managed in UnitOfWork
  50. if ( ! $this->hasIdentifier($reference)) {
  51. $class = $this->manager->getClassMetadata(get_class($reference));
  52. return $class->getIdentifierValues($reference);
  53. }
  54. // Dealing with ORM UnitOfWork
  55. if (method_exists($uow, 'getEntityIdentifier')) {
  56. return $uow->getEntityIdentifier($reference);
  57. }
  58. // PHPCR ODM UnitOfWork
  59. if ($this->manager instanceof PhpcrDocumentManager) {
  60. return $uow->getDocumentId($reference);
  61. }
  62. // ODM UnitOfWork
  63. return $uow->getDocumentIdentifier($reference);
  64. }
  65. /**
  66. * Set the reference entry identified by $name
  67. * and referenced to $reference. If $name
  68. * already is set, it overrides it
  69. *
  70. * @param string $name
  71. * @param object $reference
  72. */
  73. public function setReference($name, $reference)
  74. {
  75. $this->references[$name] = $reference;
  76. if ($this->hasIdentifier($reference)) {
  77. // in case if reference is set after flush, store its identity
  78. $uow = $this->manager->getUnitOfWork();
  79. $this->identities[$name] = $this->getIdentifier($reference, $uow);
  80. }
  81. }
  82. /**
  83. * Store the identifier of a reference
  84. *
  85. * @param string $name
  86. * @param mixed $identity
  87. */
  88. public function setReferenceIdentity($name, $identity)
  89. {
  90. $this->identities[$name] = $identity;
  91. }
  92. /**
  93. * Set the reference entry identified by $name
  94. * and referenced to managed $object. $name must
  95. * not be set yet
  96. *
  97. * Notice: in case if identifier is generated after
  98. * the record is inserted, be sure tu use this method
  99. * after $object is flushed
  100. *
  101. * @param string $name
  102. * @param object $object - managed object
  103. * @throws \BadMethodCallException - if repository already has
  104. * a reference by $name
  105. * @return void
  106. */
  107. public function addReference($name, $object)
  108. {
  109. if (isset($this->references[$name])) {
  110. throw new \BadMethodCallException("Reference to: ({$name}) already exists, use method setReference in order to override it");
  111. }
  112. $this->setReference($name, $object);
  113. }
  114. /**
  115. * Loads an object using stored reference
  116. * named by $name
  117. *
  118. * @param string $name
  119. * @throws \OutOfBoundsException - if repository does not exist
  120. * @return object
  121. */
  122. public function getReference($name)
  123. {
  124. if (!$this->hasReference($name)) {
  125. throw new \OutOfBoundsException("Reference to: ({$name}) does not exist");
  126. }
  127. $reference = $this->references[$name];
  128. $meta = $this->manager->getClassMetadata(get_class($reference));
  129. if (!$this->manager->contains($reference) && isset($this->identities[$name])) {
  130. $reference = $this->manager->getReference(
  131. $meta->name,
  132. $this->identities[$name]
  133. );
  134. $this->references[$name] = $reference; // already in identity map
  135. }
  136. return $reference;
  137. }
  138. /**
  139. * Check if an object is stored using reference
  140. * named by $name
  141. *
  142. * @param string $name
  143. * @return boolean
  144. */
  145. public function hasReference($name)
  146. {
  147. return isset($this->references[$name]);
  148. }
  149. /**
  150. * Searches for reference names in the
  151. * list of stored references
  152. *
  153. * @param object $reference
  154. * @return array
  155. */
  156. public function getReferenceNames($reference)
  157. {
  158. return array_keys($this->references, $reference, true);
  159. }
  160. /**
  161. * Checks if reference has identity stored
  162. *
  163. * @param string $name
  164. */
  165. public function hasIdentity($name)
  166. {
  167. return array_key_exists($name, $this->identities);
  168. }
  169. /**
  170. * Get all stored identities
  171. *
  172. * @return array
  173. */
  174. public function getIdentities()
  175. {
  176. return $this->identities;
  177. }
  178. /**
  179. * Get all stored references
  180. *
  181. * @return array
  182. */
  183. public function getReferences()
  184. {
  185. return $this->references;
  186. }
  187. /**
  188. * Get object manager
  189. *
  190. * @return ObjectManager
  191. */
  192. public function getManager()
  193. {
  194. return $this->manager;
  195. }
  196. /**
  197. * Checks if object has identifier already in unit of work.
  198. *
  199. * @param $reference
  200. *
  201. * @return bool
  202. */
  203. private function hasIdentifier($reference)
  204. {
  205. // in case if reference is set after flush, store its identity
  206. $uow = $this->manager->getUnitOfWork();
  207. if ($this->manager instanceof PhpcrDocumentManager) {
  208. return $uow->contains($reference);
  209. } else {
  210. return $uow->isInIdentityMap($reference);
  211. }
  212. }
  213. }