AbstractTreeRepository.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Gedmo\Tree\Entity\Repository;
  3. use Doctrine\ORM\EntityRepository,
  4. Doctrine\ORM\EntityManager,
  5. Doctrine\ORM\Mapping\ClassMetadata,
  6. Gedmo\Tool\Wrapper\EntityWrapper,
  7. Gedmo\Tree\RepositoryUtils,
  8. Gedmo\Tree\RepositoryUtilsInterface,
  9. Gedmo\Tree\RepositoryInterface;
  10. abstract class AbstractTreeRepository extends EntityRepository 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(EntityManager $em, ClassMetadata $class)
  26. {
  27. parent::__construct($em, $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('Tree listener was not found on your entity manager, it must be hooked into the event manager');
  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->_em, $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. * @see \Gedmo\Tree\RepositoryUtilsInterface::childrenHierarchy
  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. * @see \Gedmo\Tree\RepositoryUtilsInterface::buildTree
  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::buildTreeArray
  86. */
  87. public function buildTreeArray(array $nodes)
  88. {
  89. return $this->repoUtils->buildTreeArray($nodes);
  90. }
  91. /**
  92. * Checks if current repository is right
  93. * for currently used tree strategy
  94. *
  95. * @return bool
  96. */
  97. abstract protected function validate();
  98. }