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