1 <?php 
declare(strict_types
=1); 
   4  * This file is part of the Rapsys BlogBundle 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\BlogBundle\Factory
; 
  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
; 
  21 use Rapsys\PackBundle\Util\SluggerUtil
; 
  24  * This factory is used to create default repository objects for entities at runtime. 
  26 final class RepositoryFactory 
implements RepositoryFactoryInterface 
{ 
  28          * The list of EntityRepository instances 
  32         private array $repositoryList = []; 
  35          * The list of languages 
  39         private array $languages = []; 
  46         private string $locale; 
  49          * The RequestStack instance 
  53         private RequestStack 
$request; 
  56          * The RouterInterface instance 
  58          * @var RouterInterface 
  60         private RouterInterface 
$router; 
  63          * The SluggerUtil instance 
  67         private SluggerUtil 
$slugger; 
  70          * The TranslatorInterface instance 
  72          * @var TranslatorInterface 
  74         private TranslatorInterface 
$translator; 
  77          * Initializes a new RepositoryFactory instance 
  79          * @param RequestStack $request The request stack 
  80          * @param RouterInterface $router The router instance 
  81          * @param SluggerUtil $slugger The SluggerUtil instance 
  82          * @param TranslatorInterface $translator The TranslatorInterface instance 
  83          * @param array $languages The languages list 
  84          * @param string $locale The current locale 
  86         public function __construct(RequestStack 
$request, RouterInterface 
$router, SluggerUtil 
$slugger, TranslatorInterface 
$translator, array $languages, string $locale) { 
  88                 $this->request 
= $request; 
  91                 $this->router 
= $router; 
  94                 $this->slugger 
= $slugger; 
  97                 $this->translator 
= $translator; 
 100                 $this->languages 
= $languages; 
 103                 $this->locale 
= $locale; 
 109         public function getRepository(EntityManagerInterface 
$entityManager, mixed $entityName): ObjectRepository 
{ 
 110                 //Set repository hash 
 111                 $repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_hash($entityManager); 
 113                 //With entity repository instance 
 114                 if (isset($this->repositoryList
[$repositoryHash])) { 
 115                         //Return existing entity repository instance 
 116                         return $this->repositoryList
[$repositoryHash]; 
 119                 //Store and return created entity repository instance 
 120                 return $this->repositoryList
[$repositoryHash] = $this->createRepository($entityManager, $entityName); 
 124          * Create a new repository instance for an entity class 
 126          * @param EntityManagerInterface $entityManager The EntityManager instance. 
 127          * @param string $entityName The name of the entity. 
 129         private function createRepository(EntityManagerInterface 
$entityManager, string $entityName): ObjectRepository 
{ 
 131                 $metadata = $entityManager->getClassMetadata($entityName); 
 133                 //Get repository class 
 134                 $repositoryClass = $metadata->customRepositoryClassName 
?: $entityManager->getConfiguration()->getDefaultRepositoryClassName(); 
 136                 //Set to current locale 
 137                 //XXX: current request is not yet populated in constructor 
 138                 $this->locale 
= $this->request
->getCurrentRequest()->getLocale() ?? $this->locale
; 
 140                 //Return repository class instance 
 141                 //XXX: router, slugger, translator, languages and locale arguments will be ignored by default 
 142                 return new $repositoryClass($entityManager, $metadata, $this->router
, $this->slugger
, $this->translator
, $this->languages
, $this->locale
);