vendor/doctrine/doctrine-bundle/Repository/ContainerRepositoryFactory.php line 35

Open in your IDE?
  1. <?php
  2. namespace Doctrine\Bundle\DoctrineBundle\Repository;
  3. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Doctrine\ORM\Mapping\ClassMetadata;
  6. use Doctrine\ORM\Repository\RepositoryFactory;
  7. use Doctrine\Persistence\ObjectRepository;
  8. use Psr\Container\ContainerInterface;
  9. use RuntimeException;
  10. /**
  11.  * Fetches repositories from the container or falls back to normal creation.
  12.  */
  13. final class ContainerRepositoryFactory implements RepositoryFactory
  14. {
  15.     /** @var ObjectRepository[] */
  16.     private $managedRepositories = [];
  17.     /** @var ContainerInterface */
  18.     private $container;
  19.     /**
  20.      * @param ContainerInterface $container A service locator containing the repositories
  21.      */
  22.     public function __construct(ContainerInterface $container)
  23.     {
  24.         $this->container $container;
  25.     }
  26.     /**
  27.      * {@inheritdoc}
  28.      */
  29.     public function getRepository(EntityManagerInterface $entityManager$entityName) : ObjectRepository
  30.     {
  31.         $metadata            $entityManager->getClassMetadata($entityName);
  32.         $repositoryServiceId $metadata->customRepositoryClassName;
  33.         $customRepositoryName $metadata->customRepositoryClassName;
  34.         if ($customRepositoryName !== null) {
  35.             // fetch from the container
  36.             if ($this->container->has($customRepositoryName)) {
  37.                 $repository $this->container->get($customRepositoryName);
  38.                 if (! $repository instanceof ObjectRepository) {
  39.                     throw new RuntimeException(sprintf('The service "%s" must implement ObjectRepository (or extend a base class, like ServiceEntityRepository).'$repositoryServiceId));
  40.                 }
  41.                 return $repository;
  42.             }
  43.             // if not in the container but the class/id implements the interface, throw an error
  44.             if (is_a($customRepositoryNameServiceEntityRepositoryInterface::class, true)) {
  45.                 throw new RuntimeException(sprintf('The "%s" entity repository implements "%s", but its service could not be found. Make sure the service exists and is tagged with "%s".'$customRepositoryNameServiceEntityRepositoryInterface::class, ServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG));
  46.             }
  47.             if (! class_exists($customRepositoryName)) {
  48.                 throw new RuntimeException(sprintf('The "%s" entity has a repositoryClass set to "%s", but this is not a valid class. Check your class naming. If this is meant to be a service id, make sure this service exists and is tagged with "%s".'$metadata->name$customRepositoryNameServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG));
  49.             }
  50.             // allow the repository to be created below
  51.         }
  52.         return $this->getOrCreateRepository($entityManager$metadata);
  53.     }
  54.     private function getOrCreateRepository(
  55.         EntityManagerInterface $entityManager,
  56.         ClassMetadata $metadata
  57.     ) : ObjectRepository {
  58.         $repositoryHash $metadata->getName() . spl_object_hash($entityManager);
  59.         if (isset($this->managedRepositories[$repositoryHash])) {
  60.             return $this->managedRepositories[$repositoryHash];
  61.         }
  62.         $repositoryClassName $metadata->customRepositoryClassName ?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();
  63.         return $this->managedRepositories[$repositoryHash] = new $repositoryClassName($entityManager$metadata);
  64.     }
  65. }