mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 09:35:49 +02:00
Refactored cache tags and invalidation
This commit is contained in:
parent
08a1ce5f64
commit
b7af08503c
8 changed files with 159 additions and 63 deletions
|
@ -24,49 +24,52 @@ namespace App\EntityListeners;
|
||||||
|
|
||||||
use App\Entity\Base\AbstractDBElement;
|
use App\Entity\Base\AbstractDBElement;
|
||||||
use App\Entity\Base\AbstractStructuralDBElement;
|
use App\Entity\Base\AbstractStructuralDBElement;
|
||||||
use App\Entity\LabelSystem\LabelProfile;
|
|
||||||
use App\Entity\UserSystem\Group;
|
use App\Entity\UserSystem\Group;
|
||||||
use App\Entity\UserSystem\User;
|
use App\Entity\UserSystem\User;
|
||||||
use App\Services\UserSystem\UserCacheKeyGenerator;
|
use App\Services\Cache\ElementCacheTagGenerator;
|
||||||
use Doctrine\ORM\Event\LifecycleEventArgs;
|
use App\Services\Cache\UserCacheKeyGenerator;
|
||||||
|
use Doctrine\ORM\Event\PostPersistEventArgs;
|
||||||
|
use Doctrine\ORM\Event\PostRemoveEventArgs;
|
||||||
|
use Doctrine\ORM\Event\PostUpdateEventArgs;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
use function get_class;
|
|
||||||
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
||||||
|
|
||||||
class TreeCacheInvalidationListener
|
class TreeCacheInvalidationListener
|
||||||
{
|
{
|
||||||
public function __construct(protected TagAwareCacheInterface $cache, protected UserCacheKeyGenerator $keyGenerator)
|
public function __construct(
|
||||||
|
protected TagAwareCacheInterface $cache,
|
||||||
|
protected UserCacheKeyGenerator $keyGenerator,
|
||||||
|
protected ElementCacheTagGenerator $tagGenerator
|
||||||
|
)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
#[ORM\PostUpdate]
|
#[ORM\PostUpdate]
|
||||||
#[ORM\PostPersist]
|
#[ORM\PostPersist]
|
||||||
#[ORM\PostRemove]
|
#[ORM\PostRemove]
|
||||||
public function invalidate(AbstractDBElement $element, LifecycleEventArgs $event): void
|
public function invalidate(AbstractDBElement $element, PostUpdateEventArgs|PostPersistEventArgs|PostRemoveEventArgs $event): void
|
||||||
{
|
{
|
||||||
//If an element was changed, then invalidate all cached trees with this element class
|
//For all changes, we invalidate the cache for all elements of this class
|
||||||
if ($element instanceof AbstractStructuralDBElement || $element instanceof LabelProfile) {
|
$tags = [$this->tagGenerator->getElementTypeCacheTag($element)];
|
||||||
$secure_class_name = str_replace('\\', '_', $element::class);
|
|
||||||
$this->cache->invalidateTags([$secure_class_name]);
|
|
||||||
|
|
||||||
//Trigger a sidebar reload for all users (see SidebarTreeUpdater service)
|
|
||||||
if(!$element instanceof LabelProfile) {
|
//For changes on structural elements, we also invalidate the sidebar tree
|
||||||
$this->cache->invalidateTags(['sidebar_tree_update']);
|
if ($element instanceof AbstractStructuralDBElement) {
|
||||||
}
|
$tags[] = 'sidebar_tree_update';
|
||||||
}
|
}
|
||||||
|
|
||||||
//If a user change, then invalidate all cached trees for him
|
//For user changes, we invalidate the cache for this user
|
||||||
if ($element instanceof User) {
|
if ($element instanceof User) {
|
||||||
$secure_class_name = str_replace('\\', '_', $element::class);
|
$tags[] = $this->keyGenerator->generateKey($element);
|
||||||
$tag = $this->keyGenerator->generateKey($element);
|
|
||||||
$this->cache->invalidateTags([$tag, $secure_class_name]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If any group change, then invalidate all cached trees. Users Permissions can be inherited from groups,
|
/* If any group change, then invalidate all cached trees. Users Permissions can be inherited from groups,
|
||||||
so a change in any group can cause big permisssion changes for users. So to be sure, invalidate all trees */
|
so a change in any group can cause big permisssion changes for users. So to be sure, invalidate all trees */
|
||||||
if ($element instanceof Group) {
|
if ($element instanceof Group) {
|
||||||
$tag = 'groups';
|
$tags[] = 'groups';
|
||||||
$this->cache->invalidateTags([$tag]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Invalidate the cache for the given tags
|
||||||
|
$this->cache->invalidateTags($tags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
70
src/Services/Cache/ElementCacheTagGenerator.php
Normal file
70
src/Services/Cache/ElementCacheTagGenerator.php
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 - 2023 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
|
||||||
|
namespace App\Services\Cache;
|
||||||
|
|
||||||
|
use Doctrine\Persistence\Proxy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The purpose of this class is to generate cache tags for elements.
|
||||||
|
* E.g. to easily invalidate all caches for a given element type.
|
||||||
|
*/
|
||||||
|
class ElementCacheTagGenerator
|
||||||
|
{
|
||||||
|
private array $cache = [];
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a cache tag for the given element type, which can be used to invalidate all caches for this element type.
|
||||||
|
* @param string|object $element
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getElementTypeCacheTag(string|object $element): string
|
||||||
|
{
|
||||||
|
//Ensure that the given element is a class name
|
||||||
|
if (is_object($element)) {
|
||||||
|
$element = get_class($element);
|
||||||
|
} else { //And that the class exists
|
||||||
|
if (!class_exists($element)) {
|
||||||
|
throw new \InvalidArgumentException("The given class '$element' does not exist!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Check if the tag is already cached
|
||||||
|
if (isset($this->cache[$element])) {
|
||||||
|
return $this->cache[$element];
|
||||||
|
}
|
||||||
|
|
||||||
|
//If the element is a proxy, then get the real class name of the underlying object
|
||||||
|
if ($element instanceof Proxy || str_starts_with($element, 'Proxies\\')) {
|
||||||
|
$element = get_parent_class($element);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Replace all backslashes with underscores to prevent problems with the cache and save the result
|
||||||
|
$this->cache[$element] = str_replace('\\', '_', $element);
|
||||||
|
return $this->cache[$element];
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,12 +20,12 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Services\UserSystem;
|
namespace App\Services\Cache;
|
||||||
|
|
||||||
use Symfony\Bundle\SecurityBundle\Security;
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
|
||||||
use App\Entity\UserSystem\User;
|
use App\Entity\UserSystem\User;
|
||||||
use Locale;
|
use Locale;
|
||||||
|
use Symfony\Bundle\SecurityBundle\Security;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\RequestStack;
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
|
|
||||||
/**
|
/**
|
|
@ -24,6 +24,8 @@ declare(strict_types=1);
|
||||||
namespace App\Services\EDAIntegration;
|
namespace App\Services\EDAIntegration;
|
||||||
|
|
||||||
use App\Entity\Parts\Category;
|
use App\Entity\Parts\Category;
|
||||||
|
use App\EntityListeners\TreeCacheInvalidationListener;
|
||||||
|
use App\Services\Cache\ElementCacheTagGenerator;
|
||||||
use App\Services\Trees\NodesListBuilder;
|
use App\Services\Trees\NodesListBuilder;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Contracts\Cache\ItemInterface;
|
use Symfony\Contracts\Cache\ItemInterface;
|
||||||
|
@ -36,6 +38,7 @@ class KiCADHelper
|
||||||
private readonly NodesListBuilder $nodesListBuilder,
|
private readonly NodesListBuilder $nodesListBuilder,
|
||||||
private readonly TagAwareCacheInterface $kicadCache,
|
private readonly TagAwareCacheInterface $kicadCache,
|
||||||
private readonly EntityManagerInterface $em,
|
private readonly EntityManagerInterface $em,
|
||||||
|
private readonly ElementCacheTagGenerator $tagGenerator,
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -52,7 +55,7 @@ class KiCADHelper
|
||||||
{
|
{
|
||||||
return $this->kicadCache->get('kicad_categories', function (ItemInterface $item) {
|
return $this->kicadCache->get('kicad_categories', function (ItemInterface $item) {
|
||||||
//Invalidate the cache on category changes
|
//Invalidate the cache on category changes
|
||||||
$secure_class_name = str_replace('\\', '_', Category::class);
|
$secure_class_name = $this->tagGenerator->getElementTypeCacheTag(Category::class);
|
||||||
$item->tag($secure_class_name);
|
$item->tag($secure_class_name);
|
||||||
|
|
||||||
$categories = $this->nodesListBuilder->typeToNodesList(Category::class);
|
$categories = $this->nodesListBuilder->typeToNodesList(Category::class);
|
||||||
|
|
|
@ -44,15 +44,20 @@ namespace App\Services\LabelSystem;
|
||||||
use App\Entity\LabelSystem\LabelProfile;
|
use App\Entity\LabelSystem\LabelProfile;
|
||||||
use App\Entity\LabelSystem\LabelSupportedElement;
|
use App\Entity\LabelSystem\LabelSupportedElement;
|
||||||
use App\Repository\LabelProfileRepository;
|
use App\Repository\LabelProfileRepository;
|
||||||
use App\Services\UserSystem\UserCacheKeyGenerator;
|
use App\Services\Cache\ElementCacheTagGenerator;
|
||||||
|
use App\Services\Cache\UserCacheKeyGenerator;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Contracts\Cache\ItemInterface;
|
use Symfony\Contracts\Cache\ItemInterface;
|
||||||
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
||||||
|
|
||||||
final class LabelProfileDropdownHelper
|
final class LabelProfileDropdownHelper
|
||||||
{
|
{
|
||||||
public function __construct(private readonly TagAwareCacheInterface $cache, private readonly EntityManagerInterface $entityManager, private readonly UserCacheKeyGenerator $keyGenerator)
|
public function __construct(
|
||||||
{
|
private readonly TagAwareCacheInterface $cache,
|
||||||
|
private readonly EntityManagerInterface $entityManager,
|
||||||
|
private readonly UserCacheKeyGenerator $keyGenerator,
|
||||||
|
private readonly ElementCacheTagGenerator $tagGenerator,
|
||||||
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -67,7 +72,7 @@ final class LabelProfileDropdownHelper
|
||||||
$type = LabelSupportedElement::from($type);
|
$type = LabelSupportedElement::from($type);
|
||||||
}
|
}
|
||||||
|
|
||||||
$secure_class_name = str_replace('\\', '_', LabelProfile::class);
|
$secure_class_name = $this->tagGenerator->getElementTypeCacheTag(LabelProfile::class);
|
||||||
$key = 'profile_dropdown_'.$this->keyGenerator->generateKey().'_'.$secure_class_name.'_'.$type->value;
|
$key = 'profile_dropdown_'.$this->keyGenerator->generateKey().'_'.$secure_class_name.'_'.$type->value;
|
||||||
|
|
||||||
/** @var LabelProfileRepository $repo */
|
/** @var LabelProfileRepository $repo */
|
||||||
|
|
|
@ -22,14 +22,13 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Services\Trees;
|
namespace App\Services\Trees;
|
||||||
|
|
||||||
use App\Entity\Attachments\AttachmentContainingDBElement;
|
|
||||||
use App\Entity\Base\AbstractDBElement;
|
use App\Entity\Base\AbstractDBElement;
|
||||||
use App\Entity\Base\AbstractNamedDBElement;
|
|
||||||
use App\Entity\Base\AbstractStructuralDBElement;
|
use App\Entity\Base\AbstractStructuralDBElement;
|
||||||
use App\Repository\AttachmentContainingDBElementRepository;
|
use App\Repository\AttachmentContainingDBElementRepository;
|
||||||
use App\Repository\DBElementRepository;
|
use App\Repository\DBElementRepository;
|
||||||
use App\Repository\StructuralDBElementRepository;
|
use App\Repository\StructuralDBElementRepository;
|
||||||
use App\Services\UserSystem\UserCacheKeyGenerator;
|
use App\Services\Cache\ElementCacheTagGenerator;
|
||||||
|
use App\Services\Cache\UserCacheKeyGenerator;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Contracts\Cache\ItemInterface;
|
use Symfony\Contracts\Cache\ItemInterface;
|
||||||
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
||||||
|
@ -40,8 +39,12 @@ use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
||||||
*/
|
*/
|
||||||
class NodesListBuilder
|
class NodesListBuilder
|
||||||
{
|
{
|
||||||
public function __construct(protected EntityManagerInterface $em, protected TagAwareCacheInterface $cache, protected UserCacheKeyGenerator $keyGenerator)
|
public function __construct(
|
||||||
{
|
protected EntityManagerInterface $em,
|
||||||
|
protected TagAwareCacheInterface $cache,
|
||||||
|
protected UserCacheKeyGenerator $keyGenerator,
|
||||||
|
protected ElementCacheTagGenerator $tagGenerator,
|
||||||
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,9 +53,9 @@ class NodesListBuilder
|
||||||
*
|
*
|
||||||
* @template T of AbstractDBElement
|
* @template T of AbstractDBElement
|
||||||
*
|
*
|
||||||
* @param string $class_name the class name of the entity you want to retrieve
|
* @param string $class_name the class name of the entity you want to retrieve
|
||||||
* @phpstan-param class-string<T> $class_name
|
* @phpstan-param class-string<T> $class_name
|
||||||
* @param AbstractStructuralDBElement|null $parent This entity will be used as root element. Set to null, to use global root
|
* @param AbstractStructuralDBElement|null $parent This entity will be used as root element. Set to null, to use global root
|
||||||
*
|
*
|
||||||
* @return AbstractDBElement[] a flattened list containing the tree elements
|
* @return AbstractDBElement[] a flattened list containing the tree elements
|
||||||
* @phpstan-return list<T>
|
* @phpstan-return list<T>
|
||||||
|
@ -86,7 +89,7 @@ class NodesListBuilder
|
||||||
{
|
{
|
||||||
$parent_id = $parent instanceof AbstractStructuralDBElement ? $parent->getID() : '0';
|
$parent_id = $parent instanceof AbstractStructuralDBElement ? $parent->getID() : '0';
|
||||||
// Backslashes are not allowed in cache keys
|
// Backslashes are not allowed in cache keys
|
||||||
$secure_class_name = str_replace('\\', '_', $class_name);
|
$secure_class_name = $this->tagGenerator->getElementTypeCacheTag($class_name);
|
||||||
$key = 'list_'.$this->keyGenerator->generateKey().'_'.$secure_class_name.$parent_id;
|
$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) {
|
return $this->cache->get($key, function (ItemInterface $item) use ($class_name, $parent, $secure_class_name) {
|
||||||
|
@ -105,7 +108,7 @@ class NodesListBuilder
|
||||||
* The value is cached for performance reasons.
|
* The value is cached for performance reasons.
|
||||||
*
|
*
|
||||||
* @template T of AbstractStructuralDBElement
|
* @template T of AbstractStructuralDBElement
|
||||||
* @param T $element
|
* @param T $element
|
||||||
* @return AbstractStructuralDBElement[]
|
* @return AbstractStructuralDBElement[]
|
||||||
*
|
*
|
||||||
* @phpstan-return list<T>
|
* @phpstan-return list<T>
|
||||||
|
|
|
@ -22,9 +22,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Services\Trees;
|
namespace App\Services\Trees;
|
||||||
|
|
||||||
use Symfony\Bundle\SecurityBundle\Security;
|
|
||||||
use App\Entity\Attachments\AttachmentType;
|
use App\Entity\Attachments\AttachmentType;
|
||||||
use App\Entity\ProjectSystem\Project;
|
|
||||||
use App\Entity\LabelSystem\LabelProfile;
|
use App\Entity\LabelSystem\LabelProfile;
|
||||||
use App\Entity\Parts\Category;
|
use App\Entity\Parts\Category;
|
||||||
use App\Entity\Parts\Footprint;
|
use App\Entity\Parts\Footprint;
|
||||||
|
@ -34,10 +32,12 @@ use App\Entity\Parts\Part;
|
||||||
use App\Entity\Parts\StorageLocation;
|
use App\Entity\Parts\StorageLocation;
|
||||||
use App\Entity\Parts\Supplier;
|
use App\Entity\Parts\Supplier;
|
||||||
use App\Entity\PriceInformations\Currency;
|
use App\Entity\PriceInformations\Currency;
|
||||||
|
use App\Entity\ProjectSystem\Project;
|
||||||
use App\Entity\UserSystem\Group;
|
use App\Entity\UserSystem\Group;
|
||||||
use App\Entity\UserSystem\User;
|
use App\Entity\UserSystem\User;
|
||||||
use App\Helpers\Trees\TreeViewNode;
|
use App\Helpers\Trees\TreeViewNode;
|
||||||
use App\Services\UserSystem\UserCacheKeyGenerator;
|
use App\Services\Cache\UserCacheKeyGenerator;
|
||||||
|
use Symfony\Bundle\SecurityBundle\Security;
|
||||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||||
use Symfony\Contracts\Cache\ItemInterface;
|
use Symfony\Contracts\Cache\ItemInterface;
|
||||||
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
||||||
|
|
|
@ -25,17 +25,18 @@ namespace App\Services\Trees;
|
||||||
use App\Entity\Base\AbstractDBElement;
|
use App\Entity\Base\AbstractDBElement;
|
||||||
use App\Entity\Base\AbstractNamedDBElement;
|
use App\Entity\Base\AbstractNamedDBElement;
|
||||||
use App\Entity\Base\AbstractStructuralDBElement;
|
use App\Entity\Base\AbstractStructuralDBElement;
|
||||||
use App\Entity\ProjectSystem\Project;
|
|
||||||
use App\Entity\Parts\Category;
|
use App\Entity\Parts\Category;
|
||||||
use App\Entity\Parts\Footprint;
|
use App\Entity\Parts\Footprint;
|
||||||
use App\Entity\Parts\Manufacturer;
|
use App\Entity\Parts\Manufacturer;
|
||||||
use App\Entity\Parts\StorageLocation;
|
use App\Entity\Parts\StorageLocation;
|
||||||
use App\Entity\Parts\Supplier;
|
use App\Entity\Parts\Supplier;
|
||||||
|
use App\Entity\ProjectSystem\Project;
|
||||||
use App\Helpers\Trees\TreeViewNode;
|
use App\Helpers\Trees\TreeViewNode;
|
||||||
use App\Helpers\Trees\TreeViewNodeIterator;
|
use App\Helpers\Trees\TreeViewNodeIterator;
|
||||||
use App\Repository\StructuralDBElementRepository;
|
use App\Repository\StructuralDBElementRepository;
|
||||||
|
use App\Services\Cache\ElementCacheTagGenerator;
|
||||||
|
use App\Services\Cache\UserCacheKeyGenerator;
|
||||||
use App\Services\EntityURLGenerator;
|
use App\Services\EntityURLGenerator;
|
||||||
use App\Services\UserSystem\UserCacheKeyGenerator;
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use RecursiveIteratorIterator;
|
use RecursiveIteratorIterator;
|
||||||
|
@ -51,25 +52,37 @@ use function count;
|
||||||
*/
|
*/
|
||||||
class TreeViewGenerator
|
class TreeViewGenerator
|
||||||
{
|
{
|
||||||
public function __construct(protected EntityURLGenerator $urlGenerator, protected EntityManagerInterface $em, protected TagAwareCacheInterface $cache,
|
public function __construct(
|
||||||
protected UserCacheKeyGenerator $keyGenerator, protected TranslatorInterface $translator, private UrlGeneratorInterface $router,
|
protected EntityURLGenerator $urlGenerator,
|
||||||
protected bool $rootNodeExpandedByDefault, protected bool $rootNodeEnabled)
|
protected EntityManagerInterface $em,
|
||||||
{
|
protected TagAwareCacheInterface $cache,
|
||||||
|
protected ElementCacheTagGenerator $tagGenerator,
|
||||||
|
protected UserCacheKeyGenerator $keyGenerator,
|
||||||
|
protected TranslatorInterface $translator,
|
||||||
|
private UrlGeneratorInterface $router,
|
||||||
|
protected bool $rootNodeExpandedByDefault,
|
||||||
|
protected bool $rootNodeEnabled,
|
||||||
|
|
||||||
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a TreeView list for the entities of the given class.
|
* Gets a TreeView list for the entities of the given class.
|
||||||
*
|
*
|
||||||
* @param string $class The class for which the treeView should be generated
|
* @param string $class The class for which the treeView should be generated
|
||||||
* @param AbstractStructuralDBElement|null $parent The root nodes in the tree should have this element as parent (use null, if you want to get all entities)
|
* @param AbstractStructuralDBElement|null $parent The root nodes in the tree should have this element as parent (use null, if you want to get all entities)
|
||||||
* @param string $mode The link type that will be generated for the hyperlink section of each node (see EntityURLGenerator for possible values).
|
* @param string $mode The link type that will be generated for the hyperlink section of each node (see EntityURLGenerator for possible values).
|
||||||
* Set to empty string, to disable href field.
|
* Set to empty string, to disable href field.
|
||||||
* @param AbstractDBElement|null $selectedElement The element that should be selected. If set to null, no element will be selected.
|
* @param AbstractDBElement|null $selectedElement The element that should be selected. If set to null, no element will be selected.
|
||||||
*
|
*
|
||||||
* @return TreeViewNode[] an array of TreeViewNode[] elements of the root elements
|
* @return TreeViewNode[] an array of TreeViewNode[] elements of the root elements
|
||||||
*/
|
*/
|
||||||
public function getTreeView(string $class, ?AbstractStructuralDBElement $parent = null, string $mode = 'list_parts', ?AbstractDBElement $selectedElement = null): array
|
public function getTreeView(
|
||||||
{
|
string $class,
|
||||||
|
?AbstractStructuralDBElement $parent = null,
|
||||||
|
string $mode = 'list_parts',
|
||||||
|
?AbstractDBElement $selectedElement = null
|
||||||
|
): array {
|
||||||
$head = [];
|
$head = [];
|
||||||
|
|
||||||
$href_type = $mode;
|
$href_type = $mode;
|
||||||
|
@ -110,7 +123,7 @@ class TreeViewGenerator
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($item->getNodes() !== null && $item->getNodes() !== []) {
|
if ($item->getNodes() !== null && $item->getNodes() !== []) {
|
||||||
$item->addTag((string) count($item->getNodes()));
|
$item->addTag((string)count($item->getNodes()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($href_type !== '' && null !== $item->getId()) {
|
if ($href_type !== '' && null !== $item->getId()) {
|
||||||
|
@ -155,12 +168,12 @@ class TreeViewGenerator
|
||||||
{
|
{
|
||||||
$icon = "fa-fw fa-treeview fa-solid ";
|
$icon = "fa-fw fa-treeview fa-solid ";
|
||||||
return match ($class) {
|
return match ($class) {
|
||||||
Category::class => $icon . 'fa-tags',
|
Category::class => $icon.'fa-tags',
|
||||||
StorageLocation::class => $icon . 'fa-cube',
|
StorageLocation::class => $icon.'fa-cube',
|
||||||
Footprint::class => $icon . 'fa-microchip',
|
Footprint::class => $icon.'fa-microchip',
|
||||||
Manufacturer::class => $icon . 'fa-industry',
|
Manufacturer::class => $icon.'fa-industry',
|
||||||
Supplier::class => $icon . 'fa-truck',
|
Supplier::class => $icon.'fa-truck',
|
||||||
Project::class => $icon . 'fa-archive',
|
Project::class => $icon.'fa-archive',
|
||||||
default => null,
|
default => null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -170,8 +183,8 @@ class TreeViewGenerator
|
||||||
* Gets a tree of TreeViewNode elements. The root elements has $parent as parent.
|
* Gets a tree of TreeViewNode elements. The root elements has $parent as parent.
|
||||||
* The treeview is generic, that means the href are null and ID values are set.
|
* The treeview is generic, that means the href are null and ID values are set.
|
||||||
*
|
*
|
||||||
* @param string $class The class for which the tree should be generated
|
* @param string $class The class for which the tree should be generated
|
||||||
* @param AbstractStructuralDBElement|null $parent the parent the root elements should have
|
* @param AbstractStructuralDBElement|null $parent the parent the root elements should have
|
||||||
*
|
*
|
||||||
* @return TreeViewNode[]
|
* @return TreeViewNode[]
|
||||||
*/
|
*/
|
||||||
|
@ -192,13 +205,12 @@ class TreeViewGenerator
|
||||||
return $repo->getGenericNodeTree($parent);
|
return $repo->getGenericNodeTree($parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
$secure_class_name = str_replace('\\', '_', $class);
|
$secure_class_name = $this->tagGenerator->getElementTypeCacheTag($class);
|
||||||
$key = 'treeview_'.$this->keyGenerator->generateKey().'_'.$secure_class_name;
|
$key = 'treeview_'.$this->keyGenerator->generateKey().'_'.$secure_class_name;
|
||||||
|
|
||||||
return $this->cache->get($key, function (ItemInterface $item) use ($repo, $parent, $secure_class_name) {
|
return $this->cache->get($key, function (ItemInterface $item) use ($repo, $parent, $secure_class_name) {
|
||||||
// Invalidate when groups, an element with the class or the user changes
|
// Invalidate when groups, an element with the class or the user changes
|
||||||
$item->tag(['groups', 'tree_treeview', $this->keyGenerator->generateKey(), $secure_class_name]);
|
$item->tag(['groups', 'tree_treeview', $this->keyGenerator->generateKey(), $secure_class_name]);
|
||||||
|
|
||||||
return $repo->getGenericNodeTree($parent);
|
return $repo->getGenericNodeTree($parent);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue