Skip to content

Commit 5eb0468

Browse files
committed
removed global keyword usage, test under PHP 8.1
- use separate class for internal global variables - removed `setInstance` - corrections for multi tag database instances - start testing under PHP 8.1 on Linux GitHub Action
1 parent 4bc6a25 commit 5eb0468

File tree

12 files changed

+130
-86
lines changed

12 files changed

+130
-86
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

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+
}

lib/ezFunctions.php

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,28 @@
99
use ezsql\Database;
1010
use ezsql\ezQueryInterface;
1111
use ezsql\DatabaseInterface;
12+
use ezsql\Db;
1213
use ezsql\ezsqlModelInterface;
1314

1415
if (!\function_exists('ezFunctions')) {
16+
/**
17+
* Returns the global database class, last created instance.
18+
*
19+
* @return ezQueryInterface|null
20+
*/
21+
function getInstance(): ?ezQueryInterface
22+
{
23+
return Db::get('global');
24+
}
25+
26+
/**
27+
* Clear/unset the global database class instance.
28+
*/
29+
function clearInstance(): void
30+
{
31+
Db::clear('global');
32+
}
33+
1534
/**
1635
* Initialize and connect a vendor's database.
1736
*
@@ -138,7 +157,7 @@ function sqliteInstance(array $databaseSetting = null, string $instanceTag = nul
138157
*
139158
* @return string|null `mysqli`|`pgsql`|`sqlite3`|`sqlsrv`
140159
*/
141-
function get_vendor(DatabaseInterface $instance = null)
160+
function get_vendor(DatabaseInterface $instance = null): ?string
142161
{
143162
return ezSchema::vendor($instance);
144163
}
@@ -148,7 +167,7 @@ function get_vendor(DatabaseInterface $instance = null)
148167
*
149168
* @return string
150169
*/
151-
function to_string($arrays, $separation = ',')
170+
function to_string($arrays, $separation = ','): string
152171
{
153172
return ezQuery::to_string($arrays, $separation);
154173
}
@@ -479,38 +498,6 @@ function notBetween($x, $y, $y2, ...$args)
479498
return $expression;
480499
}
481500

482-
/**
483-
* Sets the global class instance for functions to call class methods directly.
484-
*
485-
* @param ezQueryInterface|null $ezSQL
486-
*
487-
* @return boolean - `true`, or `false` for error
488-
*/
489-
function setInstance(ezQueryInterface $ezSQL = null)
490-
{
491-
global $ezInstance;
492-
$status = false;
493-
494-
if ($ezSQL instanceof ezQueryInterface) {
495-
$ezInstance = $ezSQL;
496-
$status = true;
497-
}
498-
499-
return $status;
500-
}
501-
502-
/**
503-
* Returns the global database class, last created instance or the one set with `setInstance()`.
504-
*
505-
* @return ezQueryInterface|null
506-
*/
507-
function getInstance()
508-
{
509-
global $ezInstance;
510-
511-
return ($ezInstance instanceof ezQueryInterface) ? $ezInstance : null;
512-
}
513-
514501
/**
515502
* Get multiple row result set from the database (previously cached results).
516503
* Returns a multi dimensional array.
@@ -547,17 +534,6 @@ function get_results($output = \OBJECT, $instance = null)
547534
: false;
548535
}
549536

550-
/**
551-
* Clear/unset the global database class instance.
552-
*/
553-
function clearInstance()
554-
{
555-
global $ezInstance;
556-
$GLOBALS['ezInstance'] = null;
557-
$ezInstance = null;
558-
unset($GLOBALS['ezInstance']);
559-
}
560-
561537
/**
562538
* Clean input string of XSS, html, javascript, etc...
563539
* @param string $string

0 commit comments

Comments
 (0)