em = $em; $this->keyGenerator = $keyGenerator; $this->cache = $treeCache; } /** * Gets a flattened hierachical tree. Useful for generating option lists. * In difference to the Repository Function, the results here are cached. * * @param string $class_name The class name of the entity you want to retrieve. * @param AbstractStructuralDBElement|null $parent This entity will be used as root element. Set to null, to use global root * * @return AbstractStructuralDBElement[] A flattened list containing the tree elements. */ public function typeToNodesList(string $class_name, ?AbstractStructuralDBElement $parent = null): array { $parent_id = null !== $parent ? $parent->getID() : '0'; // Backslashes are not allowed in cache keys $secure_class_name = str_replace('\\', '_', $class_name); $key = 'list_'.$this->keyGenerator->generateKey().'_'.$secure_class_name.$parent_id; return $this->cache->get($key, function (ItemInterface $item) use ($class_name, $parent, $secure_class_name) { // Invalidate when groups, a element with the class or the user changes $item->tag(['groups', 'tree_list', $this->keyGenerator->generateKey(), $secure_class_name]); /** @var StructuralDBElementRepository */ $repo = $this->em->getRepository($class_name); return $repo->toNodesList($parent); }); } }