Command.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. use think\Config;
  15. use think\Db;
  16. abstract class Command extends \think\console\Command
  17. {
  18. protected $config = 'database';
  19. public function getAdapter()
  20. {
  21. if (isset($this->adapter)) {
  22. return $this->adapter;
  23. }
  24. $options = $this->getDbConfig();
  25. $adapter = AdapterFactory::instance()->getAdapter($options['adapter'], $options);
  26. if ($adapter->hasOption('table_prefix') || $adapter->hasOption('table_suffix')) {
  27. $adapter = AdapterFactory::instance()->getWrapper('prefix', $adapter);
  28. }
  29. $this->adapter = $adapter;
  30. return $adapter;
  31. }
  32. /**
  33. * 获取数据库配置
  34. * @return array
  35. */
  36. protected function getDbConfig()
  37. {
  38. $config = Db::connect($this->config)->getConfig();
  39. if ($config['deploy'] == 0) {
  40. $dbConfig = [
  41. 'adapter' => $config['type'],
  42. 'host' => $config['hostname'],
  43. 'name' => $config['database'],
  44. 'user' => $config['username'],
  45. 'pass' => $config['password'],
  46. 'port' => $config['hostport'],
  47. 'charset' => $config['charset'],
  48. 'table_prefix' => $config['prefix'],
  49. ];
  50. } else {
  51. $dbConfig = [
  52. 'adapter' => explode(',', $config['type'])[0],
  53. 'host' => explode(',', $config['hostname'])[0],
  54. 'name' => explode(',', $config['database'])[0],
  55. 'user' => explode(',', $config['username'])[0],
  56. 'pass' => explode(',', $config['password'])[0],
  57. 'port' => explode(',', $config['hostport'])[0],
  58. 'charset' => explode(',', $config['charset'])[0],
  59. 'table_prefix' => explode(',', $config['prefix'])[0],
  60. ];
  61. }
  62. $dbConfig['default_migration_table'] = $this->getConfig('table', $dbConfig['table_prefix'] . 'migrations');
  63. return $dbConfig;
  64. }
  65. protected function getConfig($name, $default = null)
  66. {
  67. $config = Config::get('migration');
  68. return isset($config[$name]) ? $config[$name] : $default;
  69. }
  70. protected function verifyMigrationDirectory($path)
  71. {
  72. if (!is_dir($path)) {
  73. throw new InvalidArgumentException(sprintf('Migration directory "%s" does not exist', $path));
  74. }
  75. if (!is_writable($path)) {
  76. throw new InvalidArgumentException(sprintf('Migration directory "%s" is not writable', $path));
  77. }
  78. }
  79. }