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