1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Respect\Structural\Driver\MongoDb; |
4
|
|
|
|
5
|
|
|
use Respect\Data\Collections\Collection; |
6
|
|
|
use Respect\Structural\Driver as BaseDriver; |
7
|
|
|
use Respect\Structural\Driver\Exception as DriverException; |
8
|
|
|
|
9
|
|
|
class Driver implements BaseDriver |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var BaseDriver |
13
|
|
|
*/ |
14
|
|
|
private $connection; |
15
|
|
|
|
16
|
2 |
|
protected function __construct(BaseDriver $connection) |
17
|
|
|
{ |
18
|
2 |
|
$this->connection = $connection; |
19
|
2 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $database |
23
|
|
|
* @param string $server |
24
|
|
|
* @param array $options |
25
|
|
|
* @param array $driverOptions |
26
|
|
|
* |
27
|
|
|
* @return Driver |
28
|
|
|
* |
29
|
|
|
* @throws DriverException |
30
|
|
|
*/ |
31
|
1 |
|
public static function factoryLegacy( |
32
|
|
|
$database, |
33
|
|
|
$server = 'mongodb://localhost:27017', |
34
|
|
|
array $options = ['connect' => false], |
35
|
|
|
array $driverOptions = [] |
36
|
|
|
) { |
37
|
1 |
|
if (!extension_loaded('mongo')) { |
38
|
|
|
throw DriverException::extensionNotLoaded('mongo'); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
$client = new \MongoClient($server, $options, $driverOptions); |
42
|
1 |
|
$driver = new MongoDriver($client, $database); |
43
|
|
|
|
44
|
1 |
|
return new self($driver); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $database |
49
|
|
|
* @param string $uri |
50
|
|
|
* @param array $uriOptions |
51
|
|
|
* @param array $driverOptions |
52
|
|
|
* |
53
|
|
|
* @return Driver |
54
|
|
|
* |
55
|
|
|
* @throws DriverException |
56
|
|
|
*/ |
57
|
1 |
|
public static function factory( |
58
|
|
|
$database, |
59
|
|
|
$uri = 'mongodb://localhost:27017', |
60
|
|
|
array $uriOptions = [], |
61
|
|
|
array $driverOptions = [] |
62
|
|
|
) { |
63
|
1 |
|
if (!extension_loaded('mongodb')) { |
64
|
|
|
throw DriverException::extensionNotLoaded('mongodb'); |
65
|
|
|
} |
66
|
1 |
|
$client = new \MongoDB\Client($uri, $uriOptions, $driverOptions); |
67
|
1 |
|
$driver = new MongoDbDriver($client, $database); |
68
|
|
|
|
69
|
1 |
|
return new self($driver); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return Driver |
74
|
|
|
*/ |
75
|
2 |
|
public function getConnection() |
76
|
|
|
{ |
77
|
2 |
|
return $this->connection; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
1 |
|
public function fetch(\Iterator $cursor) |
84
|
|
|
{ |
85
|
1 |
|
return $this->getConnection()->fetch($cursor); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
1 |
|
public function find($collection, array $query = []) |
92
|
|
|
{ |
93
|
1 |
|
return $this->getConnection()->find($collection, $query); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
1 |
|
public function generateQuery(Collection $collection) |
100
|
|
|
{ |
101
|
1 |
|
return $this->getConnection()->generateQuery($collection); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Collection $collection |
106
|
|
|
* @param $document |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
1 |
|
public function insert($collection, $document) |
111
|
|
|
{ |
112
|
1 |
|
$this->getConnection()->insert($collection, $document); |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
1 |
|
public function update($collection, $criteria, $document) |
119
|
|
|
{ |
120
|
1 |
|
$this->getConnection()->update($collection, $criteria, $document); |
121
|
1 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
1 |
|
public function remove($collection, $criteria) |
127
|
|
|
{ |
128
|
1 |
|
$this->getConnection()->remove($collection, $criteria); |
129
|
1 |
|
} |
130
|
|
|
} |
131
|
|
|
|