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