Configuration.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations\Configuration;
  4. use Doctrine\Migrations\Configuration\Exception\FrozenConfiguration;
  5. use Doctrine\Migrations\Configuration\Exception\UnknownConfigurationValue;
  6. use Doctrine\Migrations\Exception\MigrationException;
  7. use Doctrine\Migrations\Metadata\Storage\MetadataStorageConfiguration;
  8. use function strtolower;
  9. /**
  10. * The Configuration class is responsible for defining migration configuration information.
  11. */
  12. final class Configuration
  13. {
  14. public const VERSIONS_ORGANIZATION_NONE = 'none';
  15. public const VERSIONS_ORGANIZATION_BY_YEAR = 'year';
  16. public const VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH = 'year_and_month';
  17. /** @var array<string, string> */
  18. private $migrationsDirectories = [];
  19. /** @var string[] */
  20. private $migrationClasses = [];
  21. /** @var bool */
  22. private $migrationsAreOrganizedByYear = false;
  23. /** @var bool */
  24. private $migrationsAreOrganizedByYearAndMonth = false;
  25. /** @var string|null */
  26. private $customTemplate;
  27. /** @var bool */
  28. private $isDryRun = false;
  29. /** @var bool */
  30. private $allOrNothing = false;
  31. /** @var string|null */
  32. private $connectionName;
  33. /** @var string|null */
  34. private $entityManagerName;
  35. /** @var bool */
  36. private $checkDbPlatform = true;
  37. /** @var MetadataStorageConfiguration */
  38. private $metadataStorageConfiguration;
  39. /** @var bool */
  40. private $frozen = false;
  41. public function freeze(): void
  42. {
  43. $this->frozen = true;
  44. }
  45. private function assertNotFrozen(): void
  46. {
  47. if ($this->frozen) {
  48. throw FrozenConfiguration::new();
  49. }
  50. }
  51. public function setMetadataStorageConfiguration(MetadataStorageConfiguration $metadataStorageConfiguration): void
  52. {
  53. $this->assertNotFrozen();
  54. $this->metadataStorageConfiguration = $metadataStorageConfiguration;
  55. }
  56. /**
  57. * @return string[]
  58. */
  59. public function getMigrationClasses(): array
  60. {
  61. return $this->migrationClasses;
  62. }
  63. public function addMigrationClass(string $className): void
  64. {
  65. $this->assertNotFrozen();
  66. $this->migrationClasses[] = $className;
  67. }
  68. public function getMetadataStorageConfiguration(): ?MetadataStorageConfiguration
  69. {
  70. return $this->metadataStorageConfiguration;
  71. }
  72. public function addMigrationsDirectory(string $namespace, string $path): void
  73. {
  74. $this->assertNotFrozen();
  75. $this->migrationsDirectories[$namespace] = $path;
  76. }
  77. /**
  78. * @return array<string,string>
  79. */
  80. public function getMigrationDirectories(): array
  81. {
  82. return $this->migrationsDirectories;
  83. }
  84. public function getConnectionName(): ?string
  85. {
  86. return $this->connectionName;
  87. }
  88. public function setConnectionName(?string $connectionName): void
  89. {
  90. $this->assertNotFrozen();
  91. $this->connectionName = $connectionName;
  92. }
  93. public function getEntityManagerName(): ?string
  94. {
  95. return $this->entityManagerName;
  96. }
  97. public function setEntityManagerName(?string $entityManagerName): void
  98. {
  99. $this->assertNotFrozen();
  100. $this->entityManagerName = $entityManagerName;
  101. }
  102. public function setCustomTemplate(?string $customTemplate): void
  103. {
  104. $this->assertNotFrozen();
  105. $this->customTemplate = $customTemplate;
  106. }
  107. public function getCustomTemplate(): ?string
  108. {
  109. return $this->customTemplate;
  110. }
  111. public function areMigrationsOrganizedByYear(): bool
  112. {
  113. return $this->migrationsAreOrganizedByYear;
  114. }
  115. /**
  116. * @throws MigrationException
  117. */
  118. public function setMigrationsAreOrganizedByYear(
  119. bool $migrationsAreOrganizedByYear = true
  120. ): void {
  121. $this->assertNotFrozen();
  122. $this->migrationsAreOrganizedByYear = $migrationsAreOrganizedByYear;
  123. }
  124. /**
  125. * @throws MigrationException
  126. */
  127. public function setMigrationsAreOrganizedByYearAndMonth(
  128. bool $migrationsAreOrganizedByYearAndMonth = true
  129. ): void {
  130. $this->assertNotFrozen();
  131. $this->migrationsAreOrganizedByYear = $migrationsAreOrganizedByYearAndMonth;
  132. $this->migrationsAreOrganizedByYearAndMonth = $migrationsAreOrganizedByYearAndMonth;
  133. }
  134. public function areMigrationsOrganizedByYearAndMonth(): bool
  135. {
  136. return $this->migrationsAreOrganizedByYearAndMonth;
  137. }
  138. public function setIsDryRun(bool $isDryRun): void
  139. {
  140. $this->assertNotFrozen();
  141. $this->isDryRun = $isDryRun;
  142. }
  143. public function isDryRun(): bool
  144. {
  145. return $this->isDryRun;
  146. }
  147. public function setAllOrNothing(bool $allOrNothing): void
  148. {
  149. $this->assertNotFrozen();
  150. $this->allOrNothing = $allOrNothing;
  151. }
  152. public function isAllOrNothing(): bool
  153. {
  154. return $this->allOrNothing;
  155. }
  156. public function setCheckDatabasePlatform(bool $checkDbPlatform): void
  157. {
  158. $this->checkDbPlatform = $checkDbPlatform;
  159. }
  160. public function isDatabasePlatformChecked(): bool
  161. {
  162. return $this->checkDbPlatform;
  163. }
  164. public function setMigrationOrganization(string $migrationOrganization): void
  165. {
  166. $this->assertNotFrozen();
  167. switch (strtolower($migrationOrganization)) {
  168. case self::VERSIONS_ORGANIZATION_NONE:
  169. $this->setMigrationsAreOrganizedByYearAndMonth(false);
  170. break;
  171. case self::VERSIONS_ORGANIZATION_BY_YEAR:
  172. $this->setMigrationsAreOrganizedByYear();
  173. break;
  174. case self::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH:
  175. $this->setMigrationsAreOrganizedByYearAndMonth();
  176. break;
  177. default:
  178. throw UnknownConfigurationValue::new('organize_migrations', $migrationOrganization);
  179. }
  180. }
  181. }