TableManager.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace ba;
  3. use Throwable;
  4. use think\facade\Db;
  5. use think\facade\Config;
  6. use think\migration\db\Table;
  7. use Phinx\Db\Adapter\AdapterFactory;
  8. use Phinx\Db\Adapter\AdapterInterface;
  9. /**
  10. * 数据表管理类
  11. * @example TableManager::instance('test_table', ['comment' => '测试表'])->create();
  12. */
  13. class TableManager
  14. {
  15. protected static ?AdapterInterface $adapter = null;
  16. protected static ?AdapterInterface $wrapper = null;
  17. /**
  18. * 静态的 Phinx/Db/Table 实例列表
  19. * @var array
  20. * @uses Table 数组项
  21. */
  22. protected static array $instances = [];
  23. /**
  24. * 返回一个 Phinx/Db/Table 实例 用于操作数据表
  25. * @param string $table 表名
  26. * @param array $options 传递给 Phinx/Db/Table 的 options
  27. * @param bool $prefixWrapper 是否使用表前缀包装表名
  28. * @return Table
  29. */
  30. public static function instance(string $table, array $options = [], bool $prefixWrapper = true): Table
  31. {
  32. if (array_key_exists($table, self::$instances)) {
  33. return self::$instances[$table];
  34. }
  35. self::adapter($prefixWrapper);
  36. self::$instances[$table] = new Table($table, $options, $prefixWrapper ? self::$wrapper : self::$adapter);
  37. return self::$instances[$table];
  38. }
  39. /**
  40. * 返回一个 Phinx\Db\Adapter\AdapterFactory 实例
  41. * @param bool $prefixWrapper 是否使用表前缀包装表名
  42. * @return AdapterInterface
  43. */
  44. public static function adapter(bool $prefixWrapper = true): AdapterInterface
  45. {
  46. if (is_null(self::$adapter)) {
  47. $config = static::getDbConfig();
  48. $factory = AdapterFactory::instance();
  49. self::$adapter = $factory->getAdapter($config['adapter'], $config);
  50. if ($prefixWrapper && is_null(self::$wrapper)) {
  51. self::$wrapper = $factory->getWrapper('prefix', self::$adapter);
  52. }
  53. }
  54. return $prefixWrapper ? self::$wrapper : self::$adapter;
  55. }
  56. /**
  57. * 修改已有数据表的注释
  58. * Phinx只在新增表时可以设置注释
  59. * @param string $name
  60. * @param string $comment
  61. * @return bool
  62. */
  63. public static function changeComment(string $name, string $comment): bool
  64. {
  65. $name = self::tableName($name);
  66. try {
  67. $sql = "ALTER TABLE `$name` COMMENT = '$comment'";
  68. Db::execute($sql);
  69. } catch (Throwable) {
  70. return false;
  71. }
  72. return true;
  73. }
  74. /**
  75. * 数据表名
  76. * @param string $table 表名,带不带前缀均可
  77. * @param bool $fullName 是否返回带前缀的表名
  78. * @return string 表名
  79. */
  80. public static function tableName(string $table, bool $fullName = true): string
  81. {
  82. $tablePrefix = config('database.connections.mysql.prefix');
  83. $pattern = '/^' . $tablePrefix . '/i';
  84. return ($fullName ? $tablePrefix : '') . (preg_replace($pattern, '', $table));
  85. }
  86. /**
  87. * 获取数据库配置
  88. * @return array
  89. */
  90. protected static function getDbConfig(): array
  91. {
  92. $default = Config::get('database.default');
  93. $config = Config::get("database.connections.$default");
  94. if ($config['deploy'] == 0) {
  95. $dbConfig = [
  96. 'adapter' => $config['type'],
  97. 'connection' => Db::getPdo(),
  98. 'name' => $config['database'],
  99. 'table_prefix' => $config['prefix'],
  100. ];
  101. } else {
  102. $dbConfig = [
  103. 'adapter' => explode(',', $config['type'])[0],
  104. 'connection' => Db::getPdo(),
  105. 'name' => explode(',', $config['database'])[0],
  106. 'table_prefix' => explode(',', $config['prefix'])[0],
  107. ];
  108. }
  109. $table = Config::get('database.migration_table', 'migrations');
  110. $dbConfig['migration_table'] = $dbConfig['table_prefix'] . $table;
  111. return $dbConfig;
  112. }
  113. }