]> Raphaël G. Git Repositories - userbundle/blob - Factory.php
Enable register captcha
[userbundle] / Factory.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\Repository\RepositoryFactory as RepositoryFactoryInterface;
16 use Doctrine\Persistence\ObjectRepository;
17
18 use Rapsys\PackBundle\Util\SluggerUtil;
19
20 use Symfony\Component\HttpFoundation\RequestStack;
21 use Symfony\Component\Routing\RouterInterface;
22 use Symfony\Contracts\Translation\TranslatorInterface;
23
24 /**
25 * This factory is used to create default repository objects for entities at runtime.
26 */
27 final class Factory implements RepositoryFactoryInterface {
28 /**
29 * The list of EntityRepository instances
30 */
31 private array $repositoryList = [];
32
33 /**
34 * Initializes a new RepositoryFactory instance
35 *
36 * @param RequestStack $request The request stack
37 * @param RouterInterface $router The router instance
38 * @param SluggerUtil $slugger The SluggerUtil instance
39 * @param TranslatorInterface $translator The TranslatorInterface instance
40 * @param string $locale The current locale
41 */
42 public function __construct(private RequestStack $request, private RouterInterface $router, private SluggerUtil $slugger, private TranslatorInterface $translator, private string $locale) {
43 }
44
45 /**
46 * {@inheritdoc}
47 */
48 public function getRepository(EntityManagerInterface $entityManager, mixed $entityName): ObjectRepository {
49 //Set repository hash
50 $repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_hash($entityManager);
51
52 //With entity repository instance
53 if (isset($this->repositoryList[$repositoryHash])) {
54 //Return existing entity repository instance
55 return $this->repositoryList[$repositoryHash];
56 }
57
58 //Store and return created entity repository instance
59 return $this->repositoryList[$repositoryHash] = $this->createRepository($entityManager, $entityName);
60 }
61
62 /**
63 * Create a new repository instance for an entity class
64 *
65 * @param EntityManagerInterface $entityManager The EntityManager instance.
66 * @param string $entityName The name of the entity.
67 */
68 private function createRepository(EntityManagerInterface $entityManager, string $entityName): ObjectRepository {
69 //Get class metadata
70 $metadata = $entityManager->getClassMetadata($entityName);
71
72 //Get repository class
73 $repositoryClass = $metadata->customRepositoryClassName ?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();
74
75 //Set to current locale
76 //XXX: current request is not yet populated in constructor
77 $this->locale = $this->request->getCurrentRequest()?->getLocale() ?? $this->locale;
78
79 //Return repository class instance
80 //XXX: router, slugger, translator and locale arguments will be ignored by default
81 return new $repositoryClass($entityManager, $metadata, $this->router, $this->slugger, $this->translator, $this->locale);
82 }
83 }