]> Raphaël G. Git Repositories - airbundle/blob - Factory/RepositoryFactory.php
New site.icon layout
[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 ObjectRepository[]
30 */
31 private $repositoryList = [];
32
33 /**
34 * The list of languages
35 *
36 * @var string[]
37 */
38 private $languages = [];
39
40 /**
41 * The RouterInterface instance
42 *
43 * @var RouterInterface
44 */
45 private $router;
46
47 /**
48 * The SluggerUtil instance
49 *
50 * @var SluggerUtil
51 */
52 private $slugger;
53
54 /**
55 * The TranslatorInterface instance
56 *
57 * @var TranslatorInterface
58 */
59 private $translator;
60
61 /**
62 * Initializes a new RepositoryFactory instance
63 *
64 * @param RouterInterface $router The router instance
65 * @param SluggerUtil $slugger The SluggerUtil instance
66 * @param TranslatorInterface $translator The TranslatorInterface instance
67 * @param array $languages The languages list
68 */
69 public function __construct(RouterInterface $router, SluggerUtil $slugger, TranslatorInterface $translator, array $languages) {
70 //Set router
71 $this->router = $router;
72
73 //Set slugger
74 $this->slugger = $slugger;
75
76 //Set translator
77 $this->translator = $translator;
78
79 //Set languages
80 $this->languages = $languages;
81 }
82
83 /**
84 * {@inheritdoc}
85 */
86 public function getRepository(EntityManagerInterface $entityManager, $entityName): ObjectRepository {
87 //Set repository hash
88 $repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_hash($entityManager);
89
90 //With entity repository instance
91 if (isset($this->repositoryList[$repositoryHash])) {
92 //Return existing entity repository instance
93 return $this->repositoryList[$repositoryHash];
94 }
95
96 //Store and return created entity repository instance
97 return $this->repositoryList[$repositoryHash] = $this->createRepository($entityManager, $entityName);
98 }
99
100 /**
101 * Create a new repository instance for an entity class
102 *
103 * @param EntityManagerInterface $entityManager The EntityManager instance.
104 * @param string $entityName The name of the entity.
105 */
106 private function createRepository(EntityManagerInterface $entityManager, string $entityName): ObjectRepository {
107 //Get class metadata
108 $metadata = $entityManager->getClassMetadata($entityName);
109
110 //Get repository class
111 $repositoryClass = $metadata->customRepositoryClassName ?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();
112
113 //Return repository class instance
114 //XXX: router, slugger, translator and languages arguments will be ignored by default
115 return new $repositoryClass($entityManager, $metadata, $this->router, $this->slugger, $this->translator, $this->languages);
116 }
117 }