]> Raphaël G. Git Repositories - userbundle/blob - Factory/RepositoryFactory.php
Rename Rapsys\UserBundle\Factory\RepositoryFactory in Rapsys\UserBundle\Factory
[userbundle] / Factory / RepositoryFactory.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\Factory;
13
14 use Doctrine\ORM\EntityManagerInterface;
15 use Doctrine\ORM\Repository\RepositoryFactory as RepositoryFactoryInterface;
16 use Doctrine\Persistence\ObjectRepository;
17 use Symfony\Component\HttpFoundation\RequestStack;
18 use Symfony\Component\Routing\RouterInterface;
19 use Symfony\Contracts\Translation\TranslatorInterface;
20
21 use Rapsys\PackBundle\Util\SluggerUtil;
22
23 /**
24 * This factory is used to create default repository objects for entities at runtime.
25 */
26 final class RepositoryFactory implements RepositoryFactoryInterface {
27 /**
28 * The list of EntityRepository instances
29 *
30 * @var array
31 */
32 private array $repositoryList = [];
33
34 /**
35 * The list of languages
36 *
37 * @var array
38 */
39 private array $languages = [];
40
41 /**
42 * The current locale
43 *
44 * @var string
45 */
46 private string $locale;
47
48 /**
49 * The RequestStack instance
50 *
51 * @var RequestStack
52 */
53 private RequestStack $request;
54
55 /**
56 * The RouterInterface instance
57 *
58 * @var RouterInterface
59 */
60 private RouterInterface $router;
61
62 /**
63 * The SluggerUtil instance
64 *
65 * @var SluggerUtil
66 */
67 private SluggerUtil $slugger;
68
69 /**
70 * The TranslatorInterface instance
71 *
72 * @var TranslatorInterface
73 */
74 private TranslatorInterface $translator;
75
76 /**
77 * Initializes a new RepositoryFactory instance
78 *
79 * @param RequestStack $request The request stack
80 * @param RouterInterface $router The router instance
81 * @param SluggerUtil $slugger The SluggerUtil instance
82 * @param TranslatorInterface $translator The TranslatorInterface instance
83 * @param array $languages The languages list
84 * @param string $locale The current locale
85 */
86 public function __construct(RequestStack $request, RouterInterface $router, SluggerUtil $slugger, TranslatorInterface $translator, array $languages, string $locale) {
87 //Set request
88 $this->request = $request;
89
90 //Set router
91 $this->router = $router;
92
93 //Set slugger
94 $this->slugger = $slugger;
95
96 //Set translator
97 $this->translator = $translator;
98
99 //Set languages
100 $this->languages = $languages;
101
102 //Set locale
103 $this->locale = $locale;
104 }
105
106 /**
107 * {@inheritdoc}
108 */
109 public function getRepository(EntityManagerInterface $entityManager, mixed $entityName): ObjectRepository {
110 //Set repository hash
111 $repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_hash($entityManager);
112
113 //With entity repository instance
114 if (isset($this->repositoryList[$repositoryHash])) {
115 //Return existing entity repository instance
116 return $this->repositoryList[$repositoryHash];
117 }
118
119 //Store and return created entity repository instance
120 return $this->repositoryList[$repositoryHash] = $this->createRepository($entityManager, $entityName);
121 }
122
123 /**
124 * Create a new repository instance for an entity class
125 *
126 * @param EntityManagerInterface $entityManager The EntityManager instance.
127 * @param string $entityName The name of the entity.
128 */
129 private function createRepository(EntityManagerInterface $entityManager, string $entityName): ObjectRepository {
130 //Get class metadata
131 $metadata = $entityManager->getClassMetadata($entityName);
132
133 //Get repository class
134 $repositoryClass = $metadata->customRepositoryClassName ?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();
135
136 //Set to current locale
137 //XXX: current request is not yet populated in constructor
138 $this->locale = $this->request->getCurrentRequest()->getLocale() ?? $this->locale;
139
140 //Return repository class instance
141 //XXX: router, slugger, translator, languages and locale arguments will be ignored by default
142 return new $repositoryClass($entityManager, $metadata, $this->router, $this->slugger, $this->translator, $this->languages, $this->locale);
143 }
144 }