DB.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Utils;
  3. use AbmmHasan\UUID\GenerateUuid;
  4. use App\Utils\Config;
  5. use Riverside\Orm\Configuration;
  6. use Riverside\Orm\Connection;
  7. class DB
  8. {
  9. private const configFilename = 'database';
  10. private const database = 'default';
  11. private $configuration;
  12. private $connection;
  13. public function __construct()
  14. {
  15. $this->mount();
  16. }
  17. private function mount()
  18. {
  19. $config = Config::getFromFilename(self::configFilename);
  20. $options = $config[self::database];
  21. $this->configuration = new Configuration(
  22. $options['username'],
  23. $options['password'],
  24. $options['database'],
  25. $options['host'],
  26. $options['port'],
  27. $options['driver'],
  28. $options['charset'],
  29. $options['collation']
  30. );
  31. $this->connection = new Connection($this->configuration);
  32. }
  33. /*
  34. * Obtiene opciones de configuración
  35. * de la base de datos.
  36. */
  37. public function getConfig()
  38. {
  39. return $this->configuration;
  40. }
  41. /*
  42. * Obtiene opciones de configuración
  43. * de conexión de la base de datos.
  44. */
  45. public function getConnection()
  46. {
  47. return $this->connection;
  48. }
  49. /*
  50. * Obtiene el datetime actual.
  51. */
  52. public static function datetime()
  53. {
  54. return date('Y-m-d H:i:s');
  55. }
  56. /*
  57. * Genera un UUID v4 aleatorio.
  58. */
  59. public static function generateUuid()
  60. {
  61. return GenerateUuid::v4();
  62. }
  63. }