1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys UserBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\UserBundle
;
14 use Doctrine\ORM\EntityManagerInterface
;
15 use Doctrine\ORM\EntityRepository
;
16 use Doctrine\ORM\Mapping\ClassMetadata
;
18 use Psr\Container\ContainerInterface
;
20 use Rapsys\PackBundle\Util\SluggerUtil
;
21 use Rapsys\UserBundle\RapsysUserBundle
;
23 use Symfony\Component\Routing\RouterInterface
;
24 use Symfony\Contracts\Translation\TranslatorInterface
;
31 class Repository
extends EntityRepository
{
35 protected string $alias;
40 protected array $config;
43 * The table keys array
47 protected array $tableKeys;
50 * The table values array
54 protected array $tableValues;
57 * Initializes a new LocationRepository instance
59 * @param EntityManagerInterface $manager The EntityManagerInterface instance
60 * @param ClassMetadata $class The ClassMetadata instance
61 * @param ContainerInterface $container The container instance
62 * @param RouterInterface $router The router instance
63 * @param SluggerUtil $slugger The SluggerUtil instance
64 * @param TranslatorInterface $translator The TranslatorInterface instance
65 * @param string $locale The current locale
67 public function __construct(protected EntityManagerInterface
$manager, protected ClassMetadata
$class, protected ContainerInterface
$container, protected RouterInterface
$router, protected SluggerUtil
$slugger, protected TranslatorInterface
$translator, protected string $locale) {
68 //Call parent constructor
69 parent
::__construct($manager, $class);
72 //XXX: extracting doctrine.orm.resolve_target_entities seems too complicated and doctrine listener is unusable to get reliable target entities resolution here
73 $this->config
= $container->getParameter($this->alias
= RapsysUserBundle
::getAlias());
76 $qs = $manager->getConfiguration()->getQuoteStrategy();
77 $dp = $manager->getConnection()->getDatabasePlatform();
79 //Set quoted table names
80 //XXX: this allow to make this code table name independent
81 //XXX: remember to place longer prefix before shorter to avoid strange replacings
82 //XXX: entity short syntax removed in doctrine/persistence 3.x: https://github.com/doctrine/orm/issues/8818
85 'Rapsys\UserBundle\Entity\UserGroup' => $qs->getJoinTableName($manager->getClassMetadata($this->config
['class']['user'])->getAssociationMapping('groups'), $manager->getClassMetadata($this->config
['class']['user']), $dp),
86 'Rapsys\UserBundle\Entity\Civility' => $qs->getTableName($manager->getClassMetadata($this->config
['class']['civility']), $dp),
87 'Rapsys\UserBundle\Entity\Group' => $qs->getTableName($manager->getClassMetadata($this->config
['class']['group']), $dp),
88 'Rapsys\UserBundle\Entity\User' => $qs->getTableName($manager->getClassMetadata($this->config
['class']['user']), $dp),
90 //XXX: or $manager->getConnection()->quote($this->locale) ???
91 ':locale' => $dp->quoteStringLiteral($this->locale
),
93 //XXX: Set limit used to workaround mariadb subselect optimization
94 ':limit' => PHP_INT_MAX
,
101 //Set quoted table name keys
102 $this->tableKeys
= array_keys($tables);
104 //Set quoted table name values
105 $this->tableValues
= array_values($tables);
111 * @param string $req The request to replace
112 * @return string The replaced request
114 protected function replace(string $req): string {
115 //Replace bundle entity name by table name
116 return str_replace($this->tableKeys
, $this->tableValues
, $req);