MongoDocumentWrapper.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 setPropertyValue($property, $value)
  53. {
  54. $this->initialize();
  55. $this->meta->getReflectionProperty($property)->setValue($this->object, $value);
  56. return $this;
  57. }
  58. /**
  59. * {@inheritDoc}
  60. */
  61. public function hasValidIdentifier()
  62. {
  63. return (bool)$this->getIdentifier();
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. public function getIdentifier($single = true)
  69. {
  70. if (!$this->identifier) {
  71. if ($this->object instanceof Proxy) {
  72. $uow = $this->om->getUnitOfWork();
  73. if ($uow->isInIdentityMap($this->object)) {
  74. $this->identifier = (string)$uow->getDocumentIdentifier($this->object);
  75. } else {
  76. $this->initialize();
  77. }
  78. }
  79. if (!$this->identifier) {
  80. $this->identifier = (string)$this->getPropertyValue($this->meta->identifier);
  81. }
  82. }
  83. return $this->identifier;
  84. }
  85. /**
  86. * Initialize the document if it is proxy
  87. * required when is detached or not initialized
  88. */
  89. protected function initialize()
  90. {
  91. if (!$this->initialized) {
  92. if ($this->object instanceof Proxy) {
  93. $uow = $this->om->getUnitOfWork();
  94. if (!$this->object->__isInitialized__) {
  95. $persister = $uow->getDocumentPersister($this->meta->name);
  96. $identifier = null;
  97. if ($uow->isInIdentityMap($this->object)) {
  98. $identifier = $this->getIdentifier();
  99. } else {
  100. // this may not happen but in case
  101. $reflProperty = new \ReflectionProperty($this->object, 'identifier');
  102. $reflProperty->setAccessible(true);
  103. $identifier = $reflProperty->getValue($this->object);
  104. }
  105. $this->object->__isInitialized__ = true;
  106. $persister->load($identifier, $this->object);
  107. }
  108. }
  109. }
  110. }
  111. }