|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* MongoDB service provider for the Silex micro-framework. |
|
4
|
|
|
* |
|
5
|
|
|
* @see http://php.net/manual/set.mongodb.php |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace LiquidBox\Silex\Provider; |
|
9
|
|
|
|
|
10
|
|
|
use MongoDB; |
|
11
|
|
|
use Pimple\Container; |
|
12
|
|
|
use Pimple\ServiceProviderInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* MongoDb Provider. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Jonathan-Paul Marois <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class MongoDbServiceProvider implements ServiceProviderInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array Buffer. |
|
23
|
|
|
*/ |
|
24
|
|
|
private $args = array(); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private $defaultArgs = array(); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var int|string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $defaultConnection = 0; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private $id = 'mongodb'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $instances = 'mongodb.clients'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var bool |
|
48
|
|
|
*/ |
|
49
|
|
|
private $isLoaded = false; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
private $parameters = array(); |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $id |
|
58
|
|
|
* @param string $instances |
|
59
|
|
|
*/ |
|
60
|
10 |
|
public function __construct($id = null, $instances = null) |
|
61
|
|
|
{ |
|
62
|
10 |
|
if (strlen($id)) { |
|
63
|
1 |
|
$this->id = $id; |
|
64
|
1 |
|
} |
|
65
|
10 |
|
if (strlen($instances)) { |
|
66
|
1 |
|
$this->instances = $instances; |
|
67
|
1 |
|
} |
|
68
|
10 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
10 |
|
private function buildConnectionString(array $connection) |
|
74
|
|
|
{ |
|
75
|
10 |
|
$uri = $this->getArgConnectionAuthority($connection) . '/'; |
|
76
|
|
|
|
|
77
|
10 |
|
if (!empty($connection['db'])) { |
|
78
|
3 |
|
$uri .= $connection['db']; |
|
79
|
3 |
|
} |
|
80
|
10 |
|
if (!empty($connection['options'])) { |
|
81
|
2 |
|
$uri .= '?' . http_build_query($connection['options']); |
|
82
|
2 |
|
} |
|
83
|
|
|
|
|
84
|
10 |
|
return rtrim($uri, '/'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string|array $connection |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
10 |
|
private function buildUri($connection) |
|
93
|
|
|
{ |
|
94
|
10 |
|
if (!is_array($connection)) { |
|
95
|
5 |
|
$connection = array('host' => $this->sanitizeUri($connection)); |
|
96
|
5 |
|
} |
|
97
|
|
|
|
|
98
|
10 |
|
return 'mongodb://' . $this->buildConnectionString($connection); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param \Pimple\Container $app |
|
103
|
|
|
* @param string $name |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
10 |
|
private function getArg(Container $app, $name) |
|
108
|
|
|
{ |
|
109
|
10 |
|
return isset($this->args[$name]) ? $this->args[$name] : $this->getDefaultArg($app, $name); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
10 |
|
private function getArgConnectionAuthority(array $connection) |
|
113
|
|
|
{ |
|
114
|
10 |
|
if (empty($connection['user'])) { |
|
115
|
7 |
|
return $this->getArgConnectionHost($connection); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
3 |
|
return $connection['user'] . ':' . rawurlencode($connection['pwd']) . '@' . |
|
119
|
3 |
|
$this->getArgConnectionHost($connection); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
10 |
|
private function getArgConnectionHost(array $connection = array()) |
|
126
|
|
|
{ |
|
127
|
10 |
|
if (!empty($connection['hosts'])) { |
|
128
|
1 |
|
return $this->getArgConnectionHosts($connection); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
10 |
|
if (empty($connection['host'])) { |
|
132
|
7 |
|
$connection['host'] = '127.0.0.1'; |
|
133
|
7 |
|
} |
|
134
|
|
|
|
|
135
|
10 |
|
return empty($connection['port']) ? $connection['host'] : $connection['host'] . ':' . $connection['port']; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
private function getArgConnectionHosts(array $connection) |
|
139
|
|
|
{ |
|
140
|
|
|
return implode(',', array_map(function ($hosts) { |
|
141
|
1 |
|
return $this->getArgConnectionHost(is_array($hosts) ? $hosts : array('host' => $hosts)); |
|
142
|
1 |
|
}, $connection['hosts'])); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
10 |
|
private function getArgUri() |
|
149
|
|
|
{ |
|
150
|
10 |
|
if (!empty($this->args['uri'])) { |
|
151
|
2 |
|
return $this->args['uri']; |
|
152
|
|
|
} |
|
153
|
8 |
|
if (!empty($this->args['connection'])) { |
|
154
|
6 |
|
return is_array($this->args['connection']) ? $this->args['connection'] : 'mongodb://' . $this->args['connection']; |
|
|
|
|
|
|
155
|
|
|
} |
|
156
|
2 |
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param \Pimple\Container $app |
|
160
|
|
|
* @param string $name |
|
161
|
|
|
* |
|
162
|
|
|
* @return string|array |
|
163
|
|
|
*/ |
|
164
|
9 |
|
private function getDefaultArg(Container $app, $name) |
|
165
|
|
|
{ |
|
166
|
9 |
|
if (!isset($this->defaultArgs[$name])) { |
|
167
|
9 |
|
$this->defaultArgs[$name] = empty($app['mongodb.' . $name]) ? |
|
168
|
9 |
|
array('uri_options' => array(), 'driver_options' => array())[$name] : |
|
169
|
1 |
|
$app['mongodb.' . $name]; |
|
170
|
9 |
|
} |
|
171
|
|
|
|
|
172
|
9 |
|
return $this->defaultArgs[$name]; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
10 |
|
private function loadParameters(Container $app) |
|
176
|
|
|
{ |
|
177
|
10 |
|
if ($this->isLoaded) { |
|
178
|
10 |
|
return; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
10 |
|
$this->isLoaded = true; |
|
182
|
|
|
|
|
183
|
10 |
|
if (empty($app['mongodb.uri']) || !is_array($app['mongodb.uri'])) { |
|
184
|
9 |
|
$this->loadSingletonParameters($app); |
|
185
|
9 |
|
} else { |
|
186
|
1 |
|
$this->parameters = $app['mongodb.uri']; |
|
187
|
1 |
|
$this->defaultConnection = array_keys($this->parameters)[0]; |
|
188
|
|
|
} |
|
189
|
10 |
|
} |
|
190
|
|
|
|
|
191
|
9 |
|
private function loadSingletonParameters(Container $app) |
|
192
|
|
|
{ |
|
193
|
9 |
|
$this->parameters[0] = array(); |
|
194
|
|
|
|
|
195
|
9 |
|
if (!empty($app['mongodb.uri'])) { |
|
196
|
2 |
|
$this->parameters[0]['uri'] = $app['mongodb.uri']; |
|
197
|
9 |
|
} elseif (!empty($app['mongodb.connection'])) { |
|
198
|
5 |
|
$this->parameters[0]['connection'] = $app['mongodb.connection']; |
|
199
|
5 |
|
} |
|
200
|
|
|
|
|
201
|
9 |
|
if (!empty($app['mongodb.uri_options'])) { |
|
202
|
1 |
|
$this->parameters[0]['uri_options'] = $app['mongodb.uri_options']; |
|
203
|
1 |
|
} |
|
204
|
9 |
|
if (!empty($app['mongodb.driver_options'])) { |
|
205
|
1 |
|
$this->parameters[0]['driver_options'] = $app['mongodb.driver_options']; |
|
206
|
1 |
|
} |
|
207
|
9 |
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param string $uri |
|
211
|
|
|
* |
|
212
|
|
|
* @return string |
|
213
|
|
|
*/ |
|
214
|
5 |
|
private function sanitizeUri($uri) |
|
215
|
|
|
{ |
|
216
|
5 |
|
return ('mongodb://' == substr($uri, 0, 10)) ? substr($uri, 10) : $uri; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
10 |
|
public function register(Container $app) |
|
220
|
|
|
{ |
|
221
|
|
|
$app[$this->id] = function () use ($app) { |
|
222
|
10 |
|
$this->loadParameters($app); |
|
223
|
|
|
|
|
224
|
10 |
|
return $app[$this->instances][$this->defaultConnection]; |
|
225
|
|
|
}; |
|
226
|
|
|
$app[$this->instances] = function () use ($app) { |
|
227
|
10 |
|
$this->loadParameters($app); |
|
228
|
|
|
|
|
229
|
10 |
|
$instances = new Container(); |
|
230
|
10 |
|
foreach ($this->parameters as $client => $this->args) { |
|
231
|
|
|
$instances[$client] = function () use ($app) { |
|
232
|
10 |
|
return $app['mongodb.client']( |
|
233
|
10 |
|
$this->getArgUri(), |
|
234
|
10 |
|
$this->getArg($app, 'uri_options'), |
|
235
|
10 |
|
$this->getArg($app, 'driver_options') |
|
236
|
10 |
|
); |
|
237
|
|
|
}; |
|
238
|
10 |
|
} |
|
239
|
|
|
|
|
240
|
10 |
|
return $instances; |
|
241
|
|
|
}; |
|
242
|
|
|
|
|
243
|
10 |
|
$app['mongodb.client'] = $app->protect( |
|
244
|
10 |
|
function ($uri = '', array $uriOptions = array(), array $driverOptions = array()) { |
|
245
|
10 |
|
return new MongoDB\Client($this->buildUri($uri), $uriOptions, $driverOptions); |
|
246
|
|
|
} |
|
247
|
10 |
|
); |
|
248
|
10 |
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|