]> Raphaël G. Git Repositories - blogbundle/blob - Repository/EntityRepository.php
Cleanup flash message
[blogbundle] / Repository / EntityRepository.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys BlogBundle 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\BlogBundle\Repository;
13
14 use Doctrine\ORM\EntityManagerInterface;
15 use Doctrine\ORM\EntityRepository as BaseEntityRepository;
16 use Doctrine\ORM\Mapping\ClassMetadata;
17 use Symfony\Component\Routing\RouterInterface;
18 use Symfony\Contracts\Translation\TranslatorInterface;
19
20 use Rapsys\PackBundle\Util\SluggerUtil;
21
22 /**
23 * EntityRepository
24 *
25 * {@inheritdoc}
26 */
27 class EntityRepository extends BaseEntityRepository {
28 /**
29 * The RouterInterface instance
30 *
31 * @var RouterInterface
32 */
33 protected RouterInterface $router;
34
35 /**
36 * The SluggerUtil instance
37 *
38 * @var SluggerUtil
39 */
40 protected SluggerUtil $slugger;
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 * The TranslatorInterface instance
58 *
59 * @var TranslatorInterface
60 */
61 protected TranslatorInterface $translator;
62
63 /**
64 * The list of languages
65 *
66 * @var string[]
67 */
68 protected array $languages = [];
69
70 /**
71 * The current locale
72 *
73 * @var string
74 */
75 protected string $locale;
76
77 /**
78 * Initializes a new LocationRepository instance
79 *
80 * @param EntityManagerInterface $manager The EntityManagerInterface instance
81 * @param ClassMetadata $class The ClassMetadata instance
82 * @param RouterInterface $router The router instance
83 * @param SluggerUtil $slugger The SluggerUtil instance
84 * @param TranslatorInterface $translator The TranslatorInterface instance
85 * @param array $languages The languages list
86 * @param string $locale The current locale
87 */
88 public function __construct(EntityManagerInterface $manager, ClassMetadata $class, RouterInterface $router, SluggerUtil $slugger, TranslatorInterface $translator, array $languages, string $locale) {
89 //Call parent constructor
90 parent::__construct($manager, $class);
91
92 //Set languages
93 $this->languages = $languages;
94
95 //Set locale
96 $this->locale = $locale;
97
98 //Set router
99 $this->router = $router;
100
101 //Set slugger
102 $this->slugger = $slugger;
103
104 //Set translator
105 $this->translator = $translator;
106
107 //Get quote strategy
108 $qs = $manager->getConfiguration()->getQuoteStrategy();
109 $dp = $manager->getConnection()->getDatabasePlatform();
110
111 //Set quoted table names
112 //XXX: this allow to make this code table name independent
113 //XXX: remember to place longer prefix before shorter to avoid strange replacings
114 $tables = [
115 //Set entities
116 'RapsysBlogBundle:ArticleKeyword' => $qs->getJoinTableName($manager->getClassMetadata('Rapsys\BlogBundle\Entity\Article')->getAssociationMapping('keywords'), $manager->getClassMetadata('Rapsys\BlogBundle\Entity\Article'), $dp),
117 'RapsysBlogBundle:UserGroup' => $qs->getJoinTableName($manager->getClassMetadata('Rapsys\BlogBundle\Entity\User')->getAssociationMapping('groups'), $manager->getClassMetadata('Rapsys\BlogBundle\Entity\User'), $dp),
118 'RapsysBlogBundle:ArticleTranslation' => $qs->getTableName($manager->getClassMetadata('Rapsys\BlogBundle\Entity\ArticleTranslation'), $dp),
119 'RapsysBlogBundle:KeywordTranslation' => $qs->getTableName($manager->getClassMetadata('Rapsys\BlogBundle\Entity\KeywordTranslation'), $dp),
120 'RapsysBlogBundle:UserTranslation' => $qs->getTableName($manager->getClassMetadata('Rapsys\BlogBundle\Entity\UserTranslation'), $dp),
121 'RapsysBlogBundle:Article' => $qs->getTableName($manager->getClassMetadata('Rapsys\BlogBundle\Entity\Article'), $dp),
122 'RapsysBlogBundle:Civility' => $qs->getTableName($manager->getClassMetadata('Rapsys\BlogBundle\Entity\Civility'), $dp),
123 'RapsysBlogBundle:Group' => $qs->getTableName($manager->getClassMetadata('Rapsys\BlogBundle\Entity\Group'), $dp),
124 'RapsysBlogBundle:Keyword' => $qs->getTableName($manager->getClassMetadata('Rapsys\BlogBundle\Entity\Keyword'), $dp),
125 'RapsysBlogBundle:User' => $qs->getTableName($manager->getClassMetadata('Rapsys\BlogBundle\Entity\User'), $dp),
126 //Set locale
127 //XXX: or $manager->getConnection()->quote($this->locale) ???
128 ':locale' => $dp->quoteStringLiteral($this->locale),
129 //Set limit
130 //XXX: Set limit used to workaround mariadb subselect optimization
131 ':limit' => PHP_INT_MAX,
132 //Set cleanup
133 "\t" => '',
134 "\r" => ' ',
135 "\n" => ' '
136 ];
137
138 //Set quoted table name keys
139 $this->tableKeys = array_keys($tables);
140
141 //Set quoted table name values
142 $this->tableValues = array_values($tables);
143 }
144
145 /**
146 * Get replaced query
147 *
148 * @param string $req The request to replace
149 * @return string The replaced request
150 */
151 protected function replace(string $req): string {
152 //Replace bundle entity name by table name
153 return str_replace($this->tableKeys, $this->tableValues, $req);
154 }
155 }