MongoDocumentWrapper.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Gedmo\Tool\Wrapper;
  3. use Doctrine\ODM\MongoDB\DocumentManager;
  4. use Doctrine\ODM\MongoDB\Proxy\Proxy;
  5. /**
  6. * Wraps document or proxy for more convenient
  7. * manipulation
  8. *
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @package Gedmo.Tool.Wrapper
  11. * @subpackage MongoDocumentWrapper
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class MongoDocumentWrapper extends AbstractWrapper
  16. {
  17. /**
  18. * Document identifier
  19. *
  20. * @var mixed
  21. */
  22. private $identifier;
  23. /**
  24. * True if document or proxy is loaded
  25. *
  26. * @var boolean
  27. */
  28. private $initialized = false;
  29. /**
  30. * Wrapp document
  31. *
  32. * @param object $document
  33. * @param \Doctrine\ODM\MongoDB\DocumentManager $dm
  34. */
  35. public function __construct($document, DocumentManager $dm)
  36. {
  37. $this->om = $dm;
  38. $this->object = $document;
  39. $this->meta = $dm->getClassMetadata(get_class($this->object));
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function getPropertyValue($property)
  45. {
  46. $this->initialize();
  47. return $this->meta->getReflectionProperty($property)->getValue($this->object);
  48. }
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function getRootObjectName()
  53. {
  54. return $this->meta->rootDocumentName;
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public function setPropertyValue($property, $value)
  60. {
  61. $this->initialize();
  62. $this->meta->getReflectionProperty($property)->setValue($this->object, $value);
  63. return $this;
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. public function hasValidIdentifier()
  69. {
  70. return (bool)$this->getIdentifier();
  71. }
  72. /**
  73. * {@inheritDoc}
  74. */
  75. public function getIdentifier($single = true)
  76. {
  77. if (!$this->identifier) {
  78. if ($this->object instanceof Proxy) {
  79. $uow = $this->om->getUnitOfWork();
  80. if ($uow->isInIdentityMap($this->object)) {
  81. $this->identifier = (string)$uow->getDocumentIdentifier($this->object);
  82. } else {
  83. $this->initialize();
  84. }
  85. }
  86. if (!$this->identifier) {
  87. $this->identifier = (string)$this->getPropertyValue($this->meta->identifier);
  88. }
  89. }
  90. return $this->identifier;
  91. }
  92. /**
  93. * Initialize the document if it is proxy
  94. * required when is detached or not initialized
  95. */
  96. protected function initialize()
  97. {
  98. if (!$this->initialized) {
  99. if ($this->object instanceof Proxy) {
  100. $uow = $this->om->getUnitOfWork();
  101. if (!$this->object->__isInitialized__) {
  102. $persister = $uow->getDocumentPersister($this->meta->name);
  103. $identifier = null;
  104. if ($uow->isInIdentityMap($this->object)) {
  105. $identifier = $this->getIdentifier();
  106. } else {
  107. // this may not happen but in case
  108. $reflProperty = new \ReflectionProperty($this->object, 'identifier');
  109. $reflProperty->setAccessible(true);
  110. $identifier = $reflProperty->getValue($this->object);
  111. }
  112. $this->object->__isInitialized__ = true;
  113. $persister->load($identifier, $this->object);
  114. }
  115. }
  116. }
  117. }
  118. }