1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle Framework |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @method static $this instance($check = false) |
12
|
|
|
*/ |
13
|
|
|
class Storage { |
14
|
|
|
use Singleton; |
15
|
|
|
/** |
16
|
|
|
* @var Storage\_Abstract[]|False_class[] |
17
|
|
|
*/ |
18
|
|
|
protected $connections = []; |
19
|
|
|
protected $successful_connections = []; |
20
|
|
|
protected $failed_connections = []; |
21
|
|
|
/** |
22
|
|
|
* Get list of connections of specified type |
23
|
|
|
* |
24
|
|
|
* @param bool|null|string $status `null` - returns array of connections with objects<br> |
25
|
|
|
* `true|1` - returns array of names of successful connections<br> |
26
|
|
|
* `false|0` - returns array of names of failed connections |
27
|
|
|
* |
28
|
|
|
* @return array|null |
29
|
|
|
*/ |
30
|
|
|
function get_connections_list ($status = null) { |
31
|
|
|
if ($status === null) { |
32
|
|
|
return $this->connections; |
33
|
|
|
} elseif ($status == 0) { |
34
|
|
|
return $this->failed_connections; |
35
|
|
|
} elseif ($status == 1) { |
36
|
|
|
return $this->successful_connections; |
37
|
|
|
} |
38
|
|
|
return null; |
39
|
|
|
} |
40
|
|
|
/** |
41
|
|
|
* Processing of getting storage instance |
42
|
|
|
* |
43
|
|
|
* @param int $connection |
44
|
|
|
* |
45
|
|
|
* @return Storage\_Abstract|False_class |
46
|
|
|
*/ |
47
|
|
|
function storage ($connection) { |
48
|
|
|
if (!is_int($connection) && $connection != '0') { |
49
|
|
|
return False_class::instance(); |
50
|
|
|
} |
51
|
|
|
return $this->connecting($connection); |
52
|
|
|
} |
53
|
|
|
/** |
54
|
|
|
* Processing of all storage requests |
55
|
|
|
* |
56
|
|
|
* @param int $connection |
57
|
|
|
* |
58
|
|
|
* @return Storage\_Abstract|False_class |
59
|
|
|
*/ |
60
|
|
|
protected function connecting ($connection) { |
61
|
|
|
/** |
62
|
|
|
* If connection found in list of failed connections - return instance of False_class |
63
|
|
|
*/ |
64
|
|
|
if (isset($this->failed_connections[$connection])) { |
65
|
|
|
return False_class::instance(); |
66
|
|
|
} |
67
|
|
|
/** |
68
|
|
|
* If connection already exists - return reference on the instance of Storage engine object |
69
|
|
|
*/ |
70
|
|
|
if (isset($this->connections[$connection])) { |
71
|
|
|
return $this->connections[$connection]; |
72
|
|
|
} |
73
|
|
|
$Config = Config::instance(); |
74
|
|
|
/** |
75
|
|
|
* If connection to the local storage |
76
|
|
|
*/ |
77
|
|
|
if ($connection == 0) { |
78
|
|
|
$Core = Core::instance(); |
79
|
|
|
$storage['connection'] = $Core->storage_type; |
80
|
|
|
$storage['url'] = $Core->storage_url; |
81
|
|
|
$storage['host'] = $Core->storage_host; |
82
|
|
|
$storage['user'] = $Core->storage_user; |
83
|
|
|
$storage['password'] = $Core->storage_password; |
84
|
|
|
} elseif (isset($Config->storage[$connection])) { |
85
|
|
|
$storage = &$Config->storage[$connection]; |
86
|
|
|
} else { |
87
|
|
|
return False_class::instance(); |
88
|
|
|
} |
89
|
|
|
/** |
90
|
|
|
* Create new Storage connection |
91
|
|
|
*/ |
92
|
|
|
$engine_class = "\\cs\\Storage\\$storage[connection]"; |
93
|
|
|
$this->connections[$connection] = new $engine_class($storage['url'], $storage['host'], $storage['user'], $storage['password']); |
94
|
|
|
/** |
95
|
|
|
* If successfully - add connection to the list of success connections and return instance of DB engine object |
96
|
|
|
*/ |
97
|
|
|
if (is_object($this->connections[$connection]) && $this->connections[$connection]->connected()) { |
98
|
|
|
$this->successful_connections[] = "$connection/$storage[host]/$storage[connection]"; |
99
|
|
|
unset($storage); |
100
|
|
|
$this->$connection = $this->connections[$connection]; |
101
|
|
|
return $this->connections[$connection]; |
102
|
|
|
/** |
103
|
|
|
* If failed - add connection to the list of failed connections and display connection error |
104
|
|
|
*/ |
105
|
|
|
} else { |
106
|
|
|
unset($this->$connection); |
107
|
|
|
$this->failed_connections[$connection] = "$connection/$storage[host]/$storage[connection]"; |
108
|
|
|
unset($storage); |
109
|
|
|
trigger_error('Error connecting to storage '.$this->failed_connections[$connection], E_USER_WARNING); |
110
|
|
|
$return = False_class::instance(); |
111
|
|
|
$return->error = 'Connection failed'; |
112
|
|
|
return $return; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|