]> Raphaël G. Git Repositories - userbundle/blob - Repository.php
Version 0.5.1
[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
18 use Psr\Container\ContainerInterface;
19
20 use Rapsys\PackBundle\Util\SluggerUtil;
21 use Rapsys\UserBundle\RapsysUserBundle;
22
23 use Symfony\Component\Routing\RouterInterface;
24 use Symfony\Contracts\Translation\TranslatorInterface;
25
26 /**
27 * {@inheritdoc}
28 *
29 * Repository
30 */
31 class Repository extends EntityRepository {
32 /**
33 * Alias string
34 */
35 protected string $alias;
36
37 /**
38 * Config array
39 */
40 protected array $config;
41
42 /**
43 * The table keys array
44 *
45 * @var array
46 */
47 protected array $tableKeys;
48
49 /**
50 * The table values array
51 *
52 * @var array
53 */
54 protected array $tableValues;
55
56 /**
57 * Initializes a new LocationRepository instance
58 *
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
66 */
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);
70
71 //Get config
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());
74
75 //Get quote strategy
76 $qs = $manager->getConfiguration()->getQuoteStrategy();
77 $dp = $manager->getConnection()->getDatabasePlatform();
78
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 $tables = [
83 //Set entities
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),
88 //Set default group
89 //XXX: or $manager->getConnection()->quote($this->config['default']['group'][0]) ???
90 ':defaultgroup' => $dp->quoteStringLiteral($this->config['default']['group'][0]),
91 //Set locale
92 //XXX: or $manager->getConnection()->quote($this->locale) ???
93 ':locale' => $dp->quoteStringLiteral($this->locale),
94 //Set limit
95 //XXX: Set limit used to workaround mariadb subselect optimization
96 ':limit' => PHP_INT_MAX,
97 //Set cleanup
98 "\t" => '',
99 "\r" => ' ',
100 "\n" => ' '
101 ];
102
103 //Set quoted table name keys
104 $this->tableKeys = array_keys($tables);
105
106 //Set quoted table name values
107 $this->tableValues = array_values($tables);
108 }
109
110 /**
111 * Get replaced query
112 *
113 * @param string $req The request to replace
114 * @return string The replaced request
115 */
116 protected function replace(string $req): string {
117 //Replace bundle entity name by table name
118 return str_replace($this->tableKeys, $this->tableValues, $req);
119 }
120 }