ContainerRepositoryFactory.php 3.5 KB

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