AbstractMaterializedPath.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. * Array of objects which were scheduled for path process.
  36. * This time, this array contains the objects with their ID
  37. * already set
  38. *
  39. * @var array
  40. */
  41. protected $scheduledForPathProcessWithIdSet = array();
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function __construct(TreeListener $listener)
  46. {
  47. $this->listener = $listener;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getName()
  53. {
  54. return Strategy::MATERIALIZED_PATH;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function processScheduledInsertion($om, $node, AdapterInterface $ea)
  60. {
  61. $meta = $om->getClassMetadata(get_class($node));
  62. $config = $this->listener->getConfiguration($om, $meta->name);
  63. $fieldMapping = $meta->getFieldMapping($config['path_source']);
  64. if ($meta->isIdentifier($config['path_source']) || $fieldMapping['type'] === 'string') {
  65. $this->scheduledForPathProcess[spl_object_hash($node)] = $node;
  66. } else {
  67. $this->updateNode($om, $node, $ea);
  68. }
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function processScheduledUpdate($om, $node, AdapterInterface $ea)
  74. {
  75. $meta = $om->getClassMetadata(get_class($node));
  76. $config = $this->listener->getConfiguration($om, $meta->name);
  77. $uow = $om->getUnitOfWork();
  78. $changeSet = $ea->getObjectChangeSet($uow, $node);
  79. if (isset($changeSet[$config['parent']]) || isset($changeSet[$config['path_source']])) {
  80. if (isset($changeSet[$config['path']])) {
  81. $originalPath = $changeSet[$config['path']][0];
  82. } else {
  83. $pathProp = $meta->getReflectionProperty($config['path']);
  84. $pathProp->setAccessible(true);
  85. $originalPath = $pathProp->getValue($node);
  86. }
  87. $this->updateNode($om, $node, $ea);
  88. $this->updateChildren($om, $node, $ea, $originalPath);
  89. }
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function processPostPersist($om, $node, AdapterInterface $ea)
  95. {
  96. $oid = spl_object_hash($node);
  97. if ($this->scheduledForPathProcess && array_key_exists($oid, $this->scheduledForPathProcess)) {
  98. $this->scheduledForPathProcessWithIdSet[$oid] = $node;
  99. unset($this->scheduledForPathProcess[$oid]);
  100. if (empty($this->scheduledForPathProcess)) {
  101. foreach ($this->scheduledForPathProcessWithIdSet as $oid => $node) {
  102. $this->updateNode($om, $node, $ea);
  103. unset($this->scheduledForPathProcessWithIdSet[$oid]);
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function onFlushEnd($om)
  112. {
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function processPreRemove($om, $node)
  118. {}
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function processPrePersist($om, $node)
  123. {}
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function processMetadataLoad($om, $meta)
  128. {}
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function processScheduledDelete($om, $node)
  133. {
  134. $meta = $om->getClassMetadata(get_class($node));
  135. $config = $this->listener->getConfiguration($om, $meta->name);
  136. $this->removeNode($om, $meta, $config, $node);
  137. }
  138. /**
  139. * Update the $node
  140. *
  141. * @param ObjectManager $om
  142. * @param object $node - target node
  143. * @param object $ea - event adapter
  144. * @return void
  145. */
  146. public function updateNode(ObjectManager $om, $node, AdapterInterface $ea)
  147. {
  148. $oid = spl_object_hash($node);
  149. $meta = $om->getClassMetadata(get_class($node));
  150. $config = $this->listener->getConfiguration($om, $meta->name);
  151. $uow = $om->getUnitOfWork();
  152. $parentProp = $meta->getReflectionProperty($config['parent']);
  153. $parentProp->setAccessible(true);
  154. $parent = $parentProp->getValue($node);
  155. $pathProp = $meta->getReflectionProperty($config['path']);
  156. $pathProp->setAccessible(true);
  157. $pathSourceProp = $meta->getReflectionProperty($config['path_source']);
  158. $pathSourceProp->setAccessible(true);
  159. $path = $pathSourceProp->getValue($node);
  160. $fieldMapping = $meta->getFieldMapping($config['path_source']);
  161. // If PathSource field is a string, we append the ID to the path
  162. if ($fieldMapping['type'] === 'string') {
  163. $path .= '-'.$meta->getIdentifierValue($node);
  164. }
  165. $path .= $config['path_separator'];
  166. if ($parent) {
  167. $changeSet = $uow->isScheduledForUpdate($parent) ? $ea->getObjectChangeSet($uow, $parent) : false;
  168. $pathOrPathSourceHasChanged = $changeSet && (isset($changeSet[$config['path_source']]) || isset($changeSet[$config['path']]));
  169. if ($pathOrPathSourceHasChanged || !$pathProp->getValue($parent)) {
  170. $this->updateNode($om, $parent, $ea);
  171. }
  172. $path = $pathProp->getValue($parent).$path;
  173. }
  174. $pathProp->setValue($node, $path);
  175. $changes = array(
  176. $config['path'] => array(null, $path)
  177. );
  178. if (isset($config['level'])) {
  179. $level = substr_count($path, $config['path_separator']);
  180. $levelProp = $meta->getReflectionProperty($config['level']);
  181. $levelProp->setAccessible(true);
  182. $levelProp->setValue($node, $level);
  183. $changes[$config['level']] = array(null, $level);
  184. }
  185. $uow->scheduleExtraUpdate($node, $changes);
  186. $ea->setOriginalObjectProperty($uow, $oid, $config['path'], $path);
  187. }
  188. /**
  189. * Update $node 's children
  190. *
  191. * @param ObjectManager $om
  192. * @param object $node - target node
  193. * @param object $ea - event adapter
  194. * @param string $originalPath - original path of object
  195. * @return void
  196. */
  197. public function updateChildren(ObjectManager $om, $node, AdapterInterface $ea, $originalPath)
  198. {
  199. $meta = $om->getClassMetadata(get_class($node));
  200. $config = $this->listener->getConfiguration($om, $meta->name);
  201. $children = $this->getChildren($om, $meta, $config, $originalPath);
  202. foreach ($children as $child) {
  203. $this->updateNode($om, $child, $ea);
  204. }
  205. }
  206. /**
  207. * Remove node and its children
  208. *
  209. * @param ObjectManager $om
  210. * @param object $meta - Metadata
  211. * @param object $config - config
  212. * @param object $node - node to remove
  213. * @return void
  214. */
  215. abstract public function removeNode($om, $meta, $config, $node);
  216. /**
  217. * Returns children of the node with its original path
  218. *
  219. * @param ObjectManager $om
  220. * @param object $meta - Metadata
  221. * @param object $config - config
  222. * @param mixed $originalPath - original path of object
  223. * @return Doctrine\ODM\MongoDB\Cursor
  224. */
  225. abstract public function getChildren($om, $meta, $config, $originalPath);
  226. }