8000 Merge pull request #22 from InitPHP/v3.0 · InitPHP/Database@641abb0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 641abb0

Browse files
Merge pull request #22 from InitPHP/v3.0
v3.0
2 parents b3748c0 + 3c6eb38 commit 641abb0

30 files changed

+341
-5341
lines changed

README.md

Lines changed: 65 additions & 149 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "initphp/database",
3-
"description": "InitPHP DB (QueryBuilder, DBAL and ORM) Library",
3+
"description": "InitPHP DataBase (QueryBuilder, DBAL and ORM) Library",
44
"type": "library",
55
"license": "MIT",
66
"autoload": {
@@ -23,10 +23,11 @@
2323
],
2424
"minimum-stability": "stable",
2525
"require": {
26-
"php": ">=7.4",
27-
"ext-pdo": "*"
26+
"php": ">=8.0",
27+
"ext-pdo": "*",
28+
"initorm/orm": "^1.0"
2829
},
2930
"require-dev": {
30-
"phpunit/phpunit": "9.5"
31+
"phpunit/phpunit": "^10.4"
3132
}
3233
}

src/DB.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
namespace InitPHP\Database;
5+
6+
use InitORM\Database\Exceptions\DatabaseException;
7+
use InitORM\Database\Interfaces\DatabaseInterface;
8+
use InitORM\DBAL\Connection\Interfaces\ConnectionInterface;
9+
10+
/**
11+
* @mixin \InitORM\Database\Facade\DB
12+
*/
13+
class DB
14+
{
15+
16+
private static DatabaseInterface $db;
17+
18+
public function __call($name, $arguments)
19+
{
20+
return self::getDatabase()->{$name}(...$arguments);
21+
}
22+
23+
public static function __callStatic($name, $arguments)
24+
{
25+
return self::getDatabase()->{$name}(...$arguments);
26+
}
27+
28+
public static function createImmutable(array|ConnectionInterface $connection): void
29+
{
30+
self::$db = self::connect($connection);
31+
}
32+
33+
/**
34+
* @param array|ConnectionInterface $connection
35+
* @return DatabaseInterface
36+
*/
37+
public static function connect(array|ConnectionInterface $connection): DatabaseInterface
38+
{
39+
return new Database($connection);
40+
}
41+
42+
public static function getDatabase(): DatabaseInterface
43+
{
44+
if (!isset(self::$db)) {
45+
throw new DatabaseException('To create an immutable, first use the "createImmutable()" method.');
46+
}
47+
48+
return self::$db;
49+
}
50+
51+
52+
}

0 commit comments

Comments
 (0)
0