1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AsyncPHP\Icicle\Database; |
4
|
|
|
|
5
|
|
|
use Icicle\Coroutine; |
6
|
|
|
use Icicle\Promise\PromiseInterface; |
7
|
|
|
|
8
|
|
|
final class Manager |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Connector |
12
|
|
|
*/ |
13
|
|
|
private $connector; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Builder |
17
|
|
|
*/ |
18
|
|
|
private $builder; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $operations = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Connector $connector |
27
|
|
|
* @param Builder $builder |
28
|
|
|
*/ |
29
|
2 |
|
public function __construct(Connector $connector, Builder $builder) |
30
|
|
|
{ |
31
|
2 |
|
$this->connector = $connector; |
32
|
2 |
|
$this->builder = $builder; |
33
|
2 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $method |
37
|
|
|
* @param array $parameters |
38
|
|
|
* |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
2 |
|
public function __call($method, array $parameters = []) |
42
|
|
|
{ |
43
|
2 |
|
$operations = $this->operations; |
44
|
2 |
|
$operations[] = [$method, $parameters]; |
45
|
|
|
|
46
|
2 |
|
return $this->cloneWith("operations", $operations); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $key |
51
|
|
|
* @param mixed $value |
52
|
|
|
* |
53
|
|
|
* @return static |
54
|
|
|
*/ |
55
|
2 |
|
public function cloneWith($key, $value) |
56
|
|
|
{ |
57
|
2 |
|
$clone = clone $this; |
58
|
2 |
|
$clone->$key = $value; |
59
|
|
|
|
60
|
2 |
|
return $clone; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $table |
65
|
|
|
* |
66
|
|
|
* @return static |
67
|
|
|
*/ |
68
|
2 |
|
public function table($table) |
69
|
|
|
{ |
70
|
2 |
|
return $this->cloneWith("builder", $this->builder->table($table)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $columns |
75
|
|
|
* |
76
|
|
|
* @return static |
77
|
|
|
*/ |
78
|
2 |
|
public function select($columns = "*") |
79
|
|
|
{ |
80
|
2 |
|
return $this->cloneWith("builder", $this->builder->select($columns)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return PromiseInterface |
85
|
|
|
*/ |
86
|
2 |
|
public function first() |
87
|
|
|
{ |
88
|
|
|
return Coroutine\create(function () { |
89
|
2 |
|
$rows = (yield $this->limit(1)->get()); |
|
|
|
|
90
|
2 |
|
yield reset($rows); |
91
|
2 |
|
}); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return PromiseInterface |
96
|
|
|
*/ |
97
|
2 |
|
public function get() |
98
|
|
|
{ |
99
|
|
|
return Coroutine\create(function () { |
100
|
2 |
|
$builder = $this->applyOperationsTo($this->builder); |
101
|
|
|
|
102
|
2 |
|
list($statement, $values) = $builder->build(); |
103
|
2 |
|
yield $this->connector->query($statement, $values); |
104
|
2 |
|
}); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param Builder $builder |
109
|
|
|
* |
110
|
|
|
* @return Builder |
111
|
|
|
*/ |
112
|
2 |
|
private function applyOperationsTo($builder) |
113
|
|
|
{ |
114
|
2 |
|
foreach ($this->operations as $operation) { |
115
|
2 |
|
list($method, $parameters) = $operation; |
116
|
2 |
|
$builder = call_user_func_array([$builder, $method], $parameters); |
117
|
2 |
|
} |
118
|
|
|
|
119
|
2 |
|
return $builder; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $data |
124
|
|
|
* |
125
|
|
|
* @return PromiseInterface |
126
|
|
|
*/ |
127
|
2 |
View Code Duplication |
public function insert(array $data) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
return Coroutine\create(function () use ($data) { |
130
|
2 |
|
$builder = $this->builder->insert($data); |
131
|
2 |
|
$builder = $this->applyOperationsTo($builder); |
132
|
|
|
|
133
|
2 |
|
list($statement, $values) = $builder->build(); |
134
|
2 |
|
yield $this->connector->query($statement, $values); |
135
|
2 |
|
}); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param array $data |
140
|
|
|
* |
141
|
|
|
* @return PromiseInterface |
142
|
|
|
*/ |
143
|
|
View Code Duplication |
public function update(array $data) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
return Coroutine\create(function () use ($data) { |
146
|
|
|
$builder = $this->builder->update($data); |
147
|
|
|
$builder = $this->applyOperationsTo($builder); |
148
|
|
|
|
149
|
|
|
list($statement, $values) = $builder->build(); |
150
|
|
|
yield $this->connector->query($statement, $values); |
151
|
|
|
}); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return PromiseInterface |
156
|
|
|
*/ |
157
|
|
View Code Duplication |
public function delete() |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
return Coroutine\create(function () { |
160
|
|
|
$builder = $this->builder->delete(); |
161
|
|
|
$builder = $this->applyOperationsTo($builder); |
162
|
|
|
|
163
|
|
|
list($statement, $values) = $builder->build(); |
164
|
|
|
yield $this->connector->query($statement, $values); |
165
|
|
|
}); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: