YamlFile.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations\Configuration\Migration;
  4. use Doctrine\Migrations\Configuration\Configuration;
  5. use Doctrine\Migrations\Configuration\Exception\FileNotFound;
  6. use Doctrine\Migrations\Configuration\Migration\Exception\YamlNotAvailable;
  7. use Doctrine\Migrations\Configuration\Migration\Exception\YamlNotValid;
  8. use Symfony\Component\Yaml\Exception\ParseException;
  9. use Symfony\Component\Yaml\Yaml;
  10. use function assert;
  11. use function class_exists;
  12. use function file_exists;
  13. use function file_get_contents;
  14. use function is_array;
  15. final class YamlFile extends ConfigurationFile
  16. {
  17. public function getConfiguration(): Configuration
  18. {
  19. if (! class_exists(Yaml::class)) {
  20. throw YamlNotAvailable::new();
  21. }
  22. if (! file_exists($this->file)) {
  23. throw FileNotFound::new($this->file);
  24. }
  25. $content = file_get_contents($this->file);
  26. assert($content !== false);
  27. try {
  28. $config = Yaml::parse($content);
  29. } catch (ParseException $e) {
  30. throw YamlNotValid::malformed();
  31. }
  32. if (! is_array($config)) {
  33. throw YamlNotValid::invalid();
  34. }
  35. if (isset($config['migrations_paths'])) {
  36. $config['migrations_paths'] = $this->getDirectoriesRelativeToFile(
  37. $config['migrations_paths'],
  38. $this->file
  39. );
  40. }
  41. return (new ConfigurationArray($config))->getConfiguration();
  42. }
  43. }