ConfigurationArray.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations\Configuration\Migration;
  4. use Closure;
  5. use Doctrine\Migrations\Configuration\Configuration;
  6. use Doctrine\Migrations\Configuration\Migration\Exception\InvalidConfigurationKey;
  7. use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
  8. use Doctrine\Migrations\Tools\BooleanStringFormatter;
  9. use function assert;
  10. use function call_user_func;
  11. use function is_array;
  12. use function is_bool;
  13. use function is_callable;
  14. final class ConfigurationArray implements ConfigurationLoader
  15. {
  16. /** @var array<string,mixed> */
  17. private $configurations;
  18. /**
  19. * @param array<string,mixed> $configurations
  20. */
  21. public function __construct(array $configurations)
  22. {
  23. $this->configurations = $configurations;
  24. }
  25. public function getConfiguration(): Configuration
  26. {
  27. $configMap = [
  28. 'migrations_paths' => static function ($paths, Configuration $configuration): void {
  29. foreach ($paths as $namespace => $path) {
  30. $configuration->addMigrationsDirectory($namespace, $path);
  31. }
  32. },
  33. 'migrations' => static function ($migrations, Configuration $configuration): void {
  34. foreach ($migrations as $className) {
  35. $configuration->addMigrationClass($className);
  36. }
  37. },
  38. 'connection' => 'setConnectionName',
  39. 'em' => 'setEntityManagerName',
  40. 'table_storage' => [
  41. 'table_name' => 'setTableName',
  42. 'version_column_name' => 'setVersionColumnName',
  43. 'version_column_length' => static function ($value, TableMetadataStorageConfiguration $configuration): void {
  44. $configuration->setVersionColumnLength((int) $value);
  45. },
  46. 'executed_at_column_name' => 'setExecutedAtColumnName',
  47. 'execution_time_column_name' => 'setExecutionTimeColumnName',
  48. ],
  49. 'organize_migrations' => 'setMigrationOrganization',
  50. 'custom_template' => 'setCustomTemplate',
  51. 'all_or_nothing' => static function ($value, Configuration $configuration): void {
  52. $configuration->setAllOrNothing(is_bool($value) ? $value : BooleanStringFormatter::toBoolean($value, false));
  53. },
  54. 'check_database_platform' => static function ($value, Configuration $configuration): void {
  55. $configuration->setCheckDatabasePlatform(is_bool($value) ? $value : BooleanStringFormatter::toBoolean($value, false));
  56. },
  57. ];
  58. $object = new Configuration();
  59. self::applyConfigs($configMap, $object, $this->configurations);
  60. if ($object->getMetadataStorageConfiguration() === null) {
  61. $object->setMetadataStorageConfiguration(new TableMetadataStorageConfiguration());
  62. }
  63. return $object;
  64. }
  65. /**
  66. * @param mixed[] $configMap
  67. * @param Configuration|TableMetadataStorageConfiguration $object
  68. * @param array<string|int,mixed> $data
  69. */
  70. private static function applyConfigs(array $configMap, $object, array $data): void
  71. {
  72. foreach ($data as $configurationKey => $configurationValue) {
  73. if (! isset($configMap[$configurationKey])) {
  74. throw InvalidConfigurationKey::new((string) $configurationKey);
  75. }
  76. if (is_array($configMap[$configurationKey])) {
  77. if ($configurationKey !== 'table_storage') {
  78. throw InvalidConfigurationKey::new((string) $configurationKey);
  79. }
  80. $storageConfig = new TableMetadataStorageConfiguration();
  81. assert($object instanceof Configuration);
  82. $object->setMetadataStorageConfiguration($storageConfig);
  83. self::applyConfigs($configMap[$configurationKey], $storageConfig, $configurationValue);
  84. } else {
  85. $callable = $configMap[$configurationKey] instanceof Closure
  86. ? $configMap[$configurationKey]
  87. : [$object, $configMap[$configurationKey]];
  88. assert(is_callable($callable));
  89. call_user_func(
  90. $callable,
  91. $configurationValue,
  92. $object,
  93. $data
  94. );
  95. }
  96. }
  97. }
  98. }