ConcatenationFileBuilder.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations\Generator;
  4. use DateTimeImmutable;
  5. use DateTimeInterface;
  6. use Doctrine\Migrations\Query\Query;
  7. use function sprintf;
  8. /**
  9. * The ConcatenationFileBuilder class is responsible for building a migration SQL file from an array of queries per version.
  10. *
  11. * @internal
  12. */
  13. final class ConcatenationFileBuilder implements FileBuilder
  14. {
  15. /** @param array<string,Query[]> $queriesByVersion */
  16. public function buildMigrationFile(
  17. array $queriesByVersion,
  18. string $direction,
  19. ?DateTimeInterface $now = null
  20. ): string {
  21. $now = $now ?? new DateTimeImmutable();
  22. $string = sprintf("-- Doctrine Migration File Generated on %s\n", $now->format('Y-m-d H:i:s'));
  23. foreach ($queriesByVersion as $version => $queries) {
  24. $string .= "\n-- Version " . $version . "\n";
  25. foreach ($queries as $query) {
  26. $string .= $query->getStatement() . ";\n";
  27. }
  28. }
  29. return $string;
  30. }
  31. }