vendor/doctrine/mongodb-odm-bundle/Repository/ContainerRepositoryFactory.php line 47

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Bundle\MongoDBBundle\Repository;
  4. use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
  5. use Doctrine\ODM\MongoDB\DocumentManager;
  6. use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
  7. use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
  8. use Doctrine\ODM\MongoDB\Repository\GridFSRepository;
  9. use Doctrine\ODM\MongoDB\Repository\RepositoryFactory;
  10. use Doctrine\Persistence\ObjectRepository;
  11. use Psr\Container\ContainerInterface;
  12. use RuntimeException;
  13. use function class_exists;
  14. use function is_a;
  15. use function spl_object_hash;
  16. use function sprintf;
  17. /**
  18.  * Fetches repositories from the container or falls back to normal creation.
  19.  */
  20. final class ContainerRepositoryFactory implements RepositoryFactory
  21. {
  22.     /** @var array<string, ObjectRepository> */
  23.     private array $managedRepositories = [];
  24.     private ContainerInterface $container;
  25.     /** @param ContainerInterface $container A service locator containing the repositories */
  26.     public function __construct(ContainerInterface $container)
  27.     {
  28.         $this->container $container;
  29.     }
  30.     /**
  31.      * @psalm-param class-string<T> $documentName
  32.      *
  33.      * @psalm-return ObjectRepository<T>
  34.      *
  35.      * @template T of object
  36.      */
  37.     public function getRepository(DocumentManager $documentManagerstring $documentName): ObjectRepository
  38.     {
  39.         $metadata             $documentManager->getClassMetadata($documentName);
  40.         $customRepositoryName $metadata->customRepositoryClassName;
  41.         if ($customRepositoryName !== null) {
  42.             // fetch from the container
  43.             if ($this->container->has($customRepositoryName)) {
  44.                 /** @var ObjectRepository<T> $repository */
  45.                 $repository $this->container->get($customRepositoryName);
  46.                 if (! $repository instanceof DocumentRepository) {
  47.                     throw new RuntimeException(sprintf('The service "%s" must extend DocumentRepository (or a base class, like ServiceDocumentRepository).'$customRepositoryName));
  48.                 }
  49.                 return $repository;
  50.             }
  51.             // if not in the container but the class/id implements the interface, throw an error
  52.             if (is_a($customRepositoryNameServiceDocumentRepositoryInterface::class, true)) {
  53.                 throw new RuntimeException(sprintf('The "%s" document repository implements "%s", but its service could not be found. Make sure the service exists and is tagged with "%s".'$customRepositoryNameServiceDocumentRepositoryInterface::class, ServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG));
  54.             }
  55.             if (! class_exists($customRepositoryName)) {
  56.                 throw new RuntimeException(sprintf('The "%s" document 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));
  57.             }
  58.             // allow the repository to be created below
  59.         }
  60.         return $this->getOrCreateRepository($documentManager$metadata);
  61.     }
  62.     /**
  63.      * @psalm-param ClassMetadata<T> $metadata
  64.      *
  65.      * @psalm-return ObjectRepository<T>
  66.      *
  67.      * @template T of object
  68.      */
  69.     private function getOrCreateRepository(DocumentManager $documentManagerClassMetadata $metadata): ObjectRepository
  70.     {
  71.         $repositoryHash $metadata->getName() . spl_object_hash($documentManager);
  72.         if (isset($this->managedRepositories[$repositoryHash])) {
  73.             /** @psalm-var ObjectRepository<T> */
  74.             return $this->managedRepositories[$repositoryHash];
  75.         }
  76.         if ($metadata->customRepositoryClassName) {
  77.             $repositoryClassName $metadata->customRepositoryClassName;
  78.         } elseif ($metadata->isFile) {
  79.             /** @psalm-var class-string<GridFSRepository<T>> $repositoryClassName */
  80.             $repositoryClassName $documentManager->getConfiguration()->getDefaultGridFSRepositoryClassName();
  81.         } else {
  82.             /** @psalm-var class-string<ObjectRepository<T>> $repositoryClassName */
  83.             $repositoryClassName $documentManager->getConfiguration()->getDefaultDocumentRepositoryClassName();
  84.         }
  85.         return $this->managedRepositories[$repositoryHash] = new $repositoryClassName($documentManager$documentManager->getUnitOfWork(), $metadata);
  86.     }
  87. }