SchemaState.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Illuminate\Database\Schema;
  3. use Illuminate\Database\Connection;
  4. use Illuminate\Filesystem\Filesystem;
  5. use Symfony\Component\Process\Process;
  6. abstract class SchemaState
  7. {
  8. /**
  9. * The connection instance.
  10. *
  11. * @var \Illuminate\Database\Connection
  12. */
  13. protected $connection;
  14. /**
  15. * The filesystem instance.
  16. *
  17. * @var \Illuminate\Filesystem\Filesystem
  18. */
  19. protected $files;
  20. /**
  21. * The name of the application's migration table.
  22. *
  23. * @var string
  24. */
  25. protected $migrationTable = 'migrations';
  26. /**
  27. * The process factory callback.
  28. *
  29. * @var callable
  30. */
  31. protected $processFactory;
  32. /**
  33. * The output callable instance.
  34. *
  35. * @var callable
  36. */
  37. protected $output;
  38. /**
  39. * Create a new dumper instance.
  40. *
  41. * @param \Illuminate\Database\Connection $connection
  42. * @param \Illuminate\Filesystem\Filesystem|null $files
  43. * @param callable|null $processFactory
  44. * @return void
  45. */
  46. public function __construct(Connection $connection, ?Filesystem $files = null, ?callable $processFactory = null)
  47. {
  48. $this->connection = $connection;
  49. $this->files = $files ?: new Filesystem;
  50. $this->processFactory = $processFactory ?: function (...$arguments) {
  51. return Process::fromShellCommandline(...$arguments)->setTimeout(null);
  52. };
  53. $this->handleOutputUsing(function () {
  54. //
  55. });
  56. }
  57. /**
  58. * Dump the database's schema into a file.
  59. *
  60. * @param \Illuminate\Database\Connection $connection
  61. * @param string $path
  62. * @return void
  63. */
  64. abstract public function dump(Connection $connection, $path);
  65. /**
  66. * Load the given schema file into the database.
  67. *
  68. * @param string $path
  69. * @return void
  70. */
  71. abstract public function load($path);
  72. /**
  73. * Create a new process instance.
  74. *
  75. * @param mixed ...$arguments
  76. * @return \Symfony\Component\Process\Process
  77. */
  78. public function makeProcess(...$arguments)
  79. {
  80. return call_user_func($this->processFactory, ...$arguments);
  81. }
  82. /**
  83. * Determine if the current connection has a migration table.
  84. *
  85. * @return bool
  86. */
  87. public function hasMigrationTable(): bool
  88. {
  89. return $this->connection->getSchemaBuilder()->hasTable($this->migrationTable);
  90. }
  91. /**
  92. * Specify the name of the application's migration table.
  93. *
  94. * @param string $table
  95. * @return $this
  96. */
  97. public function withMigrationTable(string $table)
  98. {
  99. $this->migrationTable = $table;
  100. return $this;
  101. }
  102. /**
  103. * Specify the callback that should be used to handle process output.
  104. *
  105. * @param callable $output
  106. * @return $this
  107. */
  108. public function handleOutputUsing(callable $output)
  109. {
  110. $this->output = $output;
  111. return $this;
  112. }
  113. }