AbstractMaterializedPath.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace Gedmo\Tree\Strategy;
  3. use Gedmo\Tree\Strategy;
  4. use Doctrine\ORM\EntityManager;
  5. use Gedmo\Tree\TreeListener;
  6. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  7. use Doctrine\ORM\Query;
  8. use Doctrine\Common\Persistence\ObjectManager;
  9. use Gedmo\Mapping\Event\AdapterInterface;
  10. /**
  11. * This strategy makes tree using materialized path strategy
  12. *
  13. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  14. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  15. * @package Gedmo.Tree.Strategy
  16. * @subpackage AbstractMaterializedPath
  17. * @link http://www.gediminasm.org
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. abstract class AbstractMaterializedPath implements Strategy
  21. {
  22. /**
  23. * TreeListener
  24. *
  25. * @var AbstractTreeListener
  26. */
  27. protected $listener = null;
  28. /**
  29. * Array of objects which were scheduled for path processes
  30. *
  31. * @var array
  32. */
  33. protected $scheduledForPathProcess = array();
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function __construct(TreeListener $listener)
  38. {
  39. $this->listener = $listener;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function getName()
  45. {
  46. return Strategy::MATERIALIZED_PATH;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function processScheduledInsertion($om, $node, AdapterInterface $ea)
  52. {
  53. $meta = $om->getClassMetadata(get_class($node));
  54. $config = $this->listener->getConfiguration($om, $meta->name);
  55. if ($meta->isIdentifier($config['path_source'])) {
  56. $this->scheduledForPathProcess[spl_object_hash($node)] = node;
  57. } else {
  58. $this->updateNode($om, $node, $ea);
  59. }
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function processScheduledUpdate($om, $node, AdapterInterface $ea)
  65. {
  66. $meta = $om->getClassMetadata(get_class($node));
  67. $config = $this->listener->getConfiguration($om, $meta->name);
  68. $uow = $om->getUnitOfWork();
  69. $changeSet = $ea->getObjectChangeSet($uow, $node);
  70. if (isset($changeSet[$config['parent']]) || isset($changeSet[$config['path_source']])) {
  71. if (isset($changeSet[$config['path']])) {
  72. $originalPath = $changeSet[$config['path']][0];
  73. } else {
  74. $pathProp = $meta->getReflectionProperty($config['path']);
  75. $pathProp->setAccessible(true);
  76. $originalPath = $pathProp->getValue($node);
  77. }
  78. $this->updateNode($om, $node, $ea);
  79. $this->updateChildren($om, $node, $ea, $originalPath);
  80. }
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function processPostPersist($om, $node, AdapterInterface $ea)
  86. {
  87. foreach ($this->scheduledForPathProcess as $node) {
  88. $this->updateNode($om, $node, $ea);
  89. unset($this->scheduledForPathProcess[spl_object_hash($node)]);
  90. }
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function onFlushEnd($om)
  96. {
  97. $this->scheduledForPathProcess = array();
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function processPreRemove($om, $node)
  103. {}
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function processPrePersist($om, $node)
  108. {}
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function processMetadataLoad($om, $meta)
  113. {}
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function processScheduledDelete($om, $node)
  118. {
  119. $meta = $om->getClassMetadata(get_class($node));
  120. $config = $this->listener->getConfiguration($om, $meta->name);
  121. $this->removeNode($om, $meta, $config, $node);
  122. }
  123. /**
  124. * Update the $node
  125. *
  126. * @param ObjectManager $om
  127. * @param object $node - target node
  128. * @param object $ea - event adapter
  129. * @return void
  130. */
  131. public function updateNode(ObjectManager $om, $node, AdapterInterface $ea)
  132. {
  133. $oid = spl_object_hash($node);
  134. $meta = $om->getClassMetadata(get_class($node));
  135. $config = $this->listener->getConfiguration($om, $meta->name);
  136. $uow = $om->getUnitOfWork();
  137. $parentProp = $meta->getReflectionProperty($config['parent']);
  138. $parentProp->setAccessible(true);
  139. $parent = $parentProp->getValue($node);
  140. $pathProp = $meta->getReflectionProperty($config['path']);
  141. $pathProp->setAccessible(true);
  142. $pathSourceProp = $meta->getReflectionProperty($config['path_source']);
  143. $pathSourceProp->setAccessible(true);
  144. $path = $pathSourceProp->getValue($node).$config['path_separator'];
  145. if ($parent) {
  146. $changeSet = $uow->isScheduledForUpdate($parent) ? $ea->getObjectChangeSet($uow, $parent) : false;
  147. $pathOrPathSourceHasChanged = $changeSet && (isset($changeSet[$config['path_source']]) || isset($changeSet[$config['path']]));
  148. if ($pathOrPathSourceHasChanged || !$pathProp->getValue($parent)) {
  149. $this->updateNode($om, $parent, $ea);
  150. }
  151. $path = $pathProp->getValue($parent).$path;
  152. }
  153. $pathProp->setValue($node, $path);
  154. $changes = array(
  155. $config['path'] => array(null, $path)
  156. );
  157. if (isset($config['level'])) {
  158. $level = substr_count($path, $config['path_separator']);
  159. $levelProp = $meta->getReflectionProperty($config['level']);
  160. $levelProp->setAccessible(true);
  161. $levelProp->setValue($node, $level);
  162. $changes[$config['level']] = array(null, $level);
  163. }
  164. $uow->scheduleExtraUpdate($node, $changes);
  165. $ea->setOriginalObjectProperty($uow, $oid, $config['path'], $path);
  166. }
  167. /**
  168. * Update $node 's children
  169. *
  170. * @param ObjectManager $om
  171. * @param object $node - target node
  172. * @param object $ea - event adapter
  173. * @param string $originalPath - original path of object
  174. * @return void
  175. */
  176. public function updateChildren(ObjectManager $om, $node, AdapterInterface $ea, $originalPath)
  177. {
  178. $meta = $om->getClassMetadata(get_class($node));
  179. $config = $this->listener->getConfiguration($om, $meta->name);
  180. $children = $this->getChildren($om, $meta, $config, $originalPath);
  181. foreach ($children as $child) {
  182. $this->updateNode($om, $child, $ea);
  183. }
  184. }
  185. /**
  186. * Remove node and its children
  187. *
  188. * @param ObjectManager $om
  189. * @param object $meta - Metadata
  190. * @param object $config - config
  191. * @param object $node - node to remove
  192. * @return void
  193. */
  194. abstract public function removeNode($om, $meta, $config, $node);
  195. /**
  196. * Returns children of the node with its original path
  197. *
  198. * @param ObjectManager $om
  199. * @param object $meta - Metadata
  200. * @param object $config - config
  201. * @param mixed $originalPath - original path of object
  202. * @return Doctrine\ODM\MongoDB\Cursor
  203. */
  204. abstract public function getChildren($om, $meta, $config, $originalPath);
  205. }