File tree Expand file tree Collapse file tree 4 files changed +118
-0
lines changed Expand file tree Collapse file tree 4 files changed +118
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ use Dotenv \Dotenv ;
4+ use React \EventLoop \Loop ;
5+ use Saraf \QB \QueryBuilder \Core \DBFactory ;
6+ use Saraf \QB \QueryBuilder \Facades \Database ;
7+
8+ $ loop = Loop::get ();
9+
10+ // Environments
11+ $ env = Dotenv::createImmutable (__DIR__ . "/../ " );
12+ $ env ->load ();
13+
14+ // Env Loader
15+ $ DB_NAME = $ _ENV ['DB_NAME ' ];
16+ $ DB_USER = $ _ENV ['DB_USER ' ];
17+ $ DB_PASS = $ _ENV ['DB_PASS ' ];
18+ $ DB_HOST = $ _ENV ['DB_HOST ' ];
19+ $ DB_PORT_READ = $ _ENV ['DB_PORT_READ ' ];
20+ $ DB_PORT_WRITE = $ _ENV ['DB_PORT_WRITE ' ];
21+
22+
23+
24+ Database::initConnection (
25+ new DBFactory (
26+ $ loop ,
27+ $ DB_HOST ,
28+ $ DB_NAME ,
29+ $ DB_USER ,
30+ $ DB_PASS ,
31+ $ DB_PORT_WRITE ,
32+ $ DB_PORT_READ ,
33+ 5 ,
34+ 5 ,
35+ 2 ,
36+ 2
37+ )
38+ );
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Saraf \QB \QueryBuilder \Abstracts ;
4+
5+ abstract class Facade
6+ {
7+ protected static function getFacadeAccessor ()
8+ {
9+ throw new \RuntimeException ("Facade access not implemented " );
10+ }
11+
12+ public static function __callStatic (string $ name , array $ arguments )
13+ {
14+ $ accessor = static ::getFacadeAccessor ();
15+
16+ return $ accessor ->$ name (...$ arguments );
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Saraf \QB \QueryBuilder \Facades ;
4+
5+ use Saraf \QB \QueryBuilder \Abstracts \Facade ;
6+ use Saraf \QB \QueryBuilder \Core \DBFactory ;
7+ use Saraf \QB \QueryBuilder \QueryBuilder ;
8+ use Saraf \QB \QueryBuilder \QueryBuilderManager ;
9+
10+ /**
11+ * @method static void initConnection(DBFactory $DBFactory)
12+ * @method static QueryBuilder query()
13+ */
14+ class Database extends Facade
15+ {
16+ protected static function getFacadeAccessor (): QueryBuilderManager
17+ {
18+ return QueryBuilderManager::instance ();
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Saraf \QB \QueryBuilder ;
4+
5+ use Saraf \QB \QueryBuilder \Core \DBFactory ;
6+ use Saraf \QB \QueryBuilder \Exceptions \DBFactoryException ;
7+
8+ class QueryBuilderManager
9+ {
10+ private static ?self $ instance = null ;
11+ private DBFactory $ connection ;
12+
13+ private function __construct ()
14+ {
15+ }
16+
17+ public static function instance (): QueryBuilderManager
18+ {
19+ if (is_null (self ::$ instance )) {
20+
21+ self ::$ instance = new self ();
22+ }
23+
24+ return self ::$ instance ;
25+ }
26+
27+ public function initConnection (
28+ DBFactory $ DBFactory ,
29+ ): void
30+ {
31+ $ this ->connection = $ DBFactory ;
32+ }
33+
34+
35+ /**
36+ * @throws DBFactoryException
37+ */
38+ public function query (): QueryBuilder
39+ {
40+ return $ this ->connection ->getQueryBuilder ();
41+ }
42+ }
You can’t perform that action at this time.
0 commit comments