Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

101 рядки
2.9KB

  1. <?php
  2. namespace Framework\Database\Command;
  3. use Framework\Database\Factory;
  4. use Framework\Database\Connection\Connection;
  5. use Framework\Database\Connection\MysqlConnection;
  6. use Framework\Database\Connection\SqliteConnection;
  7. use Symfony\Component\Console\Command\Command;
  8. use Symfony\Component\Console\Input\InputArgument;
  9. use Symfony\Component\Console\Input\InputInterface;
  10. use Symfony\Component\Console\Input\InputOption;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. class MigrateCommand extends Command
  13. {
  14. protected static $defaultName = 'migrate';
  15. protected function configure()
  16. {
  17. $this
  18. ->setDescription('Migrates the database')
  19. ->addOption('fresh', null, InputOption::VALUE_NONE, 'Delete all tables before running the migrations')
  20. ->setHelp('This command looks for all migration files and runs them');
  21. }
  22. protected function execute(InputInterface $input, OutputInterface $output)
  23. {
  24. $current = getcwd();
  25. $pattern = 'database/migrations/*.php';
  26. $paths = glob("{$current}/{$pattern}");
  27. if (count($paths) < 1) {
  28. $this->writeln('No migrations found');
  29. return Command::SUCCESS;
  30. }
  31. // $connection = $this->connection();
  32. $connection = app('database');
  33. if ($input->getOption('fresh')) {
  34. $output->writeln('Dropping existing database tables');
  35. $connection->dropTables();
  36. // $connection = $this->connection();
  37. $connection = app('database');
  38. }
  39. if (!$connection->hasTable('migrations')) {
  40. $output->writeln('Creating migrations table');
  41. $this->createMigrationsTable($connection);
  42. }
  43. foreach ($paths as $path) {
  44. [$prefix, $file] = explode('_', $path);
  45. [$class, $extension] = explode('.', $file);
  46. require $path;
  47. $output->writeln("Migrating: {$class}");
  48. $obj = new $class();
  49. $obj->migrate($connection);
  50. $connection
  51. ->query()
  52. ->from('migrations')
  53. ->insert(['name'], ['name' => $class]);
  54. }
  55. return Command::SUCCESS;
  56. }
  57. // private function connection(): Connection
  58. // {
  59. // $factory = new Factory();
  60. // $factory->addConnector('mysql', function($config) {
  61. // return new MysqlConnection($config);
  62. // });
  63. // $factory->addConnector('sqlite', function($config) {
  64. // return new SqliteConnection($config);
  65. // });
  66. // $config = require getcwd() . '/config/database.php';
  67. // return $factory->connect($config[$config['default']]);
  68. // }
  69. private function createMigrationsTable(Connection $connection)
  70. {
  71. $table = $connection->createTable('migrations');
  72. $table->id('id');
  73. $table->string('name');
  74. $table->execute();
  75. }
  76. }

Powered by TurnKey Linux.