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