AbstractTreeRepository.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Gedmo\Tree\Document\MongoDB\Repository;
  3. use Doctrine\ODM\MongoDB\DocumentRepository,
  4. Doctrine\ODM\MongoDB\DocumentManager,
  5. Doctrine\ODM\MongoDB\Mapping\ClassMetadata,
  6. Doctrine\ODM\MongoDB\UnitOfWork,
  7. Gedmo\Tree\RepositoryUtils,
  8. Gedmo\Tree\RepositoryUtilsInterface,
  9. Gedmo\Tree\RepositoryInterface;
  10. abstract class AbstractTreeRepository extends DocumentRepository implements RepositoryInterface
  11. {
  12. /**
  13. * Tree listener on event manager
  14. *
  15. * @var AbstractTreeListener
  16. */
  17. protected $listener = null;
  18. /**
  19. * Repository utils
  20. */
  21. protected $repoUtils = null;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function __construct(DocumentManager $em, UnitOfWork $uow, ClassMetadata $class)
  26. {
  27. parent::__construct($em, $uow, $class);
  28. $treeListener = null;
  29. foreach ($em->getEventManager()->getListeners() as $listeners) {
  30. foreach ($listeners as $listener) {
  31. if ($listener instanceof \Gedmo\Tree\TreeListener) {
  32. $treeListener = $listener;
  33. break;
  34. }
  35. }
  36. if ($treeListener) {
  37. break;
  38. }
  39. }
  40. if (is_null($treeListener)) {
  41. throw new \Gedmo\Exception\InvalidMappingException('This repository can be attached only to ODM MongoDB tree listener');
  42. }
  43. $this->listener = $treeListener;
  44. if (!$this->validate()) {
  45. throw new \Gedmo\Exception\InvalidMappingException('This repository cannot be used for tree type: ' . $treeListener->getStrategy($em, $class->name)->getName());
  46. }
  47. $this->repoUtils = new RepositoryUtils($this->dm, $this->getClassMetadata(), $this->listener, $this);
  48. }
  49. /**
  50. * Sets the RepositoryUtilsInterface instance
  51. *
  52. * @param \Gedmo\Tree\RepositoryUtilsInterface $repoUtils
  53. *
  54. * @return $this
  55. */
  56. public function setRepoUtils(RepositoryUtilsInterface $repoUtils)
  57. {
  58. $this->repoUtils = $repoUtils;
  59. return $this;
  60. }
  61. /**
  62. * Returns the RepositoryUtilsInterface instance
  63. *
  64. * @return \Gedmo\Tree\RepositoryUtilsInterface|null
  65. */
  66. public function getRepoUtils()
  67. {
  68. return $this->repoUtils;
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public function childrenHierarchy($node = null, $direct = false, array $options = array(), $includeNode = false)
  74. {
  75. return $this->repoUtils->childrenHierarchy($node, $direct, $options, $includeNode);
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. public function buildTree(array $nodes, array $options = array())
  81. {
  82. return $this->repoUtils->buildTree($nodes, $options);
  83. }
  84. /**
  85. * @see \Gedmo\Tree\RepositoryUtilsInterface::setChildrenIndex
  86. */
  87. public function setChildrenIndex($childrenIndex)
  88. {
  89. $this->repoUtils->setChildrenIndex($childrenIndex);
  90. }
  91. /**
  92. * @see \Gedmo\Tree\RepositoryUtilsInterface::getChildrenIndex
  93. */
  94. public function getChildrenIndex()
  95. {
  96. return $this->repoUtils->getChildrenIndex();
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public function buildTreeArray(array $nodes)
  102. {
  103. return $this->repoUtils->buildTreeArray($nodes);
  104. }
  105. /**
  106. * Checks if current repository is right
  107. * for currently used tree strategy
  108. *
  109. * @return bool
  110. */
  111. abstract protected function validate();
  112. /**
  113. * Get all root nodes query builder
  114. *
  115. * @param string - Sort by field
  116. * @param string - Sort direction ("asc" or "desc")
  117. *
  118. * @return \Doctrine\MongoDB\Query\Builder - QueryBuilder object
  119. */
  120. abstract public function getRootNodesQueryBuilder($sortByField = null, $direction = 'asc');
  121. /**
  122. * Get all root nodes query
  123. *
  124. * @param string - Sort by field
  125. * @param string - Sort direction ("asc" or "desc")
  126. *
  127. * @return \Doctrine\MongoDB\Query\Query - Query object
  128. */
  129. abstract public function getRootNodesQuery($sortByField = null, $direction = 'asc');
  130. /**
  131. * Returns a QueryBuilder configured to return an array of nodes suitable for buildTree method
  132. *
  133. * @param object $node - Root node
  134. * @param bool $direct - Obtain direct children?
  135. * @param array $options - Options
  136. * @param boolean $includeNode - Include node in results?
  137. *
  138. * @return \Doctrine\MongoDB\Query\Builder - QueryBuilder object
  139. */
  140. abstract public function getNodesHierarchyQueryBuilder($node = null, $direct = false, array $options = array(), $includeNode = false);
  141. /**
  142. * Returns a Query configured to return an array of nodes suitable for buildTree method
  143. *
  144. * @param object $node - Root node
  145. * @param bool $direct - Obtain direct children?
  146. * @param array $options - Options
  147. * @param boolean $includeNode - Include node in results?
  148. *
  149. * @return \Doctrine\MongoDB\Query\Query - Query object
  150. */
  151. abstract public function getNodesHierarchyQuery($node = null, $direct = false, array $options = array(), $includeNode = false);
  152. /**
  153. * Get list of children followed by given $node. This returns a QueryBuilder object
  154. *
  155. * @param object $node - if null, all tree nodes will be taken
  156. * @param boolean $direct - true to take only direct children
  157. * @param string $sortByField - field name to sort by
  158. * @param string $direction - sort direction : "ASC" or "DESC"
  159. * @param bool $includeNode - Include the root node in results?
  160. *
  161. * @return \Doctrine\MongoDB\Query\Builder - QueryBuilder object
  162. */
  163. abstract public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false);
  164. /**
  165. * Get list of children followed by given $node. This returns a Query
  166. *
  167. * @param object $node - if null, all tree nodes will be taken
  168. * @param boolean $direct - true to take only direct children
  169. * @param string $sortByField - field name to sort by
  170. * @param string $direction - sort direction : "ASC" or "DESC"
  171. * @param bool $includeNode - Include the root node in results?
  172. *
  173. * @return \Doctrine\MongoDB\Query\Query - Query object
  174. */
  175. abstract public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false);
  176. }