|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
4
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
5
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
use midgard\portable\storage\connection; |
|
9
|
|
|
use midgard\portable\api\error\exception; |
|
|
|
|
|
|
10
|
|
|
use midgard\portable\api\config; |
|
11
|
|
|
|
|
12
|
|
|
class midgard_connection |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* |
|
16
|
|
|
* @var config |
|
17
|
|
|
*/ |
|
18
|
|
|
public $config; |
|
19
|
|
|
|
|
20
|
|
|
private static $instance; |
|
21
|
|
|
|
|
22
|
|
|
private $error_code = exception::OK; |
|
23
|
|
|
|
|
24
|
|
|
private $error_string; |
|
25
|
|
|
|
|
26
|
|
|
private $loglevel; |
|
27
|
|
|
|
|
28
|
|
|
private $available_loglevels = ['error', 'warn', 'warning', 'info', 'message', 'debug']; |
|
29
|
|
|
|
|
30
|
|
|
private $replication_enabled = true; |
|
31
|
|
|
|
|
32
|
135 |
|
public static function get_instance() |
|
33
|
|
|
{ |
|
34
|
135 |
|
if (self::$instance === null) { |
|
35
|
|
|
self::$instance = new static; |
|
36
|
|
|
} |
|
37
|
135 |
|
return self::$instance; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function copy() |
|
41
|
|
|
{ |
|
42
|
|
|
return clone self::$instance; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function open($name) |
|
46
|
|
|
{ |
|
47
|
1 |
|
if ($this->config !== null) { |
|
48
|
1 |
|
$this->error_code = exception::INTERNAL; |
|
49
|
1 |
|
$this->error_string = 'MidgardConfig already associated with MidgardConnection'; |
|
50
|
1 |
|
return false; |
|
51
|
|
|
} |
|
52
|
1 |
|
$config = new config; |
|
53
|
1 |
|
if (!$config->read_file($name, false)) { |
|
54
|
1 |
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
$this->config = $config; |
|
57
|
|
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function open_config(midgard_config $config) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->config = $config; |
|
63
|
|
|
$this->set_loglevel($config->loglevel); |
|
64
|
12 |
|
} |
|
65
|
|
|
|
|
66
|
12 |
|
public function is_connected() |
|
67
|
12 |
|
{ |
|
68
|
12 |
|
return is_object($this->config); |
|
69
|
|
|
} |
|
70
|
1 |
|
|
|
71
|
|
|
public function get_error() |
|
72
|
1 |
|
{ |
|
73
|
|
|
return $this->error_code; |
|
74
|
|
|
} |
|
75
|
59 |
|
|
|
76
|
|
|
public function set_error($errorcode) |
|
77
|
59 |
|
{ |
|
78
|
|
|
$this->error_code = $errorcode; |
|
79
|
|
|
$this->error_string = null; |
|
80
|
124 |
|
} |
|
81
|
|
|
|
|
82
|
124 |
|
public function get_error_string() |
|
83
|
124 |
|
{ |
|
84
|
124 |
|
if ($this->error_string === null) { |
|
85
|
|
|
return exception::get_error_string($this->error_code); |
|
86
|
57 |
|
} |
|
87
|
|
|
return $this->error_string; |
|
88
|
57 |
|
} |
|
89
|
50 |
|
|
|
90
|
|
|
public function set_error_string($string) |
|
91
|
23 |
|
{ |
|
92
|
|
|
$this->error_string = $string; |
|
93
|
|
|
} |
|
94
|
43 |
|
|
|
95
|
|
|
public function get_user() |
|
96
|
43 |
|
{ |
|
97
|
43 |
|
if (!$this->is_connected()) { |
|
98
|
|
|
return null; |
|
99
|
1 |
|
} |
|
100
|
|
|
return connection::get_user(); |
|
101
|
1 |
|
} |
|
102
|
1 |
|
|
|
103
|
|
|
public function set_loglevel($level, $callback = '???') |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
if (!in_array($level, $this->available_loglevels)) { |
|
106
|
|
|
return false; |
|
107
|
13 |
|
} |
|
108
|
|
|
$this->loglevel = $level; |
|
109
|
13 |
|
return true; |
|
110
|
1 |
|
} |
|
111
|
|
|
|
|
112
|
13 |
|
public function get_loglevel() |
|
113
|
13 |
|
{ |
|
114
|
|
|
return $this->loglevel; |
|
115
|
|
|
} |
|
116
|
13 |
|
|
|
117
|
|
|
public function enable_replication($toggle) |
|
118
|
13 |
|
{ |
|
119
|
|
|
$this->replication_enabled = (bool) $toggle; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function is_enabled_replication() |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->replication_enabled; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: