]> Raphaël G. Git Repositories - treebundle/blob - Repository.php
Import repository base class
[treebundle] / Repository.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys TreeBundle 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\TreeBundle;
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
22 use Symfony\Component\Routing\RouterInterface;
23 use Symfony\Contracts\Translation\TranslatorInterface;
24
25 /**
26 * Repository
27 *
28 * {@inheritdoc}
29 */
30 class Repository extends EntityRepository {
31 /**
32 * The table keys array
33 *
34 * @var array
35 */
36 protected array $keys;
37
38 /**
39 * The table names array
40 *
41 * @var array
42 */
43 protected array $names;
44
45 /**
46 * Initializes a new LocationRepository instance
47 *
48 * @param EntityManagerInterface $manager The EntityManagerInterface instance
49 * @param ClassMetadata $class The ClassMetadata instance
50 * @param ContainerInterface $container The container instance
51 * @param RouterInterface $router The router instance
52 * @param SluggerUtil $slugger The SluggerUtil instance
53 * @param TranslatorInterface $translator The TranslatorInterface instance
54 * @param string $locale The current locale
55 * @param array $languages The languages list
56 */
57 public function __construct(protected EntityManagerInterface $manager, protected ClassMetadata $class, protected ContainerInterface $container, protected RouterInterface $router, protected SluggerUtil $slugger, protected TranslatorInterface $translator, protected string $locale, protected array $languages) {
58 //Call parent constructor
59 parent::__construct($this->manager, $this->class);
60
61 //Get quote strategy
62 $qs = $this->manager->getConfiguration()->getQuoteStrategy();
63 $dp = $this->manager->getConnection()->getDatabasePlatform();
64
65 //Set quoted table names
66 //XXX: this allow to make this code table name independent
67 //XXX: remember to place longer prefix before shorter to avoid strange replacings
68 //XXX: entity short syntax removed in doctrine/persistence 3.x: https://github.com/doctrine/orm/issues/8818
69 $tables = [
70 'Rapsys\TreeBundle\Entity\UserGroup' => $qs->getJoinTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\User')->getAssociationMapping('groups'), $manager->getClassMetadata('Rapsys\TreeBundle\Entity\User'), $dp),
71 'Rapsys\TreeBundle\Entity\Album' => $qs->getTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\Album'), $dp),
72 'Rapsys\TreeBundle\Entity\Asset' => $qs->getTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\Asset'), $dp),
73 'Rapsys\TreeBundle\Entity\Civility' => $qs->getTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\Civility'), $dp),
74 'Rapsys\TreeBundle\Entity\Group' => $qs->getTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\Group'), $dp),
75 'Rapsys\TreeBundle\Entity\User' => $qs->getTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\User'), $dp),
76 //Set locale
77 //XXX: or $manager->getConnection()->quote($this->locale) ???
78 ':locale' => $dp->quoteStringLiteral($this->locale),
79 //Set limit
80 //XXX: Set limit used to workaround mariadb subselect optimization
81 ':limit' => PHP_INT_MAX,
82 //Set cleanup
83 "\t" => '',
84 "\r" => ' ',
85 "\n" => ' '
86 ];
87
88 //Set quoted table keys
89 $this->keys = array_keys($tables);
90
91 //Set quoted table names
92 $this->names = array_values($tables);
93 }
94
95 /**
96 * Get replaced query
97 *
98 * @param string $req The request to replace
99 * @return string The replaced request
100 */
101 protected function replace(string $req): string {
102 //Replace bundle entity name by table name
103 return str_replace($this->keys, $this->names, $req);
104 }
105 }