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
|
|
|
use midgard\portable\api\user; |
12
|
|
|
|
13
|
|
|
class midgard_connection |
14
|
|
|
{ |
15
|
|
|
public ?config $config = null; |
16
|
|
|
|
17
|
|
|
private static ?self $instance = null; |
18
|
|
|
|
19
|
|
|
private int $error_code = exception::OK; |
20
|
|
|
|
21
|
|
|
private ?string $error_string = null; |
22
|
|
|
|
23
|
|
|
private $loglevel; |
24
|
|
|
|
25
|
|
|
private array $available_loglevels = ['error', 'warn', 'warning', 'info', 'message', 'debug']; |
26
|
|
|
|
27
|
|
|
private bool $replication_enabled = true; |
28
|
|
|
|
29
|
137 |
|
public static function get_instance() : self |
30
|
|
|
{ |
31
|
137 |
|
return self::$instance ??= new static; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function copy() : self |
35
|
|
|
{ |
36
|
|
|
return clone self::$instance; |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
public function open(string $name) : bool |
40
|
|
|
{ |
41
|
1 |
|
if ($this->config !== null) { |
42
|
1 |
|
$this->error_code = exception::INTERNAL; |
43
|
1 |
|
$this->error_string = 'MidgardConfig already associated with MidgardConnection'; |
44
|
1 |
|
return false; |
45
|
|
|
} |
46
|
1 |
|
$config = new config; |
47
|
1 |
|
if (!$config->read_file($name, false)) { |
48
|
1 |
|
return false; |
49
|
|
|
} |
50
|
|
|
$this->config = $config; |
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
12 |
|
public function open_config(config $config) |
55
|
|
|
{ |
56
|
12 |
|
$this->config = $config; |
57
|
12 |
|
$this->set_loglevel($config->loglevel); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function is_connected() : bool |
61
|
|
|
{ |
62
|
1 |
|
return is_object($this->config); |
63
|
|
|
} |
64
|
|
|
|
65
|
59 |
|
public function get_error() : int |
66
|
|
|
{ |
67
|
59 |
|
return $this->error_code; |
68
|
|
|
} |
69
|
|
|
|
70
|
127 |
|
public function set_error(int $errorcode) |
71
|
|
|
{ |
72
|
127 |
|
$this->error_code = $errorcode; |
73
|
127 |
|
$this->error_string = null; |
74
|
|
|
} |
75
|
|
|
|
76
|
57 |
|
public function get_error_string() : string |
77
|
|
|
{ |
78
|
57 |
|
return $this->error_string ?? exception::get_error_string($this->error_code); |
79
|
|
|
} |
80
|
|
|
|
81
|
125 |
|
public function set_error_string(string $string) |
82
|
|
|
{ |
83
|
125 |
|
$this->error_string = $string; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public function get_user() : ?user |
87
|
|
|
{ |
88
|
1 |
|
if (!$this->is_connected()) { |
89
|
1 |
|
return null; |
90
|
|
|
} |
91
|
|
|
return connection::get_user(); |
92
|
|
|
} |
93
|
|
|
|
94
|
13 |
|
public function set_loglevel($level, $callback = '???') : bool |
|
|
|
|
95
|
|
|
{ |
96
|
13 |
|
if (!in_array($level, $this->available_loglevels)) { |
97
|
1 |
|
return false; |
98
|
|
|
} |
99
|
13 |
|
$this->loglevel = $level; |
100
|
13 |
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
10 |
|
public function get_loglevel() |
104
|
|
|
{ |
105
|
10 |
|
return $this->loglevel; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function enable_replication(bool $toggle) |
109
|
|
|
{ |
110
|
|
|
$this->replication_enabled = $toggle; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function is_enabled_replication() : bool |
114
|
|
|
{ |
115
|
|
|
return $this->replication_enabled; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: