1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// Copyright (c) Dmitry Kosenkov, All rights reserved. |
4
|
|
|
|
5
|
|
|
namespace HSAL\Driver; |
6
|
|
|
|
7
|
|
|
use \HSAL\HSAL; |
8
|
|
|
use \HSAL\Driver; |
9
|
|
|
use \HSAL\DriverInterface; |
|
|
|
|
10
|
|
|
|
11
|
|
|
class Handlersocketi extends Driver implements DriverInterface |
12
|
|
|
{ |
13
|
|
|
private $hsr; |
14
|
|
|
private $hsr_connected = FALSE; |
15
|
|
|
|
16
|
|
|
private $hsw; |
17
|
|
|
private $hsw_connected = FALSE; |
18
|
|
|
|
19
|
|
|
const PORT_READ = 9998; |
20
|
|
|
const PORT_WRITE = 9999; |
21
|
|
|
|
22
|
|
|
protected function getReadSocket() |
23
|
|
|
{ |
24
|
|
|
if (!$this->hsr_connected) |
25
|
|
|
{ |
26
|
|
|
$this->hsr = new \HandlerSocketi($this->host, self::PORT_READ); |
27
|
|
|
|
28
|
|
|
$this->hsr_connected = TRUE; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $this->hsr; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function getWriteSocket() |
35
|
|
|
{ |
36
|
|
|
if (!$this->hsw_connected) |
37
|
|
|
{ |
38
|
|
|
$this->hsw = new \HandlerSocketi($this->host, self::PORT_WRITE); |
39
|
|
|
|
40
|
|
|
$this->hsw_connected = TRUE; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this->hsw; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
public function fetchArray($table, Array $fields, $index, Array $condition, $operator) |
48
|
|
|
{ |
49
|
|
|
$hs = $this->getReadSocket(); |
50
|
|
|
|
51
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
52
|
|
|
|
53
|
|
|
if ($index == HSAL::INDEX_PRIMARY) $index = 'PRIMARY'; |
54
|
|
|
|
55
|
|
|
$index = $hs->openIndex($database, $table, $fields, ['index' => $index]); |
56
|
|
|
|
57
|
|
|
$result = $index->find([$operator => $condition]); |
58
|
|
|
|
59
|
|
|
if (!empty($result) && is_array($result)) |
60
|
|
|
return $result[0]; |
61
|
|
|
else |
62
|
|
|
return FALSE; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function fetchAll($table, Array $fields, $index, Array $condition, $operator, $limit, $offset) |
66
|
|
|
{ |
67
|
|
|
$hs = $this->getReadSocket(); |
68
|
|
|
|
69
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
70
|
|
|
|
71
|
|
|
if ($index == HSAL::INDEX_PRIMARY) $index = 'PRIMARY'; |
72
|
|
|
|
73
|
|
|
$index = $hs->openIndex($database, $table, $fields, ['index' => $index]); |
74
|
|
|
|
75
|
|
|
$result = $index->find([$operator => $condition], ['limit' => $limit, 'offset' => $offset]); |
76
|
|
|
|
77
|
|
View Code Duplication |
if (!empty($result) && is_array($result)) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
foreach ($result as $key => $arr) |
80
|
|
|
{ |
81
|
|
|
$result[$key] = array_combine($fields, $arr); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $result; |
85
|
|
|
} |
86
|
|
|
else |
87
|
|
|
return FALSE; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
public function delete($table, $index, Array $condition, $operator) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$hs = $this->getWriteSocket(); |
93
|
|
|
|
94
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
95
|
|
|
|
96
|
|
|
if ($index == HSAL::INDEX_PRIMARY) $index = 'PRIMARY'; |
97
|
|
|
|
98
|
|
|
$index = $hs->openIndex($database, $table, [], ['index' => $index]); |
99
|
|
|
|
100
|
|
|
$result = $index->remove([$operator => $condition]); |
101
|
|
|
|
102
|
|
|
return (bool)$result; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function insert($table, Array $values) |
106
|
|
|
{ |
107
|
|
|
$hs = $this->getWriteSocket(); |
108
|
|
|
|
109
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
110
|
|
|
|
111
|
|
|
$index = $hs->openIndex($database, $table, array_keys($values)); |
112
|
|
|
|
113
|
|
|
$result = $index->insert(array_values($values)); |
114
|
|
|
|
115
|
|
|
return (bool)$result; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function update($table, Array $values, $index, Array $condition, $operator) |
119
|
|
|
{ |
120
|
|
|
$hs = $this->getWriteSocket(); |
121
|
|
|
|
122
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
123
|
|
|
|
124
|
|
|
if ($index == HSAL::INDEX_PRIMARY) $index = 'PRIMARY'; |
|
|
|
|
125
|
|
|
|
126
|
|
|
$fields = array_keys($values); |
|
|
|
|
127
|
|
|
|
128
|
|
|
$index = $hs->openIndex($database, $table, array_keys($values)); |
129
|
|
|
|
130
|
|
|
$result = $index->update([$operator => $condition], array_values($values)); |
131
|
|
|
|
132
|
|
|
return (bool)$result; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
View Code Duplication |
public function increment($table, $field, $index, Array $condition, $operator, $increment) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$hs = $this->getWriteSocket(); |
138
|
|
|
|
139
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
140
|
|
|
|
141
|
|
|
if ($index == HSAL::INDEX_PRIMARY) $index = 'PRIMARY'; |
|
|
|
|
142
|
|
|
|
143
|
|
|
$index = $hs->openIndex($database, $table, [$field]); |
144
|
|
|
|
145
|
|
|
$result = $index->update([$operator => $condition], ['+' => $increment]); |
146
|
|
|
|
147
|
|
|
return (bool)$result; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
View Code Duplication |
public function decrement($table, $field, $index, Array $condition, $operator, $decrement) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
|
153
|
|
|
$hs = $this->getWriteSocket(); |
154
|
|
|
|
155
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
156
|
|
|
|
157
|
|
|
if ($index == HSAL::INDEX_PRIMARY) $index = 'PRIMARY'; |
|
|
|
|
158
|
|
|
|
159
|
|
|
$index = $hs->openIndex($database, $table, [$field]); |
160
|
|
|
|
161
|
|
|
$result = $index->update([$operator => $condition], ['-' => $decrement]); |
162
|
|
|
|
163
|
|
|
return (bool)$result; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
} |
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: