forked from jhaagsma/eemphyre-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainer.class.php
129 lines (98 loc) · 3.53 KB
/
Container.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php namespace EmPHyre;
/**
* Container is the class creation object for the EmPHyre project
*
* PHP version 7
*
* ------
* This project uses the EmPHyre microframework,
* and is built off the EmPHyre example files
*
* Written by Julian Haagsma.
*
* @category Classes
* @package EmPHyre
* @author Julian Haagsma <jhaagsma@gmail.com>
* @license All files are licensed under the MIT License.
* @link https://demo3.phasesensors.com
* @since March 2016
*/
// prototype from http://code.tutsplus.com/tutorials/dependency-injection-huh--net-26903
class Container
{
protected function __construct()
{
// protected so it can't be instantiated
}//end __construct()
/**
* Array of database connections, mysqli
**/
private static $instances = [];
public static $params = [];
public static $userNamespace = null;
/*
public function __construct($params)
{
self::_params = $params;
}*/
private static function getDbName($database = null)
{
return ($database == null ? self::$params['db']['default']['db_name'] : $database);
}//end getDbName()
private static function getDbHost($database = null)
{
$database = self::getDbName($database);
return ( isset(self::$params['db'][$database]['db_host']) ? self::$params['db'][$database]['db_host'] : self::$params['db']['default']['db_host'] );
}//end getDbHost()
private static function getDbUser($database = null)
{
$database = self::getDbName($database);
return ( isset(self::$params['db'][$database]['db_user']) ? self::$params['db'][$database]['db_user'] : self::$params['db']['default']['db_user'] );
}//end getDbUser()
private static function getDbPass($database = null)
{
$database = self::getDbName($database);
return ( isset(self::$params['db'][$database]['db_pass']) ? self::$params['db'][$database]['db_pass'] : self::$params['db']['default']['db_pass'] );
}//end getDbPass()
public static function getDb($database = null)
{
$database = self::getDbName($database);
if (empty(self::$instances[$database])) {
self::$instances[$database] = new \EmPHyre\MysqlDb(
self::getDbHost($database),
$database,
self::getDbUser($database),
self::getDbPass($database),
false,
'counters',
false
);
// make 3 more things to handle more parameters
}
self::$instances[$database]->createIfNotExists();
return self::$instances[$database];
}//end getDb()
public static function newUserFromUUID($uuid = null, $clearcache = false)
{
$userid = User::getUserIdFromUUID($uuid);
return self::newUser($userid, $clearcache);
}//end newUserFromUUID()
public static function newUser($userid = 0, $clearcache = false)
{
if (static::$userNamespace) {
$class = "\\".static::$userNamespace."\\User";
$user = new $class($userid);
} else {
$user = new User($userid);
}
$user->setDb(self::getDb());
// use the default database
$user->initialize($clearcache);
return $user;
}//end newUser()
public static function newUserFromName($username = null, $clearcache = false)
{
$userid = User::getUserIdFromName($username);
return self::newUser($userid, $clearcache);
}//end newUserFromName()
}//end class