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

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 InvalidArgumentException;
  9. use Psr\Container\ContainerInterface;
  10. use RuntimeException;
  11. /**
  12.  * Fetches repositories from the container or falls back to normal creation.
  13.  */
  14. final class ContainerRepositoryFactory implements RepositoryFactory
  15. {
  16.     /** @var ObjectRepository[] */
  17.     private $managedRepositories = [];
  18.     /** @var ContainerInterface|null */
  19.     private $container;
  20.     /**
  21.      * @param ContainerInterface $container A service locator containing the repositories
  22.      */
  23.     public function __construct(ContainerInterface $container null)
  24.     {
  25.         // When DoctrineBundle requires Symfony 3.3+, this can be removed
  26.         // and the $container argument can become required.
  27.         if ($container === null) {
  28.             throw new InvalidArgumentException(sprintf('The first argument of %s::__construct() is required on Symfony 3.3 or higher.'self::class));
  29.         }
  30.         $this->container $container;
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public function getRepository(EntityManagerInterface $entityManager$entityName) : ObjectRepository
  36.     {
  37.         $metadata            $entityManager->getClassMetadata($entityName);
  38.         $repositoryServiceId $metadata->customRepositoryClassName;
  39.         $customRepositoryName $metadata->customRepositoryClassName;
  40.         if ($customRepositoryName !== null) {
  41.             // fetch from the container
  42.             if ($this->container && $this->container->has($customRepositoryName)) {
  43.                 $repository $this->container->get($customRepositoryName);
  44.                 if (! $repository instanceof ObjectRepository) {
  45.                     throw new RuntimeException(sprintf('The service "%s" must implement ObjectRepository (or extend a base class, like ServiceEntityRepository).'$repositoryServiceId));
  46.                 }
  47.                 return $repository;
  48.             }
  49.             // if not in the container but the class/id implements the interface, throw an error
  50.             if (is_a($customRepositoryNameServiceEntityRepositoryInterface::class, true)) {
  51.                 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));
  52.             }
  53.             if (! class_exists($customRepositoryName)) {
  54.                 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));
  55.             }
  56.             // allow the repository to be created below
  57.         }
  58.         return $this->getOrCreateRepository($entityManager$metadata);
  59.     }
  60.     private function getOrCreateRepository(
  61.         EntityManagerInterface $entityManager,
  62.         ClassMetadata $metadata
  63.     ) : ObjectRepository {
  64.         $repositoryHash $metadata->getName() . spl_object_hash($entityManager);
  65.         if (isset($this->managedRepositories[$repositoryHash])) {
  66.             return $this->managedRepositories[$repositoryHash];
  67.         }
  68.         $repositoryClassName $metadata->customRepositoryClassName ?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();
  69.         return $this->managedRepositories[$repositoryHash] = new $repositoryClassName($entityManager$metadata);
  70.     }
  71. }