From 6b83c772cc263716e07706a8db9534f10970eff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 15 Feb 2026 00:28:40 +0100 Subject: [PATCH] Moved user twig functions requiring repo access to its own extension service --- src/Twig/UserExtension.php | 23 +------------- src/Twig/UserRepoExtension.php | 56 ++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 22 deletions(-) create mode 100644 src/Twig/UserRepoExtension.php diff --git a/src/Twig/UserExtension.php b/src/Twig/UserExtension.php index 3cf76e63..81ff0857 100644 --- a/src/Twig/UserExtension.php +++ b/src/Twig/UserExtension.php @@ -43,11 +43,7 @@ namespace App\Twig; use Twig\Attribute\AsTwigFilter; use Twig\Attribute\AsTwigFunction; -use App\Entity\Base\AbstractDBElement; use App\Entity\UserSystem\User; -use App\Entity\LogSystem\AbstractLogEntry; -use App\Repository\LogEntryRepository; -use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken; @@ -58,28 +54,11 @@ use Symfony\Component\Security\Core\Exception\AccessDeniedException; */ final readonly class UserExtension { - private LogEntryRepository $repo; - public function __construct(EntityManagerInterface $em, + public function __construct( private Security $security, private UrlGeneratorInterface $urlGenerator) { - $this->repo = $em->getRepository(AbstractLogEntry::class); - } - - /** - * Returns the user which has edited the given entity the last time. - */ - #[AsTwigFunction('last_editing_user')] - public function lastEditingUser(AbstractDBElement $element): ?User - { - return $this->repo->getLastEditingUser($element); - } - - #[AsTwigFunction('creating_user')] - public function creatingUser(AbstractDBElement $element): ?User - { - return $this->repo->getCreatingUser($element); } /** diff --git a/src/Twig/UserRepoExtension.php b/src/Twig/UserRepoExtension.php new file mode 100644 index 00000000..51ac113c --- /dev/null +++ b/src/Twig/UserRepoExtension.php @@ -0,0 +1,56 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Twig; + +use App\Entity\Base\AbstractDBElement; +use App\Entity\UserSystem\User; +use App\Repository\LogEntryRepository; +use Doctrine\ORM\EntityManagerInterface; +use Twig\Attribute\AsTwigFunction; + +final readonly class UserRepoExtension +{ + + public function __construct(private EntityManagerInterface $entityManager) + { + } + + /** + * Returns the user which has edited the given entity the last time. + */ + #[AsTwigFunction('creating_user')] + public function creatingUser(AbstractDBElement $element): ?User + { + return $this->entityManager->getRepository(LogEntryRepository::class)->getCreatingUser($element); + } + + /** + * Returns the user which has edited the given entity the last time. + */ + #[AsTwigFunction('last_editing_user')] + public function lastEditingUser(AbstractDBElement $element): ?User + { + return $this->entityManager->getRepository(LogEntryRepository::class)->getLastEditingUser($element); + } +}