ODM.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 __call($method, $args)
  76. {
  77. if (is_null($this->args)) {
  78. throw new RuntimeException("Event args must be set before calling its methods");
  79. }
  80. $method = str_replace('Object', $this->getDomainObjectName(), $method);
  81. return call_user_func_array(array($this->args, $method), $args);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getObjectChangeSet($uow, $object)
  87. {
  88. return $uow->getDocumentChangeSet($object);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getSingleIdentifierFieldName($meta)
  94. {
  95. return $meta->identifier;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function recomputeSingleObjectChangeSet($uow, $meta, $object)
  101. {
  102. $uow->recomputeSingleDocumentChangeSet($meta, $object);
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function getScheduledObjectUpdates($uow)
  108. {
  109. return $uow->getScheduledDocumentUpdates();
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function getScheduledObjectInsertions($uow)
  115. {
  116. return $uow->getScheduledDocumentInsertions();
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function getScheduledObjectDeletions($uow)
  122. {
  123. return $uow->getScheduledDocumentDeletions();
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function setOriginalObjectProperty($uow, $oid, $property, $value)
  129. {
  130. $uow->setOriginalDocumentProperty($oid, $property, $value);
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function clearObjectChangeSet($uow, $oid)
  136. {
  137. $uow->clearDocumentChangeSet($oid);
  138. }
  139. /**
  140. * Creates a ODM specific LifecycleEventArgs.
  141. *
  142. * @param $document
  143. * @param \Doctrine\ODM\MongoDB\DocumentManager $documentManager
  144. * @return \Doctrine\ODM\MongoDB\Event\LifecycleEventArgs
  145. */
  146. public function createLifecycleEventArgsInstance($document, $documentManager)
  147. {
  148. return new LifecycleEventArgs($document, $documentManager);
  149. }
  150. }