Command.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\migration;
  12. use InvalidArgumentException;
  13. use Phinx\Db\Adapter\AdapterFactory;
  14. abstract class Command extends \think\console\Command
  15. {
  16. public function getAdapter()
  17. {
  18. if (isset($this->adapter)) {
  19. return $this->adapter;
  20. }
  21. $options = $this->getDbConfig();
  22. $adapter = AdapterFactory::instance()->getAdapter($options['adapter'], $options);
  23. if ($adapter->hasOption('table_prefix') || $adapter->hasOption('table_suffix')) {
  24. $adapter = AdapterFactory::instance()->getWrapper('prefix', $adapter);
  25. }
  26. $this->adapter = $adapter;
  27. return $adapter;
  28. }
  29. /**
  30. * 获取数据库配置
  31. * @return array
  32. */
  33. protected function getDbConfig(): array
  34. {
  35. $default = $this->app->config->get('database.default');
  36. $config = $this->app->config->get("database.connections.{$default}");
  37. if (0 == $config['deploy']) {
  38. $dbConfig = [
  39. 'adapter' => $config['type'],
  40. 'host' => $config['hostname'],
  41. 'name' => $config['database'],
  42. 'user' => $config['username'],
  43. 'pass' => $config['password'],
  44. 'port' => $config['hostport'],
  45. 'charset' => $config['charset'],
  46. 'table_prefix' => $config['prefix'],
  47. ];
  48. } else {
  49. $dbConfig = [
  50. 'adapter' => explode(',', $config['type'])[0],
  51. 'host' => explode(',', $config['hostname'])[0],
  52. 'name' => explode(',', $config['database'])[0],
  53. 'user' => explode(',', $config['username'])[0],
  54. 'pass' => explode(',', $config['password'])[0],
  55. 'port' => explode(',', $config['hostport'])[0],
  56. 'charset' => explode(',', $config['charset'])[0],
  57. 'table_prefix' => explode(',', $config['prefix'])[0],
  58. ];
  59. }
  60. $table = $this->app->config->get('database.migration_table', 'migrations');
  61. $dbConfig['default_migration_table'] = $dbConfig['table_prefix'] . $table;
  62. return $dbConfig;
  63. }
  64. protected function verifyMigrationDirectory(string $path)
  65. {
  66. if (!is_dir($path)) {
  67. throw new InvalidArgumentException(sprintf('Migration directory "%s" does not exist', $path));
  68. }
  69. if (!is_writable($path)) {
  70. throw new InvalidArgumentException(sprintf('Migration directory "%s" is not writable', $path));
  71. }
  72. }
  73. }