JsonFile.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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\JsonNotValid;
  7. use function assert;
  8. use function file_exists;
  9. use function file_get_contents;
  10. use function json_decode;
  11. use function json_last_error;
  12. use const JSON_ERROR_NONE;
  13. final class JsonFile extends ConfigurationFile
  14. {
  15. public function getConfiguration(): Configuration
  16. {
  17. if (! file_exists($this->file)) {
  18. throw FileNotFound::new($this->file);
  19. }
  20. $contents = file_get_contents($this->file);
  21. assert($contents !== false);
  22. $config = json_decode($contents, true);
  23. if (json_last_error() !== JSON_ERROR_NONE) {
  24. throw JsonNotValid::new();
  25. }
  26. if (isset($config['migrations_paths'])) {
  27. $config['migrations_paths'] = $this->getDirectoriesRelativeToFile(
  28. $config['migrations_paths'],
  29. $this->file
  30. );
  31. }
  32. return (new ConfigurationArray($config))->getConfiguration();
  33. }
  34. }