ODM.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Gedmo\Mapping\Event\Adapter;
  3. use Gedmo\Mapping\Event\AdapterInterface;
  4. use Gedmo\Exception\RuntimeException;
  5. use Doctrine\Common\EventArgs;
  6. use Doctrine\ODM\MongoDB\DocumentManager;
  7. use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
  8. /**
  9. * Doctrine event adapter for ODM specific
  10. * event arguments
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class ODM implements AdapterInterface
  16. {
  17. /**
  18. * @var \Doctrine\Common\EventArgs
  19. */
  20. private $args;
  21. /**
  22. * @var \Doctrine\ODM\MongoDB\DocumentManager
  23. */
  24. private $dm;
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function setEventArgs(EventArgs $args)
  29. {
  30. $this->args = $args;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getDomainObjectName()
  36. {
  37. return 'Document';
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getManagerName()
  43. {
  44. return 'ODM';
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getRootObjectClass($meta)
  50. {
  51. return $meta->rootDocumentName;
  52. }
  53. /**
  54. * Set the document manager
  55. *
  56. * @param \Doctrine\ODM\MongoDB\DocumentManager $dm
  57. */
  58. public function setDocumentManager(DocumentManager $dm)
  59. {
  60. $this->dm = $dm;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getObjectManager()
  66. {
  67. if (!is_null($this->dm)) {
  68. return $this->dm;
  69. }
  70. return $this->__call('getDocumentManager', array());
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function getObjectState($uow, $object)
  76. {
  77. return $uow->getDocumentState($object);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function __call($method, $args)
  83. {
  84. if (is_null($this->args)) {
  85. throw new RuntimeException("Event args must be set before calling its methods");
  86. }
  87. $method = str_replace('Object', $this->getDomainObjectName(), $method);
  88. return call_user_func_array(array($this->args, $method), $args);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getObjectChangeSet($uow, $object)
  94. {
  95. return $uow->getDocumentChangeSet($object);
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function getSingleIdentifierFieldName($meta)
  101. {
  102. return $meta->identifier;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function recomputeSingleObjectChangeSet($uow, $meta, $object)
  108. {
  109. $uow->recomputeSingleDocumentChangeSet($meta, $object);
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function getScheduledObjectUpdates($uow)
  115. {
  116. $updates = $uow->getScheduledDocumentUpdates();
  117. $upserts = $uow->getScheduledDocumentUpserts();
  118. return array_merge($updates, $upserts);
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function getScheduledObjectInsertions($uow)
  124. {
  125. return $uow->getScheduledDocumentInsertions();
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function getScheduledObjectDeletions($uow)
  131. {
  132. return $uow->getScheduledDocumentDeletions();
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function setOriginalObjectProperty($uow, $oid, $property, $value)
  138. {
  139. $uow->setOriginalDocumentProperty($oid, $property, $value);
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function clearObjectChangeSet($uow, $oid)
  145. {
  146. $uow->clearDocumentChangeSet($oid);
  147. }
  148. /**
  149. * Creates a ODM specific LifecycleEventArgs.
  150. *
  151. * @param object $document
  152. * @param \Doctrine\ODM\MongoDB\DocumentManager $documentManager
  153. *
  154. * @return \Doctrine\ODM\MongoDB\Event\LifecycleEventArgs
  155. */
  156. public function createLifecycleEventArgsInstance($document, $documentManager)
  157. {
  158. return new LifecycleEventArgs($document, $documentManager);
  159. }
  160. }