AbstractMaterializedPath.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. use Gedmo\Exception\TreeLockingException;
  12. /**
  13. * This strategy makes tree using materialized path strategy
  14. *
  15. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  16. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  17. * @author <rocco@roccosportal.com>
  18. * @package Gedmo.Tree.Strategy
  19. * @subpackage AbstractMaterializedPath
  20. * @link http://www.gediminasm.org
  21. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  22. */
  23. abstract class AbstractMaterializedPath implements Strategy
  24. {
  25. const ACTION_INSERT = 'insert';
  26. const ACTION_UPDATE = 'update';
  27. const ACTION_REMOVE = 'remove';
  28. /**
  29. * TreeListener
  30. *
  31. * @var AbstractTreeListener
  32. */
  33. protected $listener = null;
  34. /**
  35. * Array of objects which were scheduled for path processes
  36. *
  37. * @var array
  38. */
  39. protected $scheduledForPathProcess = array();
  40. /**
  41. * Array of objects which were scheduled for path process.
  42. * This time, this array contains the objects with their ID
  43. * already set
  44. *
  45. * @var array
  46. */
  47. protected $scheduledForPathProcessWithIdSet = array();
  48. /**
  49. * Roots of trees which needs to be locked
  50. *
  51. * @var array
  52. */
  53. protected $rootsOfTreesWhichNeedsLocking = array();
  54. /**
  55. * Objects which are going to be inserted (set only if tree locking is used)
  56. *
  57. * @var array
  58. */
  59. protected $pendingObjectsToInsert = array();
  60. /**
  61. * Objects which are going to be updated (set only if tree locking is used)
  62. *
  63. * @var array
  64. */
  65. protected $pendingObjectsToUpdate = array();
  66. /**
  67. * Objects which are going to be removed (set only if tree locking is used)
  68. *
  69. * @var array
  70. */
  71. protected $pendingObjectsToRemove = array();
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function __construct(TreeListener $listener)
  76. {
  77. $this->listener = $listener;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getName()
  83. {
  84. return Strategy::MATERIALIZED_PATH;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function processScheduledInsertion($om, $node, AdapterInterface $ea)
  90. {
  91. $meta = $om->getClassMetadata(get_class($node));
  92. $config = $this->listener->getConfiguration($om, $meta->name);
  93. $fieldMapping = $meta->getFieldMapping($config['path_source']);
  94. if ($meta->isIdentifier($config['path_source']) || $fieldMapping['type'] === 'string') {
  95. $this->scheduledForPathProcess[spl_object_hash($node)] = $node;
  96. } else {
  97. $this->updateNode($om, $node, $ea);
  98. }
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function processScheduledUpdate($om, $node, AdapterInterface $ea)
  104. {
  105. $meta = $om->getClassMetadata(get_class($node));
  106. $config = $this->listener->getConfiguration($om, $meta->name);
  107. $uow = $om->getUnitOfWork();
  108. $changeSet = $ea->getObjectChangeSet($uow, $node);
  109. if (isset($changeSet[$config['parent']]) || isset($changeSet[$config['path_source']])) {
  110. if (isset($changeSet[$config['path']])) {
  111. $originalPath = $changeSet[$config['path']][0];
  112. } else {
  113. $pathProp = $meta->getReflectionProperty($config['path']);
  114. $pathProp->setAccessible(true);
  115. $originalPath = $pathProp->getValue($node);
  116. }
  117. $this->updateNode($om, $node, $ea);
  118. $this->updateChildren($om, $node, $ea, $originalPath);
  119. }
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function processPostPersist($om, $node, AdapterInterface $ea)
  125. {
  126. $oid = spl_object_hash($node);
  127. if ($this->scheduledForPathProcess && array_key_exists($oid, $this->scheduledForPathProcess)) {
  128. $this->scheduledForPathProcessWithIdSet[$oid] = $node;
  129. unset($this->scheduledForPathProcess[$oid]);
  130. if (empty($this->scheduledForPathProcess)) {
  131. foreach ($this->scheduledForPathProcessWithIdSet as $oid => $node) {
  132. $this->updateNode($om, $node, $ea);
  133. unset($this->scheduledForPathProcessWithIdSet[$oid]);
  134. }
  135. }
  136. }
  137. $this->processPostEventsActions($om, $ea, $node, self::ACTION_INSERT);
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function processPostUpdate($om, $node, AdapterInterface $ea)
  143. {
  144. $this->processPostEventsActions($om, $ea, $node, self::ACTION_UPDATE);
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function processPostRemove($om, $node, AdapterInterface $ea)
  150. {
  151. $this->processPostEventsActions($om, $ea, $node, self::ACTION_REMOVE);
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function onFlushEnd($om, AdapterInterface $ea)
  157. {
  158. $this->lockTrees($om, $ea);
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function processPreRemove($om, $node)
  164. {
  165. $this->processPreLockingActions($om, $node, self::ACTION_REMOVE);
  166. }
  167. /**
  168. * {@inheritdoc}
  169. */
  170. public function processPrePersist($om, $node)
  171. {
  172. $this->processPreLockingActions($om, $node, self::ACTION_INSERT);
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function processPreUpdate($om, $node)
  178. {
  179. $this->processPreLockingActions($om, $node, self::ACTION_UPDATE);
  180. }
  181. /**
  182. * {@inheritdoc}
  183. */
  184. public function processMetadataLoad($om, $meta)
  185. {}
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function processScheduledDelete($om, $node)
  190. {
  191. $meta = $om->getClassMetadata(get_class($node));
  192. $config = $this->listener->getConfiguration($om, $meta->name);
  193. $this->removeNode($om, $meta, $config, $node);
  194. }
  195. /**
  196. * Update the $node
  197. *
  198. * @param ObjectManager $om
  199. * @param object $node - target node
  200. * @param object $ea - event adapter
  201. * @return void
  202. */
  203. public function updateNode(ObjectManager $om, $node, AdapterInterface $ea)
  204. {
  205. $oid = spl_object_hash($node);
  206. $meta = $om->getClassMetadata(get_class($node));
  207. $config = $this->listener->getConfiguration($om, $meta->name);
  208. $uow = $om->getUnitOfWork();
  209. $parentProp = $meta->getReflectionProperty($config['parent']);
  210. $parentProp->setAccessible(true);
  211. $parent = $parentProp->getValue($node);
  212. $pathProp = $meta->getReflectionProperty($config['path']);
  213. $pathProp->setAccessible(true);
  214. $pathSourceProp = $meta->getReflectionProperty($config['path_source']);
  215. $pathSourceProp->setAccessible(true);
  216. $path = $pathSourceProp->getValue($node);
  217. // We need to avoid the presence of the path separator in the path source
  218. if (strpos($path, $config['path_separator']) !== false) {
  219. $msg = 'You can\'t use the Path separator ("%s") as a character for your PathSource field value.';
  220. throw new RuntimeException(sprintf($msg, $config['path_separator']));
  221. }
  222. $fieldMapping = $meta->getFieldMapping($config['path_source']);
  223. // default behavior: if PathSource field is a string, we append the ID to the path
  224. // path_append_id is true: always append id
  225. // path_append_id is false: never append id
  226. if ($config['path_append_id'] === true || ($fieldMapping['type'] === 'string' && $config['path_append_id']!==false)) {
  227. if (method_exists($meta, 'getIdentifierValue')) {
  228. $identifier = $meta->getIdentifierValue($node);
  229. } else {
  230. $identifierProp = $meta->getReflectionProperty($meta->getSingleIdentifierFieldName());
  231. $identifierProp->setAccessible(true);
  232. $identifier = $identifierProp->getValue($node);
  233. }
  234. $path .= '-'.$identifier;
  235. }
  236. if ($parent) {
  237. // Ensure parent has been initialized in the case where it's a proxy
  238. $om->initializeObject($parent);
  239. $changeSet = $uow->isScheduledForUpdate($parent) ? $ea->getObjectChangeSet($uow, $parent) : false;
  240. $pathOrPathSourceHasChanged = $changeSet && (isset($changeSet[$config['path_source']]) || isset($changeSet[$config['path']]));
  241. if ($pathOrPathSourceHasChanged || !$pathProp->getValue($parent)) {
  242. $this->updateNode($om, $parent, $ea);
  243. }
  244. $parentPath = $pathProp->getValue($parent);
  245. // if parent path not ends with separator
  246. if ($parentPath[strlen($parentPath) - 1] !== $config['path_separator']) {
  247. // add separator
  248. $path = $pathProp->getValue($parent) . $config['path_separator'] . $path;
  249. } else {
  250. // don't add separator
  251. $path = $pathProp->getValue($parent) . $path;
  252. }
  253. }
  254. if ($config['path_starts_with_separator'] && (strlen($path) > 0 && $path[0] !== $config['path_separator'])) {
  255. $path = $config['path_separator'] . $path;
  256. }
  257. if ($config['path_ends_with_separator'] && ($path[strlen($path) - 1] !== $config['path_separator'])) {
  258. $path .= $config['path_separator'];
  259. }
  260. $pathProp->setValue($node, $path);
  261. $changes = array(
  262. $config['path'] => array(null, $path)
  263. );
  264. if (isset($config['path_hash'])) {
  265. $pathHash = md5($path);
  266. $pathHashProp = $meta->getReflectionProperty($config['path_hash']);
  267. $pathHashProp->setAccessible(true);
  268. $pathHashProp->setValue($node, $pathHash);
  269. $changes[$config['path_hash']] = array(null, $pathHash);
  270. }
  271. if (isset($config['level'])) {
  272. $level = substr_count($path, $config['path_separator']);
  273. $levelProp = $meta->getReflectionProperty($config['level']);
  274. $levelProp->setAccessible(true);
  275. $levelProp->setValue($node, $level);
  276. $changes[$config['level']] = array(null, $level);
  277. }
  278. $uow->scheduleExtraUpdate($node, $changes);
  279. $ea->setOriginalObjectProperty($uow, $oid, $config['path'], $path);
  280. if(isset($config['path_hash'])){
  281. $ea->setOriginalObjectProperty($uow, $oid, $config['path_hash'], $pathHash);
  282. }
  283. }
  284. /**
  285. * Update node's children
  286. *
  287. * @param ObjectManager $om
  288. * @param object $node
  289. * @param AdapterInterface $ea
  290. * @param string $originalPath
  291. * @return void
  292. */
  293. public function updateChildren(ObjectManager $om, $node, AdapterInterface $ea, $originalPath)
  294. {
  295. $meta = $om->getClassMetadata(get_class($node));
  296. $config = $this->listener->getConfiguration($om, $meta->name);
  297. $children = $this->getChildren($om, $meta, $config, $originalPath);
  298. foreach ($children as $child) {
  299. $this->updateNode($om, $child, $ea);
  300. }
  301. }
  302. /**
  303. * Process pre-locking actions
  304. *
  305. * @param ObjectManager $om
  306. * @param object $node
  307. * @param string $action
  308. * @return void
  309. */
  310. public function processPreLockingActions($om, $node, $action)
  311. {
  312. $meta = $om->getClassMetadata(get_class($node));
  313. $config = $this->listener->getConfiguration($om, $meta->name);
  314. if ($config['activate_locking']) {;
  315. $parentProp = $meta->getReflectionProperty($config['parent']);
  316. $parentProp->setAccessible(true);
  317. $parentNode = $node;
  318. while (!is_null($parent = $parentProp->getValue($parentNode))) {
  319. $parentNode = $parent;
  320. }
  321. // In some cases, the parent could be a not initialized proxy. In this case, the
  322. // "lockTime" field may NOT be loaded yet and have null instead of the date.
  323. // We need to be sure that this field has its real value
  324. if ($parentNode !== $node && $parentNode instanceof \Doctrine\ODM\MongoDB\Proxy\Proxy) {
  325. $reflMethod = new \ReflectionMethod(get_class($parentNode), '__load');
  326. $reflMethod->setAccessible(true);
  327. $reflMethod->invoke($parentNode);
  328. }
  329. // If tree is already locked, we throw an exception
  330. $lockTimeProp = $meta->getReflectionProperty($config['lock_time']);
  331. $lockTimeProp->setAccessible(true);
  332. $lockTime = $lockTimeProp->getValue($parentNode);
  333. if (!is_null($lockTime)) {
  334. $lockTime = $lockTime instanceof \MongoDate ? $lockTime->sec : $lockTime->getTimestamp();
  335. }
  336. if (!is_null($lockTime) && ($lockTime >= (time() - $config['locking_timeout']))) {
  337. $msg = 'Tree with root id "%s" is locked.';
  338. $id = $meta->getIdentifierValue($parentNode);
  339. throw new TreeLockingException(sprintf($msg, $id));
  340. }
  341. $this->rootsOfTreesWhichNeedsLocking[spl_object_hash($parentNode)] = $parentNode;
  342. $oid = spl_object_hash($node);
  343. switch ($action) {
  344. case self::ACTION_INSERT:
  345. $this->pendingObjectsToInsert[$oid] = $node;
  346. break;
  347. case self::ACTION_UPDATE:
  348. $this->pendingObjectsToUpdate[$oid] = $node;
  349. break;
  350. case self::ACTION_REMOVE:
  351. $this->pendingObjectsToRemove[$oid] = $node;
  352. break;
  353. default:
  354. throw new \InvalidArgumentException(sprintf('"%s" is not a valid action.', $action));
  355. }
  356. }
  357. }
  358. /**
  359. * Process pre-locking actions
  360. *
  361. * @param ObjectManager $om
  362. * @param AdapterInterface $ea
  363. * @param object $node
  364. * @param string $action
  365. * @return void
  366. */
  367. public function processPostEventsActions(ObjectManager $om, AdapterInterface $ea, $node, $action)
  368. {
  369. $meta = $om->getClassMetadata(get_class($node));
  370. $config = $this->listener->getConfiguration($om, $meta->name);
  371. if ($config['activate_locking']) {
  372. switch ($action) {
  373. case self::ACTION_INSERT:
  374. unset($this->pendingObjectsToInsert[spl_object_hash($node)]);
  375. break;
  376. case self::ACTION_UPDATE:
  377. unset($this->pendingObjectsToUpdate[spl_object_hash($node)]);
  378. break;
  379. case self::ACTION_REMOVE:
  380. unset($this->pendingObjectsToRemove[spl_object_hash($node)]);
  381. break;
  382. default:
  383. throw new \InvalidArgumentException(sprintf('"%s" is not a valid action.', $action));
  384. }
  385. if (empty($this->pendingObjectsToInsert) && empty($this->pendingObjectsToUpdate) &&
  386. empty($this->pendingObjectsToRemove)) {
  387. $this->releaseTreeLocks($om, $ea);
  388. }
  389. }
  390. }
  391. /**
  392. * Locks all needed trees
  393. *
  394. * @param ObjectManager $om
  395. * @param AdapterInterface $ea
  396. * @return void
  397. */
  398. protected function lockTrees(ObjectManager $om, AdapterInterface $ea)
  399. {
  400. // Do nothing by default
  401. }
  402. /**
  403. * Releases all trees which are locked
  404. *
  405. * @param ObjectManager $om
  406. * @param AdapterInterface $ea
  407. * @return void
  408. */
  409. protected function releaseTreeLocks(ObjectManager $om, AdapterInterface $ea)
  410. {
  411. // Do nothing by default
  412. }
  413. /**
  414. * Remove node and its children
  415. *
  416. * @param ObjectManager $om
  417. * @param object $meta - Metadata
  418. * @param object $config - config
  419. * @param object $node - node to remove
  420. * @return void
  421. */
  422. abstract public function removeNode($om, $meta, $config, $node);
  423. /**
  424. * Returns children of the node with its original path
  425. *
  426. * @param ObjectManager $om
  427. * @param object $meta - Metadata
  428. * @param object $config - config
  429. * @param string $originalPath - original path of object
  430. * @return Doctrine\ODM\MongoDB\Cursor
  431. */
  432. abstract public function getChildren($om, $meta, $config, $originalPath);
  433. }