AbstractMaterializedPath.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. use Gedmo\Exception\RuntimeException;
  11. /**
  12. * This strategy makes tree using materialized path strategy
  13. *
  14. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  15. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  16. * @package Gedmo.Tree.Strategy
  17. * @subpackage AbstractMaterializedPath
  18. * @link http://www.gediminasm.org
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. abstract class AbstractMaterializedPath implements Strategy
  22. {
  23. /**
  24. * TreeListener
  25. *
  26. * @var AbstractTreeListener
  27. */
  28. protected $listener = null;
  29. /**
  30. * Array of objects which were scheduled for path processes
  31. *
  32. * @var array
  33. */
  34. protected $scheduledForPathProcess = array();
  35. /**
  36. * Array of objects which were scheduled for path process.
  37. * This time, this array contains the objects with their ID
  38. * already set
  39. *
  40. * @var array
  41. */
  42. protected $scheduledForPathProcessWithIdSet = array();
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function __construct(TreeListener $listener)
  47. {
  48. $this->listener = $listener;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getName()
  54. {
  55. return Strategy::MATERIALIZED_PATH;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function processScheduledInsertion($om, $node, AdapterInterface $ea)
  61. {
  62. $meta = $om->getClassMetadata(get_class($node));
  63. $config = $this->listener->getConfiguration($om, $meta->name);
  64. $fieldMapping = $meta->getFieldMapping($config['path_source']);
  65. if ($meta->isIdentifier($config['path_source']) || $fieldMapping['type'] === 'string') {
  66. $this->scheduledForPathProcess[spl_object_hash($node)] = $node;
  67. } else {
  68. $this->updateNode($om, $node, $ea);
  69. }
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function processScheduledUpdate($om, $node, AdapterInterface $ea)
  75. {
  76. $meta = $om->getClassMetadata(get_class($node));
  77. $config = $this->listener->getConfiguration($om, $meta->name);
  78. $uow = $om->getUnitOfWork();
  79. $changeSet = $ea->getObjectChangeSet($uow, $node);
  80. if (isset($changeSet[$config['parent']]) || isset($changeSet[$config['path_source']])) {
  81. if (isset($changeSet[$config['path']])) {
  82. $originalPath = $changeSet[$config['path']][0];
  83. } else {
  84. $pathProp = $meta->getReflectionProperty($config['path']);
  85. $pathProp->setAccessible(true);
  86. $originalPath = $pathProp->getValue($node);
  87. }
  88. $this->updateNode($om, $node, $ea);
  89. $this->updateChildren($om, $node, $ea, $originalPath);
  90. }
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function processPostPersist($om, $node, AdapterInterface $ea)
  96. {
  97. $oid = spl_object_hash($node);
  98. if ($this->scheduledForPathProcess && array_key_exists($oid, $this->scheduledForPathProcess)) {
  99. $this->scheduledForPathProcessWithIdSet[$oid] = $node;
  100. unset($this->scheduledForPathProcess[$oid]);
  101. if (empty($this->scheduledForPathProcess)) {
  102. foreach ($this->scheduledForPathProcessWithIdSet as $oid => $node) {
  103. $this->updateNode($om, $node, $ea);
  104. unset($this->scheduledForPathProcessWithIdSet[$oid]);
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function onFlushEnd($om)
  113. {
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function processPreRemove($om, $node)
  119. {}
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function processPrePersist($om, $node)
  124. {}
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function processMetadataLoad($om, $meta)
  129. {}
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function processScheduledDelete($om, $node)
  134. {
  135. $meta = $om->getClassMetadata(get_class($node));
  136. $config = $this->listener->getConfiguration($om, $meta->name);
  137. $this->removeNode($om, $meta, $config, $node);
  138. }
  139. /**
  140. * Update the $node
  141. *
  142. * @param ObjectManager $om
  143. * @param object $node - target node
  144. * @param object $ea - event adapter
  145. * @return void
  146. */
  147. public function updateNode(ObjectManager $om, $node, AdapterInterface $ea)
  148. {
  149. $oid = spl_object_hash($node);
  150. $meta = $om->getClassMetadata(get_class($node));
  151. $config = $this->listener->getConfiguration($om, $meta->name);
  152. $uow = $om->getUnitOfWork();
  153. $parentProp = $meta->getReflectionProperty($config['parent']);
  154. $parentProp->setAccessible(true);
  155. $parent = $parentProp->getValue($node);
  156. $pathProp = $meta->getReflectionProperty($config['path']);
  157. $pathProp->setAccessible(true);
  158. $pathSourceProp = $meta->getReflectionProperty($config['path_source']);
  159. $pathSourceProp->setAccessible(true);
  160. $path = $pathSourceProp->getValue($node);
  161. // We need to avoid the presence of the path separator in the path source
  162. if (strpos($path, $config['path_separator']) !== false) {
  163. $msg = 'You can\'t use the Path separator ("%s") as a character for your PathSource field value.';
  164. throw new RuntimeException(sprintf($msg, $config['path_separator']));
  165. }
  166. $fieldMapping = $meta->getFieldMapping($config['path_source']);
  167. // If PathSource field is a string, we append the ID to the path
  168. if ($fieldMapping['type'] === 'string') {
  169. $path .= '-'.$meta->getIdentifierValue($node);
  170. }
  171. $path .= $config['path_separator'];
  172. if ($parent) {
  173. $changeSet = $uow->isScheduledForUpdate($parent) ? $ea->getObjectChangeSet($uow, $parent) : false;
  174. $pathOrPathSourceHasChanged = $changeSet && (isset($changeSet[$config['path_source']]) || isset($changeSet[$config['path']]));
  175. if ($pathOrPathSourceHasChanged || !$pathProp->getValue($parent)) {
  176. $this->updateNode($om, $parent, $ea);
  177. }
  178. $path = $pathProp->getValue($parent).$path;
  179. }
  180. $pathProp->setValue($node, $path);
  181. $changes = array(
  182. $config['path'] => array(null, $path)
  183. );
  184. if (isset($config['level'])) {
  185. $level = substr_count($path, $config['path_separator']);
  186. $levelProp = $meta->getReflectionProperty($config['level']);
  187. $levelProp->setAccessible(true);
  188. $levelProp->setValue($node, $level);
  189. $changes[$config['level']] = array(null, $level);
  190. }
  191. $uow->scheduleExtraUpdate($node, $changes);
  192. $ea->setOriginalObjectProperty($uow, $oid, $config['path'], $path);
  193. }
  194. /**
  195. * Update $node 's children
  196. *
  197. * @param ObjectManager $om
  198. * @param object $node - target node
  199. * @param object $ea - event adapter
  200. * @param string $originalPath - original path of object
  201. * @return void
  202. */
  203. public function updateChildren(ObjectManager $om, $node, AdapterInterface $ea, $originalPath)
  204. {
  205. $meta = $om->getClassMetadata(get_class($node));
  206. $config = $this->listener->getConfiguration($om, $meta->name);
  207. $children = $this->getChildren($om, $meta, $config, $originalPath);
  208. foreach ($children as $child) {
  209. $this->updateNode($om, $child, $ea);
  210. }
  211. }
  212. /**
  213. * Remove node and its children
  214. *
  215. * @param ObjectManager $om
  216. * @param object $meta - Metadata
  217. * @param object $config - config
  218. * @param object $node - node to remove
  219. * @return void
  220. */
  221. abstract public function removeNode($om, $meta, $config, $node);
  222. /**
  223. * Returns children of the node with its original path
  224. *
  225. * @param ObjectManager $om
  226. * @param object $meta - Metadata
  227. * @param object $config - config
  228. * @param mixed $originalPath - original path of object
  229. * @return Doctrine\ODM\MongoDB\Cursor
  230. */
  231. abstract public function getChildren($om, $meta, $config, $originalPath);
  232. }