ConfigurationFile.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations\Configuration\EntityManager;
  4. use Doctrine\Migrations\Configuration\EntityManager\Exception\FileNotFound;
  5. use Doctrine\Migrations\Configuration\EntityManager\Exception\InvalidConfiguration;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use InvalidArgumentException;
  8. use function file_exists;
  9. /**
  10. * This class will return an EntityManager instance, loaded from a configuration file provided as argument.
  11. */
  12. final class ConfigurationFile implements EntityManagerLoader
  13. {
  14. /** @var string */
  15. private $filename;
  16. public function __construct(string $filename)
  17. {
  18. $this->filename = $filename;
  19. }
  20. /**
  21. * Read the input and return a Configuration, returns null if the config
  22. * is not supported.
  23. *
  24. * @throws InvalidConfiguration
  25. */
  26. public function getEntityManager(?string $name = null): EntityManagerInterface
  27. {
  28. if ($name !== null) {
  29. throw new InvalidArgumentException('Only one connection is supported');
  30. }
  31. if (! file_exists($this->filename)) {
  32. throw FileNotFound::new($this->filename);
  33. }
  34. $params = include $this->filename;
  35. if ($params instanceof EntityManagerInterface) {
  36. return $params;
  37. }
  38. if ($params instanceof EntityManagerLoader) {
  39. return $params->getEntityManager();
  40. }
  41. throw InvalidConfiguration::invalidArrayConfiguration();
  42. }
  43. }