Schema.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Database\Schema;
  12. use Hyperf\Context\ApplicationContext;
  13. use Hyperf\Database\ConnectionInterface;
  14. use Hyperf\Database\ConnectionResolverInterface;
  15. /**
  16. * @method static bool hasTable(string $table)
  17. * @method static array getColumnListing(string $table)
  18. * @method static array getColumnTypeListing(string $table)
  19. * @method static void dropAllTables()
  20. * @method static void dropAllViews()
  21. * @method static array getAllTables()
  22. * @method static array getAllViews()
  23. * @method static bool hasColumn(string $table, string $column)
  24. * @method static bool hasColumns(string $table, array $columns)
  25. * @method static string getColumnType(string $table, string $column)
  26. * @method static void table(string $table, \Closure $callback)
  27. * @method static void create(string $table, \Closure $callback))
  28. * @method static void drop(string $table)
  29. * @method static void dropIfExists(string $table)
  30. * @method static void rename(string $from, string $to)
  31. * @method static bool enableForeignKeyConstraints()
  32. * @method static bool disableForeignKeyConstraints()
  33. * @method static \Hyperf\Database\Connection getConnection()
  34. * @method static Builder setConnection(\Hyperf\Database\Connection $connection)
  35. * @method static void blueprintResolver(\Closure $resolver)
  36. */
  37. class Schema
  38. {
  39. public static function __callStatic($name, $arguments)
  40. {
  41. $container = ApplicationContext::getContainer();
  42. $resolver = $container->get(ConnectionResolverInterface::class);
  43. $connection = $resolver->connection();
  44. return $connection->getSchemaBuilder()->{$name}(...$arguments);
  45. }
  46. public function __call($name, $arguments)
  47. {
  48. return self::__callStatic($name, $arguments);
  49. }
  50. /**
  51. * Create a connection by ConnectionResolver.
  52. */
  53. public function connection(string $name = 'default'): ConnectionInterface
  54. {
  55. $container = ApplicationContext::getContainer();
  56. $resolver = $container->get(ConnectionResolverInterface::class);
  57. return $resolver->connection($name);
  58. }
  59. }