AbstractTreeRepository.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. abstract class AbstractTreeRepository extends DocumentRepository
  10. {
  11. /**
  12. * Tree listener on event manager
  13. *
  14. * @var AbstractTreeListener
  15. */
  16. protected $listener = null;
  17. /**
  18. * Repository utils
  19. */
  20. protected $repoUtils = null;
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function __construct(DocumentManager $em, UnitOfWork $uow, ClassMetadata $class)
  25. {
  26. parent::__construct($em, $uow, $class);
  27. $treeListener = null;
  28. foreach ($em->getEventManager()->getListeners() as $listeners) {
  29. foreach ($listeners as $listener) {
  30. if ($listener instanceof \Gedmo\Tree\TreeListener) {
  31. $treeListener = $listener;
  32. break;
  33. }
  34. }
  35. if ($treeListener) {
  36. break;
  37. }
  38. }
  39. if (is_null($treeListener)) {
  40. throw new \Gedmo\Exception\InvalidMappingException('This repository can be attached only to ODM MongoDB tree listener');
  41. }
  42. $this->listener = $treeListener;
  43. if (!$this->validate()) {
  44. throw new \Gedmo\Exception\InvalidMappingException('This repository cannot be used for tree type: ' . $treeListener->getStrategy($em, $class->name)->getName());
  45. }
  46. $this->repoUtils = new RepositoryUtils($this->dm, $this->getClassMetadata(), $this->listener, $this);
  47. }
  48. /**
  49. * Sets the RepositoryUtilsInterface instance
  50. *
  51. * @param \Gedmo\Tree\RepositoryUtilsInterface $repoUtils
  52. *
  53. * @return $this
  54. */
  55. public function setRepoUtils(RepositoryUtilsInterface $repoUtils)
  56. {
  57. $this->repoUtils = $repoUtils;
  58. return $this;
  59. }
  60. /**
  61. * Returns the RepositoryUtilsInterface instance
  62. *
  63. * @return \Gedmo\Tree\RepositoryUtilsInterface|null
  64. */
  65. public function getRepoUtils()
  66. {
  67. return $this->repoUtils;
  68. }
  69. /**
  70. * @see \Gedmo\Tree\RepositoryUtilsInterface::childrenHierarchy
  71. */
  72. public function childrenHierarchy($node = null, $direct = false, array $options = array())
  73. {
  74. return $this->repoUtils->childrenHierarchy($node, $direct, $options);
  75. }
  76. /**
  77. * @see \Gedmo\Tree\RepositoryUtilsInterface::buildTree
  78. */
  79. public function buildTree(array $nodes, array $options = array())
  80. {
  81. return $this->repoUtils->buildTree($nodes, $options);
  82. }
  83. /**
  84. * @see \Gedmo\Tree\RepositoryUtilsInterface::buildTreeArray
  85. */
  86. public function buildTreeArray(array $nodes)
  87. {
  88. return $this->repoUtils->buildTreeArray($nodes);
  89. }
  90. /**
  91. * Checks if current repository is right
  92. * for currently used tree strategy
  93. *
  94. * @return bool
  95. */
  96. abstract protected function validate();
  97. /**
  98. * Returns an array of nodes suitable for method buildTree
  99. *
  100. * @param object - Root node
  101. * @param bool - Obtain direct children?
  102. * @param array - Metadata configuration
  103. * @param array - Options
  104. *
  105. * @return array - Array of nodes
  106. */
  107. abstract public function getNodesHierarchy($node, $direct, array $config, array $options = array());
  108. }