Skip to content

Commit dc37b85

Browse files
authored
Merge branch 'master' into master
2 parents e690998 + f21aeb5 commit dc37b85

File tree

15 files changed

+171
-110
lines changed

15 files changed

+171
-110
lines changed

.github/workflows/ezsql-linux.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
fail-fast: false
1818
matrix:
1919
operating-system: [ubuntu-18.04]
20-
php-versions: ['7.4', '8.0']
20+
php-versions: ['7.4', '8.0', '8.1']
2121

2222
steps:
2323
- name: Checkout

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ $db = Database::initialize('****', [$dsn_path_user, $password, $database, $other
6767
use ezsql\Config;
6868
use ezsql\Database\ez_****;
6969

70-
$setting = new Config('****', [$dsn_path_user, $password, $database, $other_settings]);
70+
$settings = new Config('****', [$dsn_path_user, $password, $database, $other_settings]);
7171

7272
$db = new ez_****($settings);
7373
```

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"issues": "https://github.com/ezSQL/ezSQL/issues"
4040
},
4141
"require": {
42-
"php": "^7.1 || ^8",
42+
"php": ">7.1",
4343
"psr/container": "^1.0"
4444
},
4545
"provide": {
@@ -55,7 +55,7 @@
5555
}
5656
},
5757
"require-dev": {
58-
"phpunit/phpunit": "^6 || ^7 || ^8"
58+
"phpunit/phpunit": "^6 | ^7 | ^8"
5959
},
6060
"autoload-dev": {
6161
"psr-4": {

lib/Database.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace ezsql;
66

7+
use ezsql\Db;
78
use ezsql\DInjector;
8-
use function ezsql\functions\setInstance;
99

1010
class Database
1111
{
@@ -15,7 +15,10 @@ class Database
1515
* @var float
1616
*/
1717
private static $_ts = null;
18-
private static $factory = null;
18+
19+
/**
20+
* @var ezQueryInterface[]
21+
*/
1922
private static $instances = [];
2023

2124
// @codeCoverageIgnoreStart
@@ -67,10 +70,10 @@ public function __wakeup()
6770
* @param string $tag Store the instance for later use
6871
* @return Database\ez_pdo|Database\ez_pgsql|Database\ez_sqlsrv|Database\ez_sqlite3|Database\ez_mysqli
6972
*/
70-
public static function initialize(?string $vendor = null, ?array $setting = null, ?string $tag = null)
73+
public static function initialize(?string $vendor = null, ?array $setting = null, ?string $tag = null): ezQueryInterface
7174
{
7275
if (isset(self::$instances[$vendor]) && empty($setting) && empty($tag))
73-
return setInstance(self::$instances[$vendor]) ? self::$instances[$vendor] : false;
76+
return self::$instances[$vendor];
7477

7578
if (empty($vendor) || empty($setting)) {
7679
throw new \Exception(\MISSING_CONFIGURATION);
@@ -79,7 +82,7 @@ public static function initialize(?string $vendor = null, ?array $setting = null
7982
$key = $vendor;
8083
$value = \VENDOR[$key];
8184

82-
if (empty($GLOBALS['ez' . $key]) || !empty($tag)) {
85+
if (!Db::has('ez' . $key) || !empty($tag)) {
8386
$di = new DInjector();
8487
$di->set($key, $value);
8588
$di->set('ezsql\ConfigInterface', 'ezsql\Config');
@@ -90,8 +93,9 @@ public static function initialize(?string $vendor = null, ?array $setting = null
9093
}
9194
}
9295

93-
setInstance($GLOBALS['ez' . $key]);
94-
return $GLOBALS['ez' . $key];
96+
$db = Db::get('ez' . $key);
97+
Db::set('global', $db);
98+
return $db;
9599
}
96100
}
97101

lib/Database/ez_mysqli.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
namespace ezsql\Database;
66

77
use Exception;
8+
use ezsql\Db;
89
use ezsql\ezsqlModel;
910
use ezsql\ConfigInterface;
1011
use ezsql\DatabaseInterface;
11-
use function ezsql\functions\setInstance;
1212

1313
class ez_mysqli extends ezsqlModel implements DatabaseInterface
1414
{
@@ -44,9 +44,9 @@ public function __construct(ConfigInterface $settings = null)
4444
parent::__construct();
4545
$this->database = $settings;
4646

47-
if (empty($GLOBALS['ez' . \MYSQLI]))
48-
$GLOBALS['ez' . \MYSQLI] = $this;
49-
setInstance($this);
47+
if (!Db::has('ez' . \MYSQLI))
48+
Db::set('ez' . \MYSQLI, $this);
49+
Db::set('global', $this);
5050
} // __construct
5151

5252
public function settings()

lib/Database/ez_pdo.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
namespace ezsql\Database;
66

77
use Exception;
8+
use ezsql\Db;
89
use ezsql\ezsqlModel;
910
use ezsql\ConfigInterface;
1011
use ezsql\DatabaseInterface;
11-
use function ezsql\functions\setInstance;
1212

1313
class ez_pdo extends ezsqlModel implements DatabaseInterface
1414
{
@@ -47,9 +47,9 @@ public function __construct(ConfigInterface $settings = null)
4747
// Turn on track errors
4848
ini_set('track_errors', '1');
4949

50-
if (empty($GLOBALS['ez' . \Pdo]))
51-
$GLOBALS['ez' . \Pdo] = $this;
52-
setInstance($this);
50+
if (!Db::has('ez' . \Pdo))
51+
Db::set('ez' . \Pdo, $this);
52+
Db::set('global', $this);
5353
} // __construct
5454

5555
public function settings()

lib/Database/ez_pgsql.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
namespace ezsql\Database;
66

77
use Exception;
8+
use ezsql\Db;
89
use ezsql\ezsqlModel;
910
use ezsql\ConfigInterface;
1011
use ezsql\DatabaseInterface;
11-
use function ezsql\functions\setInstance;
1212

1313
class ez_pgsql extends ezsqlModel implements DatabaseInterface
1414
{
@@ -44,9 +44,9 @@ public function __construct(ConfigInterface $settings = null)
4444
parent::__construct();
4545
$this->database = $settings;
4646

47-
if (empty($GLOBALS['ez' . \PGSQL]))
48-
$GLOBALS['ez' . \PGSQL] = $this;
49-
setInstance($this);
47+
if (!Db::has('ez' . \PGSQL))
48+
Db::set('ez' . \PGSQL, $this);
49+
Db::set('global', $this);
5050
} // __construct
5151

5252
public function settings()

lib/Database/ez_sqlite3.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
namespace ezsql\Database;
66

77
use Exception;
8+
use ezsql\Db;
89
use ezsql\ezsqlModel;
910
use ezsql\ConfigInterface;
1011
use ezsql\DatabaseInterface;
11-
use function ezsql\functions\setInstance;
1212

1313
class ez_sqlite3 extends ezsqlModel implements DatabaseInterface
1414
{
@@ -51,9 +51,9 @@ public function __construct(ConfigInterface $settings = null)
5151
// Turn on track errors
5252
ini_set('track_errors', '1');
5353

54-
if (!isset($GLOBALS['ez' . \SQLITE3]))
55-
$GLOBALS['ez' . \SQLITE3] = $this;
56-
setInstance($this);
54+
if (!Db::has('ez' . \SQLITE3))
55+
Db::set('ez' . \SQLITE3, $this);
56+
Db::set('global', $this);
5757
}
5858

5959
public function settings()

lib/Database/ez_sqlsrv.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
namespace ezsql\Database;
66

77
use Exception;
8+
use ezsql\Db;
89
use ezsql\ezsqlModel;
910
use ezsql\ConfigInterface;
1011
use ezsql\DatabaseInterface;
11-
use function ezsql\functions\setInstance;
1212

1313
class ez_sqlsrv extends ezsqlModel implements DatabaseInterface
1414
{
@@ -53,9 +53,9 @@ public function __construct(ConfigInterface $settings = null)
5353
parent::__construct();
5454
$this->database = $settings;
5555

56-
if (empty($GLOBALS['ez' . \SQLSRV]))
57-
$GLOBALS['ez' . \SQLSRV] = $this;
58-
setInstance($this);
56+
if (!Db::has('ez' . \SQLSRV))
57+
Db::set('ez' . \SQLSRV, $this);
58+
Db::set('global', $this);
5959
}
6060

6161
public function settings()

lib/Db.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ezsql;
6+
7+
use ezsql\ezQueryInterface;
8+
9+
/**
10+
* Used internally for needed **global** variables.
11+
*
12+
* @internal
13+
*/
14+
final class Db
15+
{
16+
/**
17+
* @var ezQueryInterface[]
18+
*/
19+
protected static $storage;
20+
21+
/**
22+
* @param string $key
23+
* @param mixed $value
24+
* @return void
25+
*/
26+
public static function set(string $key, $value): void
27+
{
28+
self::$storage[$key] = $value;
29+
}
30+
31+
/**
32+
* @param string $key
33+
* @return ezQueryInterface
34+
*/
35+
public static function get(string $key): ?ezQueryInterface
36+
{
37+
return self::$storage[$key] ?? null;
38+
}
39+
40+
/**
41+
* @param string $tag
42+
* @return boolean
43+
*/
44+
public static function has(string $tag): bool
45+
{
46+
return isset(self::$storage[$tag]);
47+
}
48+
49+
/**
50+
* @param string $tag
51+
* @return void
52+
*/
53+
public static function clear(string $tag): void
54+
{
55+
if (self::has($tag))
56+
unset(self::$storage[$tag]);
57+
}
58+
59+
/**
60+
* @return void
61+
*/
62+
public static function reset(): void
63+
{
64+
self::$storage = null;
65+
}
66+
}

0 commit comments

Comments
 (0)