]> Raphaël G. Git Repositories - userbundle/blob - Repository.php
Add short syntax note
[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 //XXX: entity short syntax removed in doctrine/persistence 3.x: https://github.com/doctrine/orm/issues/8818
83 $tables = [
84 //Set entities
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),
89 //Set locale
90 //XXX: or $manager->getConnection()->quote($this->locale) ???
91 ':locale' => $dp->quoteStringLiteral($this->locale),
92 //Set limit
93 //XXX: Set limit used to workaround mariadb subselect optimization
94 ':limit' => PHP_INT_MAX,
95 //Set cleanup
96 "\t" => '',
97 "\r" => ' ',
98 "\n" => ' '
99 ];
100
101 //Set quoted table name keys
102 $this->tableKeys = array_keys($tables);
103
104 //Set quoted table name values
105 $this->tableValues = array_values($tables);
106 }
107
108 /**
109 * Get replaced query
110 *
111 * @param string $req The request to replace
112 * @return string The replaced request
113 */
114 protected function replace(string $req): string {
115 //Replace bundle entity name by table name
116 return str_replace($this->tableKeys, $this->tableValues, $req);
117 }
118 }