Skip to content

Commit a105380

Browse files
committed
Big refactoring: commands refactoring, changed class naming and interfaces due to MongoDB Driver Crud spec etc.
1 parent 6d0ed70 commit a105380

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+952
-1633
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
The Tequila MongoDB project is a library, which provides the high-level abstraction around the new low-level [PHP MongoDB driver](https://github.com/mongodb/mongo-php-driver), e.g.
44
schema-management, abstractions around connections, databases, collections, indexes etc.
55

6-
The library works on PHP 5.6.0 or higher, PHP 7.0 or higher, MongoDB 3.0 or higher. It also requires the PHP `mongodb` extension (the MongoDB driver for PHP) to be installed.
6+
The library works on PHP 5.6.0 or higher, PHP 7.0 or higher, MongoDB 3.2 or higher. It also requires the PHP `mongodb` extension (the MongoDB driver for PHP) to be installed.
77

8-
The library is under an active development and is NOT ready for use right now. The first release is planned for October 1, 2016.
8+
The library is under an active development and is NOT ready for use right now. The first release is planned for October 7, 2016.
99
Contributions are appreciated.
1010

1111
The documentation will come soon.

src/Client.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Tequila\MongoDB;
4+
5+
use MongoDB\Driver\Manager;
6+
use Tequila\MongoDB\Command\DropDatabase;
7+
use Tequila\MongoDB\Command\ListDatabases;
8+
use Tequila\MongoDB\Exception\UnexpectedResultException;
9+
use Tequila\MongoDB\Options\Connection\ConnectionOptions;
10+
use Tequila\MongoDB\Options\Driver\DriverOptions;
11+
12+
class Client
13+
{
14+
/**
15+
* @var Manager
16+
*/
17+
private $manager;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $uri;
23+
24+
/**
25+
* @var array
26+
*/
27+
private $typeMap;
28+
29+
/**
30+
* @param string $uri
31+
* @param array $uriOptions
32+
* @param array $driverOptions
33+
*/
34+
public function __construct($uri = 'mongodb://localhost:27017', array $uriOptions = [], array $driverOptions = [])
35+
{
36+
$uriOptions = ConnectionOptions::resolve($uriOptions);
37+
$driverOptions = DriverOptions::resolve($driverOptions);
38+
39+
$this->typeMap = $driverOptions['typeMap'];
40+
unset($driverOptions['typeMap']);
41+
42+
$this->uri = $uri;
43+
$this->manager = new Manager((string)$uri, $uriOptions, $driverOptions);
44+
}
45+
46+
/**
47+
* @param $databaseName
48+
* @param array $options
49+
* @return array|object
50+
*/
51+
public function dropDatabase($databaseName, array $options = [])
52+
{
53+
$command = new DropDatabase($databaseName, $options);
54+
$cursor = $command->execute($this->manager);
55+
56+
return current($cursor->toArray());
57+
}
58+
59+
/**
60+
* @return array
61+
*/
62+
public function listDatabases()
63+
{
64+
$cursor = (new ListDatabases())->execute($this->manager);
65+
$result = current($cursor->toArray());
66+
67+
if (isset($result['databases']) && is_array($result['databases'])) {
68+
return $result['databases'];
69+
}
70+
71+
throw new UnexpectedResultException('listDatabases command did not return expected "databases" array');
72+
}
73+
74+
/**
75+
* @param string $databaseName
76+
* @param string $collectionName
77+
* @param array $options
78+
* @return Collection
79+
*/
80+
public function selectCollection($databaseName, $collectionName, array $options = [])
81+
{
82+
$options += ['typeMap' => $this->typeMap];
83+
84+
return new Collection($this->manager, $databaseName, $collectionName, $options);
85+
}
86+
87+
/**
88+
* @param string $databaseName
89+
* @param $options
90+
* @return Database
91+
*/
92+
public function selectDatabase($databaseName, array $options = [])
93+
{
94+
$options += ['typeMap' => $this->typeMap];
95+
96+
return new Database($this->manager, $databaseName, $options);
97+
}
98+
}

src/Collection.php

Lines changed: 124 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
namespace Tequila\MongoDB;
44

5+
use MongoDB\Driver\Manager;
6+
use MongoDB\Driver\ReadConcern;
7+
use MongoDB\Driver\ReadPreference;
8+
use MongoDB\Driver\WriteConcern;
9+
use Tequila\MongoDB\BSON\BSONArray;
10+
use Tequila\MongoDB\Command\CreateIndexes;
11+
use Tequila\MongoDB\Command\DropCollection;
12+
use Tequila\MongoDB\Command\DropIndexes;
13+
use Tequila\MongoDB\Command\ListIndexes;
514
use Tequila\MongoDB\Operation\Find;
15+
use Tequila\MongoDB\Options\DatabaseAndCollectionOptions;
616
use Tequila\MongoDB\Write\Bulk\BulkWrite;
717
use Tequila\MongoDB\Write\Bulk\BulkWriteOptions;
818
use Tequila\MongoDB\Write\Model\DeleteMany;
@@ -19,12 +29,10 @@
1929

2030
class Collection
2131
{
22-
use Traits\ReadPreferenceAndConcernsTrait;
23-
2432
/**
25-
* @var Connection
33+
* @var Manager
2634
*/
27-
private $connection;
35+
private $manager;
2836

2937
/**
3038
* @var string
@@ -34,18 +42,45 @@ class Collection
3442
/**
3543
* @var string
3644
*/
37-
private $name;
45+
private $collectionName;
46+
47+
/**
48+
* @var ReadConcern|null
49+
*/
50+
private $readConcern;
51+
52+
/**
53+
* @var ReadPreference|null
54+
*/
55+
private $readPreference;
56+
57+
/**
58+
* @var WriteConcern|null
59+
*/
60+
private $writeConcern;
61+
62+
/**
63+
* @var array
64+
*/
65+
private $typeMap;
3866

3967
/**
40-
* @param Connection $connection
68+
* @param Manager $manager
4169
* @param string $databaseName
4270
* @param string $collectionName
71+
* @param array $options
4372
*/
44-
public function __construct(Connection $connection, $databaseName, $collectionName)
73+
public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [])
4574
{
46-
$this->connection = $connection;
47-
$this->databaseName = $databaseName;
48-
$this->name = $collectionName;
75+
$this->manager = $manager;
76+
$this->databaseName = (string)$databaseName;
77+
$this->collectionName = (string)$collectionName;
78+
79+
$options = DatabaseAndCollectionOptions::resolve($options, $manager);
80+
$this->readConcern = $options['readConcern'];
81+
$this->readPreference = $options['readPreference'];
82+
$this->writeConcern = $options['writeConcern'];
83+
$this->typeMap = $options['typeMap'];
4984
}
5085

5186
/**
@@ -59,17 +94,52 @@ public function getDatabaseName()
5994
/**
6095
* @return string
6196
*/
62-
public function getName()
97+
public function getCollectionName()
6398
{
64-
return $this->name;
99+
return $this->collectionName;
65100
}
66101

67102
/**
103+
* @param array $options
68104
* @return array
69105
*/
70-
public function drop()
106+
public function drop(array $options = [])
107+
{
108+
$command = new DropCollection($this->databaseName, $this->collectionName, $options);
109+
$cursor = $command->execute($this->manager);
110+
111+
return current($cursor->toArray());
112+
}
113+
114+
/**
115+
* @param array $options
116+
* @return array|object
117+
*/
118+
public function dropIndexes(array $options = [])
119+
{
120+
$command = new DropIndexes($this->databaseName, $this->collectionName, '*', $options);
121+
$cursor = $command->execute($this->manager);
122+
123+
return current($cursor->toArray());
124+
}
125+
126+
/**
127+
* @param string $indexName
128+
* @param array $options
129+
* @return array|object
130+
*/
131+
public function dropIndex($indexName, array $options = [])
71132
{
72-
return $this->connection->dropCollection($this->databaseName, $this->name);
133+
$command = new DropIndexes(
134+
$this->databaseName,
135+
$this->collectionName,
136+
$indexName,
137+
$options
138+
);
139+
140+
$cursor = $command->execute($this->manager);
141+
142+
return current($cursor->toArray());
73143
}
74144

75145
/**
@@ -79,10 +149,36 @@ public function drop()
79149
*/
80150
public function bulkWrite(array $requests, array $options = [])
81151
{
82-
$options = $options + ['writeConcern' => $this->getWriteConcern()];
152+
$options = $options + ['writeConcern' => $this->writeConcern];
83153
$bulk = new BulkWrite($requests, $options);
84154

85-
return $bulk->execute($this->connection, $this->databaseName, $this->name);
155+
return $bulk->execute($this->manager, $this->databaseName, $this->collectionName);
156+
}
157+
158+
/**
159+
* @param Index[] $indexes
160+
* @return string[]
161+
*/
162+
public function createIndexes(array $indexes)
163+
{
164+
$command = new CreateIndexes($this->databaseName, $this->collectionName, $indexes);
165+
$command->execute($this->manager);
166+
167+
return array_map(function(Index $index) {
168+
return $index->getName();
169+
}, $indexes);
170+
}
171+
172+
/**
173+
* @param array $key
174+
* @param array $options
175+
* @return string
176+
*/
177+
public function createIndex(array $key, array $options = [])
178+
{
179+
$index = new Index($key, $options);
180+
181+
return current($this->createIndexes([$index]));
86182
}
87183

88184
/**
@@ -94,7 +190,7 @@ public function find($filter = [], array $options = [])
94190
{
95191
$operation = new Find($filter, $options);
96192

97-
return $operation->execute($this->connection, $this->databaseName, $this->name);
193+
return $operation->execute($this->manager, $this->databaseName, $this->collectionName);
98194
}
99195

100196
/**
@@ -104,7 +200,6 @@ public function find($filter = [], array $options = [])
104200
*/
105201
public function insertOne($document, array $options = [])
106202
{
107-
108203
$model = new InsertOne($document);
109204
$bulkWriteResult = $this->bulkWrite([$model], $options);
110205

@@ -157,6 +252,17 @@ public function deleteMany($filter, array $options = [])
157252
return new DeleteResult($bulkWriteResult);
158253
}
159254

255+
/**
256+
* @return array|BSONArray
257+
*/
258+
public function listIndexes()
259+
{
260+
$command = new ListIndexes($this->databaseName, $this->collectionName);
261+
$cursor = $command->execute($this->manager);
262+
263+
return $cursor->toArray();
264+
}
265+
160266
/**
161267
* @param array|object $filter
162268
* @param $update

src/CollectionInterface.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)