]> Raphaël G. Git Repositories - userbundle/blob - Repository.php
Version 0.5.0
[userbundle] / Repository.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys UserBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\UserBundle;
13
14 use Doctrine\ORM\EntityManagerInterface;
15 use Doctrine\ORM\EntityRepository;
16 use Doctrine\ORM\Mapping\ClassMetadata;
17 use Symfony\Component\Routing\RouterInterface;
18 use Symfony\Contracts\Translation\TranslatorInterface;
19
20 use Rapsys\PackBundle\Util\SluggerUtil;
21
22 /**
23 * {@inheritdoc}
24 *
25 * Repository
26 */
27 class Repository extends EntityRepository {
28 /**
29 * The table keys array
30 *
31 * @var array
32 */
33 protected array $tableKeys;
34
35 /**
36 * The table values array
37 *
38 * @var array
39 */
40 protected array $tableValues;
41
42 /**
43 * Initializes a new LocationRepository instance
44 *
45 * @param EntityManagerInterface $manager The EntityManagerInterface instance
46 * @param ClassMetadata $class The ClassMetadata instance
47 * @param RouterInterface $router The router instance
48 * @param SluggerUtil $slugger The SluggerUtil instance
49 * @param TranslatorInterface $translator The TranslatorInterface instance
50 * @param string $locale The current locale
51 */
52 public function __construct(protected EntityManagerInterface $manager, protected ClassMetadata $class, protected RouterInterface $router, protected SluggerUtil $slugger, protected TranslatorInterface $translator, protected string $locale) {
53 //Call parent constructor
54 parent::__construct($manager, $class);
55
56 //Get quote strategy
57 $qs = $manager->getConfiguration()->getQuoteStrategy();
58 $dp = $manager->getConnection()->getDatabasePlatform();
59
60 //Set quoted table names
61 //XXX: this allow to make this code table name independent
62 //XXX: remember to place longer prefix before shorter to avoid strange replacings
63 $tables = [
64 //Set entities
65 'RapsysUserBundle:UserGroup' => $qs->getJoinTableName($manager->getClassMetadata('Rapsys\UserBundle\Entity\User')->getAssociationMapping('groups'), $manager->getClassMetadata('Rapsys\UserBundle\Entity\User'), $dp),
66 'RapsysUserBundle:Civility' => $qs->getTableName($manager->getClassMetadata('Rapsys\UserBundle\Entity\Civility'), $dp),
67 'RapsysUserBundle:Group' => $qs->getTableName($manager->getClassMetadata('Rapsys\UserBundle\Entity\Group'), $dp),
68 'RapsysUserBundle:User' => $qs->getTableName($manager->getClassMetadata('Rapsys\UserBundle\Entity\User'), $dp),
69 //Set locale
70 //XXX: or $manager->getConnection()->quote($this->locale) ???
71 ':locale' => $dp->quoteStringLiteral($this->locale),
72 //Set limit
73 //XXX: Set limit used to workaround mariadb subselect optimization
74 ':limit' => PHP_INT_MAX,
75 //Set cleanup
76 "\t" => '',
77 "\r" => ' ',
78 "\n" => ' '
79 ];
80
81 //Set quoted table name keys
82 $this->tableKeys = array_keys($tables);
83
84 //Set quoted table name values
85 $this->tableValues = array_values($tables);
86 }
87
88 /**
89 * Get replaced query
90 *
91 * @param string $req The request to replace
92 * @return string The replaced request
93 */
94 protected function replace(string $req): string {
95 //Replace bundle entity name by table name
96 return str_replace($this->tableKeys, $this->tableValues, $req);
97 }
98 }