|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Saltwater\RedBean\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Saltwater\Server as S; |
|
6
|
|
|
use Saltwater\Salt\Provider; |
|
7
|
|
|
|
|
8
|
|
|
class Db extends Provider |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var \RedBean_Instance |
|
12
|
|
|
*/ |
|
13
|
|
|
private static $r; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @return \RedBean_Instance |
|
|
|
|
|
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct($config) |
|
19
|
|
|
{ |
|
20
|
|
|
if (empty(self::$r)) { |
|
21
|
|
|
self::makeDB($config); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
return self::$r; |
|
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
protected static function makeDB($config) |
|
28
|
|
|
{ |
|
29
|
|
|
$cfg = $config->database; |
|
30
|
|
|
|
|
31
|
|
|
if (empty(self::$r)) { |
|
32
|
|
|
self::$r = new \RedBean_Instance(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (!isset($cfg->type)) { |
|
36
|
|
|
$cfg->type = 'mysql'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
self::setupDB($cfg); |
|
40
|
|
|
|
|
41
|
|
|
self::addDB($cfg); |
|
42
|
|
|
|
|
43
|
|
|
self::configureDB($cfg); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private static function setupDB($cfg) |
|
47
|
|
|
{ |
|
48
|
|
|
if (!empty(self::$r->toolboxes)) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
self::$r->setup( |
|
53
|
|
|
self::cfgToDSN($cfg), |
|
54
|
|
|
$cfg->user, |
|
55
|
|
|
$cfg->password |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
self::$r->setupPipeline(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private static function addDB($cfg) |
|
62
|
|
|
{ |
|
63
|
|
|
if (isset(self::$r->toolboxes[$cfg->name])) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
self::$r->addDatabase( |
|
68
|
|
|
$cfg->name, |
|
69
|
|
|
self::cfgToDSN($cfg), |
|
70
|
|
|
$cfg->user, |
|
71
|
|
|
$cfg->password |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private static function configureDB($cfg) |
|
76
|
|
|
{ |
|
77
|
|
|
self::$r->selectDatabase($cfg->name); |
|
78
|
|
|
|
|
79
|
|
|
if (!empty($cfg->prefix)) { |
|
80
|
|
|
self::$r->prefix($cfg->prefix); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
self::$r->redbean->beanhelper->setModelFormatter( |
|
84
|
|
|
'Saltwater\RedBean\Provider\Db::entity' |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
self::$r->useWriterCache(true); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Return an Entity class name from the EntityProvider |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $name |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function entity($name) |
|
98
|
|
|
{ |
|
99
|
|
|
return S::$n->entity->get($name); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private static function cfgToDSN($cfg) |
|
103
|
|
|
{ |
|
104
|
|
|
if (isset($cfg->dsn)) { |
|
105
|
|
|
return $cfg->dsn; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return self::makeDSN($cfg->type, $cfg->host, $cfg->name); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
private static function makeDSN($type, $host, $name) |
|
112
|
|
|
{ |
|
113
|
|
|
return $type . ':host=' . $host . ';' . 'dbname=' . $name; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.