Last active
          August 22, 2024 16:24 
        
      - 
      
- 
        Save docteurklein/9778800 to your computer and use it in GitHub Desktop. 
    Service Repository Factory
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| public function process(ContainerBuilder $container) | |
| { | |
| $factory = $container->findDefinition('app.doctrine.repository.factory'); | |
| $repositories = []; | |
| foreach ($container->findTaggedServiceIds('app.repository') as $id => $params) { | |
| foreach ($params as $param) { | |
| $repositories[$param['class']] = $id; | |
| $repository = $container->findDefinition($id); | |
| $repository->replaceArgument(0, new Reference('doctrine.orm.default_entity_manager')); | |
| $definition = new Definition; | |
| $definition->setClass('Doctrine\ORM\Mapping\ClassMetadata'); | |
| $definition->setFactoryService('doctrine.orm.default_entity_manager'); | |
| $definition->setFactoryMethod('getClassMetadata'); | |
| $definition->setArguments([$param['class']]); | |
| $repository->replaceArgument(1, $definition); | |
| } | |
| } | |
| $factory->replaceArgument(0, $repositories); | |
| $container->findDefinition('doctrine.orm.configuration')->addMethodCall('setRepositoryFactory', [$factory]); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| namespace App\Doctrine\Repository; | |
| use Symfony\Component\DependencyInjection\ContainerInterface; | |
| use Doctrine\ORM\EntityManagerInterface; | |
| use Doctrine\ORM\Repository\RepositoryFactory; | |
| class Factory implements RepositoryFactory | |
| { | |
| private $ids; | |
| private $container; | |
| private $default; | |
| public function __construct(array $ids, ContainerInterface $container, RepositoryFactory $default) | |
| { | |
| $this->ids = $ids; | |
| $this->container = $container; | |
| $this->default = $default; | |
| } | |
| public function getRepository(EntityManagerInterface $entityManager, $entityName) | |
| { | |
| if (isset($this->ids[$entityName])) { | |
| return $this->container->get($this->ids[$entityName]); | |
| } | |
| return $this->default->getRepository($entityManager, $entityName); | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | services: | |
| app.doctrine.repository.factory: | |
| class: App\Doctrine\Repository\Factory | |
| arguments: | |
| - [] | |
| - '@service_container' | |
| - '@app.doctrine.repository.factory.default' | |
| app.doctrine.repository.factory.default: | |
| class: Doctrine\ORM\Repository\DefaultRepositoryFactory | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
since symfony 2.6
setFactoryMethodandsetFactoryServiceare deprecated in favor ofsetFactorysee http://symfony.com/doc/current/components/dependency_injection/factories.html