|
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 |
|
15
|
|
|
Singleton; |
|
16
|
|
|
const CONNECTIONS_ACTIVE = 0; |
|
17
|
|
|
const CONNECTIONS_SUCCESSFUL = 1; |
|
18
|
|
|
const CONNECTIONS_FAILED = 2; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array[] |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $connections = [ |
|
23
|
|
|
self::CONNECTIONS_ACTIVE => [], |
|
24
|
|
|
self::CONNECTIONS_SUCCESSFUL => [], |
|
25
|
|
|
self::CONNECTIONS_FAILED => [] |
|
26
|
|
|
]; |
|
27
|
|
|
/** |
|
28
|
|
|
* Get list of connections of specified type |
|
29
|
|
|
* |
|
30
|
|
|
* @param int $type One of constants `self::CONNECTIONS_*` |
|
31
|
|
|
* |
|
32
|
|
|
* @return array For `self::CONNECTIONS_ACTIVE` array of successful connections with corresponding objects as values of array, otherwise array where keys |
|
33
|
|
|
* are storage ids and values are strings with information about storage |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public function get_connections_list ($type) { |
|
36
|
2 |
|
return $this->connections[$type]; |
|
37
|
|
|
} |
|
38
|
|
|
/** |
|
39
|
|
|
* Processing of getting storage instance |
|
40
|
|
|
* |
|
41
|
|
|
* @param int $storage_id |
|
42
|
|
|
* |
|
43
|
|
|
* @return Storage\_Abstract|False_class |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function storage ($storage_id) { |
|
46
|
|
|
/** |
|
47
|
|
|
* If connection found in list of failed connections - return instance of False_class |
|
48
|
|
|
*/ |
|
49
|
2 |
|
if (isset($this->connections[self::CONNECTIONS_FAILED][$storage_id])) { |
|
50
|
2 |
|
return False_class::instance(); |
|
51
|
|
|
} |
|
52
|
|
|
/** |
|
53
|
|
|
* If connection already exists - return reference on the instance of Storage engine object |
|
54
|
|
|
*/ |
|
55
|
2 |
|
if (isset($this->connections[self::CONNECTIONS_ACTIVE][$storage_id])) { |
|
56
|
2 |
|
return $this->connections[self::CONNECTIONS_ACTIVE][$storage_id]; |
|
57
|
|
|
} |
|
58
|
|
|
/** |
|
59
|
|
|
* If connection to the local storage |
|
60
|
|
|
*/ |
|
61
|
2 |
|
if ($storage_id == 0) { |
|
62
|
2 |
|
$Core = Core::instance(); |
|
63
|
|
|
$storage = [ |
|
64
|
2 |
|
'connection' => $Core->storage_type, |
|
65
|
2 |
|
'url' => $Core->storage_url, |
|
66
|
2 |
|
'host' => $Core->storage_host, |
|
67
|
2 |
|
'user' => $Core->storage_user, |
|
68
|
2 |
|
'password' => $Core->storage_password |
|
69
|
|
|
]; |
|
70
|
|
|
} else { |
|
71
|
2 |
|
$storage = Config::instance()->storage[$storage_id]; |
|
72
|
|
|
} |
|
73
|
|
|
/** |
|
74
|
|
|
* Establish new connection |
|
75
|
|
|
* |
|
76
|
|
|
* @var Storage\_Abstract $connection |
|
77
|
|
|
*/ |
|
78
|
2 |
|
$engine_class = "\\cs\\Storage\\$storage[connection]"; |
|
79
|
2 |
|
$connection = new $engine_class( |
|
80
|
2 |
|
$storage['url'], |
|
81
|
2 |
|
$storage['host'], |
|
82
|
2 |
|
$storage['user'], |
|
83
|
2 |
|
$storage['password'] |
|
84
|
|
|
); |
|
85
|
2 |
|
$connection_name = "$storage_id/$storage[host]/$storage[connection]"; |
|
86
|
|
|
/** |
|
87
|
|
|
* If successfully - add connection to the list of success connections and return instance of Storage engine object |
|
88
|
|
|
*/ |
|
89
|
2 |
|
if (is_object($connection) && $connection->connected()) { |
|
90
|
2 |
|
$this->connections[self::CONNECTIONS_ACTIVE][$storage_id] = $connection; |
|
91
|
2 |
|
$this->connections[self::CONNECTIONS_SUCCESSFUL][] = $connection_name; |
|
92
|
2 |
|
return $connection; |
|
93
|
|
|
} |
|
94
|
|
|
/** |
|
95
|
|
|
* If failed - add connection to the list of failed connections and log error |
|
96
|
|
|
*/ |
|
97
|
2 |
|
$this->connections[self::CONNECTIONS_FAILED][$storage_id] = $connection_name; |
|
98
|
2 |
|
trigger_error("Error connecting to storage $connection_name", E_USER_WARNING); |
|
99
|
2 |
|
return False_class::instance(); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|