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
84 'RapsysUserBundle:UserGroup' => $qs->getJoinTableName($manager->getClassMetadata($this->config
['class']['user'])->getAssociationMapping('groups'), $manager->getClassMetadata($this->config
['class']['user']), $dp),
85 'RapsysUserBundle:Civility' => $qs->getTableName($manager->getClassMetadata($this->config
['class']['civility']), $dp),
86 'RapsysUserBundle:Group' => $qs->getTableName($manager->getClassMetadata($this->config
['class']['group']), $dp),
87 'RapsysUserBundle:User' => $qs->getTableName($manager->getClassMetadata($this->config
['class']['user']), $dp),
89 //XXX: or $manager->getConnection()->quote($this->locale) ???
90 ':locale' => $dp->quoteStringLiteral($this->locale
),
92 //XXX: Set limit used to workaround mariadb subselect optimization
93 ':limit' => PHP_INT_MAX
,
100 //Set quoted table name keys
101 $this->tableKeys
= array_keys($tables);
103 //Set quoted table name values
104 $this->tableValues
= array_values($tables);
110 * @param string $req The request to replace
111 * @return string The replaced request
113 protected function replace(string $req): string {
114 //Replace bundle entity name by table name
115 return str_replace($this->tableKeys
, $this->tableValues
, $req);