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