ModelFactory.php 863 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. namespace Libs;
  3. use Clue\React\SQLite\Factory as SqliteFactory;
  4. use Clue\React\SQLite\DatabaseInterface;
  5. class ModelFactory
  6. {
  7. private static ?DatabaseInterface $adb = null;
  8. private static function getDbPath(): string
  9. {
  10. // Prefer env var if present, else default to project-root/mail.db
  11. $dbFile = $_ENV['DB_FILE'] ?? 'mail.db';
  12. if ($dbFile && $dbFile[0] === '/') {
  13. return $dbFile;
  14. }
  15. return dirname(__DIR__) . DIRECTORY_SEPARATOR . $dbFile;
  16. }
  17. public static function adb(): DatabaseInterface
  18. {
  19. if (!self::$adb) {
  20. $factory = new SqliteFactory();
  21. // For clue/reactphp-sqlite ^1.7, pass filename (not DSN) and options
  22. self::$adb = $factory->openLazy(self::getDbPath(), null, ['idle' => 300]);
  23. }
  24. return self::$adb;
  25. }
  26. }