1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys AirBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\AirBundle\Factory
;
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
;
20 use Rapsys\PackBundle\Util\SluggerUtil
;
23 * This factory is used to create default repository objects for entities at runtime.
25 final class RepositoryFactory
implements RepositoryFactoryInterface
{
27 * The list of EntityRepository instances
29 * @var ObjectRepository[]
31 private $repositoryList = [];
34 * The list of languages
38 private $languages = [];
41 * The RouterInterface instance
43 * @var RouterInterface
48 * The SluggerUtil instance
55 * The TranslatorInterface instance
57 * @var TranslatorInterface
62 * Initializes a new RepositoryFactory instance
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
69 public function __construct(RouterInterface
$router, SluggerUtil
$slugger, TranslatorInterface
$translator, array $languages) {
71 $this->router
= $router;
74 $this->slugger
= $slugger;
77 $this->translator
= $translator;
80 $this->languages
= $languages;
86 public function getRepository(EntityManagerInterface
$entityManager, $entityName): ObjectRepository
{
88 $repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_hash($entityManager);
90 //With entity repository instance
91 if (isset($this->repositoryList
[$repositoryHash])) {
92 //Return existing entity repository instance
93 return $this->repositoryList
[$repositoryHash];
96 //Store and return created entity repository instance
97 return $this->repositoryList
[$repositoryHash] = $this->createRepository($entityManager, $entityName);
101 * Create a new repository instance for an entity class
103 * @param EntityManagerInterface $entityManager The EntityManager instance.
104 * @param string $entityName The name of the entity.
106 private function createRepository(EntityManagerInterface
$entityManager, string $entityName): ObjectRepository
{
108 $metadata = $entityManager->getClassMetadata($entityName);
110 //Get repository class
111 $repositoryClass = $metadata->customRepositoryClassName
?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();
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
);