2020-01-02 22:55:28 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-01-02 22:55:28 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Trees;
|
|
|
|
|
2020-02-01 19:48:07 +01:00
|
|
|
use App\Entity\Base\AbstractStructuralDBElement;
|
2020-01-02 22:55:28 +01:00
|
|
|
use App\Repository\StructuralDBElementRepository;
|
|
|
|
use App\Services\UserCacheKeyGenerator;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Symfony\Contracts\Cache\ItemInterface;
|
|
|
|
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This service gives you a flat list containing all structured entities in the order of the structure.
|
|
|
|
*/
|
|
|
|
class NodesListBuilder
|
|
|
|
{
|
2022-09-18 22:59:31 +02:00
|
|
|
protected EntityManagerInterface $em;
|
|
|
|
protected TagAwareCacheInterface $cache;
|
|
|
|
protected UserCacheKeyGenerator $keyGenerator;
|
2020-01-02 22:55:28 +01:00
|
|
|
|
2020-01-04 20:24:09 +01:00
|
|
|
public function __construct(EntityManagerInterface $em, TagAwareCacheInterface $treeCache, UserCacheKeyGenerator $keyGenerator)
|
2020-01-02 22:55:28 +01:00
|
|
|
{
|
|
|
|
$this->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.
|
|
|
|
*
|
2020-08-21 21:36:22 +02:00
|
|
|
* @param string $class_name the class name of the entity you want to retrieve
|
2020-02-01 19:48:07 +01:00
|
|
|
* @param AbstractStructuralDBElement|null $parent This entity will be used as root element. Set to null, to use global root
|
2020-01-02 22:55:28 +01:00
|
|
|
*
|
2020-08-21 21:36:22 +02:00
|
|
|
* @return AbstractStructuralDBElement[] a flattened list containing the tree elements
|
2020-01-02 22:55:28 +01:00
|
|
|
*/
|
2020-02-01 19:48:07 +01:00
|
|
|
public function typeToNodesList(string $class_name, ?AbstractStructuralDBElement $parent = null): array
|
2020-01-02 22:55:28 +01:00
|
|
|
{
|
2020-01-05 15:46:58 +01:00
|
|
|
$parent_id = null !== $parent ? $parent->getID() : '0';
|
2020-01-02 22:55:28 +01:00
|
|
|
// Backslashes are not allowed in cache keys
|
|
|
|
$secure_class_name = str_replace('\\', '_', $class_name);
|
|
|
|
$key = 'list_'.$this->keyGenerator->generateKey().'_'.$secure_class_name.$parent_id;
|
|
|
|
|
2020-01-05 15:46:58 +01:00
|
|
|
return $this->cache->get($key, function (ItemInterface $item) use ($class_name, $parent, $secure_class_name) {
|
2020-01-02 22:55:28 +01:00
|
|
|
// Invalidate when groups, a element with the class or the user changes
|
|
|
|
$item->tag(['groups', 'tree_list', $this->keyGenerator->generateKey(), $secure_class_name]);
|
2022-09-18 23:44:44 +02:00
|
|
|
/** @var StructuralDBElementRepository $repo */
|
|
|
|
$repo = $this->em->getRepository($class_name);
|
|
|
|
return $repo->toNodesList($parent);
|
2020-01-02 22:55:28 +01:00
|
|
|
});
|
|
|
|
}
|
2022-08-21 01:34:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a flattened list of all (recursive) children elements of the given AbstractStructuralDBElement.
|
|
|
|
* The value is cached for performance reasons.
|
|
|
|
*
|
2022-09-11 19:14:16 +02:00
|
|
|
* @template T of AbstractStructuralDBElement
|
2022-08-21 01:34:17 +02:00
|
|
|
* @param T $element
|
|
|
|
* @return T[]
|
|
|
|
*/
|
|
|
|
public function getChildrenFlatList(AbstractStructuralDBElement $element): array
|
|
|
|
{
|
|
|
|
return $this->typeToNodesList(get_class($element), $element);
|
|
|
|
}
|
2020-01-02 22:55:28 +01:00
|
|
|
}
|