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 
  31         private array $repositoryList = []; 
  34          * The list of languages 
  38         private array $languages = []; 
  45         private string $locale; 
  48          * The RouterInterface instance 
  50          * @var RouterInterface 
  52         private RouterInterface 
$router; 
  55          * The SluggerUtil instance 
  59         private SluggerUtil 
$slugger; 
  62          * The TranslatorInterface instance 
  64          * @var TranslatorInterface 
  66         private TranslatorInterface 
$translator; 
  69          * Initializes a new RepositoryFactory instance 
  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 
  77         public function __construct(RouterInterface 
$router, SluggerUtil 
$slugger, TranslatorInterface 
$translator, array $languages, string $locale) { 
  79                 $this->router 
= $router; 
  82                 $this->slugger 
= $slugger; 
  85                 $this->translator 
= $translator; 
  88                 $this->languages 
= $languages; 
  91                 $this->locale 
= $locale; 
  97         public function getRepository(EntityManagerInterface 
$entityManager, mixed $entityName): ObjectRepository 
{ 
  99                 $repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_hash($entityManager); 
 101                 //With entity repository instance 
 102                 if (isset($this->repositoryList
[$repositoryHash])) { 
 103                         //Return existing entity repository instance 
 104                         return $this->repositoryList
[$repositoryHash]; 
 107                 //Store and return created entity repository instance 
 108                 return $this->repositoryList
[$repositoryHash] = $this->createRepository($entityManager, $entityName); 
 112          * Create a new repository instance for an entity class 
 114          * @param EntityManagerInterface $entityManager The EntityManager instance. 
 115          * @param string $entityName The name of the entity. 
 117         private function createRepository(EntityManagerInterface 
$entityManager, string $entityName): ObjectRepository 
{ 
 119                 $metadata = $entityManager->getClassMetadata($entityName); 
 121                 //Get repository class 
 122                 $repositoryClass = $metadata->customRepositoryClassName 
?: $entityManager->getConfiguration()->getDefaultRepositoryClassName(); 
 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
);