ORM.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace Gedmo\Mapping\Event\Adapter;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Gedmo\Mapping\Event\AdapterInterface;
  5. use Gedmo\Exception\RuntimeException;
  6. use Doctrine\Common\EventArgs;
  7. use Doctrine\ORM\Event\LifecycleEventArgs;
  8. /**
  9. * Doctrine event adapter for ORM 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 ORM implements AdapterInterface
  16. {
  17. /**
  18. * @var \Doctrine\Common\EventArgs
  19. */
  20. private $args;
  21. /**
  22. * @var \Doctrine\ORM\EntityManagerInterface
  23. */
  24. private $em;
  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 'Entity';
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getManagerName()
  43. {
  44. return 'ORM';
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getRootObjectClass($meta)
  50. {
  51. return $meta->rootEntityName;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function __call($method, $args)
  57. {
  58. if (is_null($this->args)) {
  59. throw new RuntimeException("Event args must be set before calling its methods");
  60. }
  61. $method = str_replace('Object', $this->getDomainObjectName(), $method);
  62. return call_user_func_array(array($this->args, $method), $args);
  63. }
  64. /**
  65. * Set the entity manager
  66. *
  67. * @param \Doctrine\ORM\EntityManagerInterface $em
  68. */
  69. public function setEntityManager(EntityManagerInterface $em)
  70. {
  71. $this->em = $em;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getObjectManager()
  77. {
  78. if (!is_null($this->em)) {
  79. return $this->em;
  80. }
  81. return $this->__call('getEntityManager', array());
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getObjectState($uow, $object)
  87. {
  88. return $uow->getEntityState($object);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getObjectChangeSet($uow, $object)
  94. {
  95. return $uow->getEntityChangeSet($object);
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function getSingleIdentifierFieldName($meta)
  101. {
  102. return $meta->getSingleIdentifierFieldName();
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function recomputeSingleObjectChangeSet($uow, $meta, $object)
  108. {
  109. $uow->recomputeSingleEntityChangeSet($meta, $object);
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function getScheduledObjectUpdates($uow)
  115. {
  116. return $uow->getScheduledEntityUpdates();
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function getScheduledObjectInsertions($uow)
  122. {
  123. return $uow->getScheduledEntityInsertions();
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function getScheduledObjectDeletions($uow)
  129. {
  130. return $uow->getScheduledEntityDeletions();
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function setOriginalObjectProperty($uow, $oid, $property, $value)
  136. {
  137. $uow->setOriginalEntityProperty($oid, $property, $value);
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function clearObjectChangeSet($uow, $oid)
  143. {
  144. $uow->clearEntityChangeSet($oid);
  145. }
  146. /**
  147. * Creates a ORM specific LifecycleEventArgs.
  148. *
  149. * @param object $document
  150. * @param \Doctrine\ODM\MongoDB\DocumentManager $documentManager
  151. *
  152. * @return \Doctrine\ODM\MongoDB\Event\LifecycleEventArgs
  153. */
  154. public function createLifecycleEventArgsInstance($document, $documentManager)
  155. {
  156. return new LifecycleEventArgs($document, $documentManager);
  157. }
  158. }