Skip to content

Commit fe8890c

Browse files
authored
Merge pull request #49 from daniel-sc/migrate-mongo-dependency
migrate (optional) ext-mongo to mongodb/mongodb + ext-mongodb
2 parents ab7f6f9 + 78e2453 commit fe8890c

File tree

7 files changed

+45
-42
lines changed

7 files changed

+45
-42
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ language: php
22
dist: precise
33

44
php:
5-
- 5.3
65
- 5.4
76
- 5.5
87

composer.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@
1919
"chunks"
2020
],
2121
"require": {
22-
"php": ">=5.3"
22+
"php": ">=5.4"
2323
},
2424
"require-dev": {
2525
"mikey179/vfsstream": "v1.2.0",
2626
"league/phpunit-coverage-listener": "~1.1",
2727
"fabpot/php-cs-fixer": "~2.2",
28-
"phpunit/phpunit": "4.*"
28+
"phpunit/phpunit": "4.*",
29+
"mongodb/mongodb": "^1.4.0",
30+
"ext-mongodb": "*"
31+
},
32+
"suggest": {
33+
"mongodb/mongodb":"Required to use this package with Mongo DB"
2934
},
3035
"autoload": {
3136
"psr-0": {

src/Flow/Mongo/MongoConfig.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Flow\Mongo;
44

55
use Flow\Config;
6+
use MongoDB\GridFS\Bucket;
67

78
/**
89
* @codeCoverageIgnore
@@ -12,17 +13,17 @@ class MongoConfig extends Config implements MongoConfigInterface
1213
private $gridFs;
1314

1415
/**
15-
* @param \MongoGridFS $gridFS storage of the upload (and chunks)
16+
* @param Bucket $gridFS storage of the upload (and chunks)
1617
*/
17-
function __construct(\MongoGridFS $gridFS)
18+
function __construct(Bucket $gridFS)
1819
{
1920
parent::__construct();
2021
$this->gridFs = $gridFS;
2122
}
2223

2324

2425
/**
25-
* @return \MongoGridFS
26+
* @return Bucket
2627
*/
2728
public function getGridFs()
2829
{

src/Flow/Mongo/MongoConfigInterface.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Flow\Mongo;
44

55
use Flow\ConfigInterface;
6+
use MongoDB\GridFS\Bucket;
67

78
/**
89
* @codeCoverageIgnore
@@ -11,7 +12,7 @@ interface MongoConfigInterface extends ConfigInterface
1112
{
1213

1314
/**
14-
* @return \MongoGridFS
15+
* @return Bucket
1516
*/
1617
public function getGridFs();
1718

src/Flow/Mongo/MongoFile.php

+24-24
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
namespace Flow\Mongo;
44

5+
use Exception;
56
use Flow\File;
67
use Flow\Request;
78
use Flow\RequestInterface;
9+
use MongoDB\BSON\Binary;
10+
use MongoDB\BSON\ObjectId;
11+
use MongoDB\BSON\UTCDateTime;
12+
use MongoDB\Operation\FindOneAndReplace;
813

914

1015
/**
@@ -42,9 +47,9 @@ protected function getGridFsFile()
4247
if (!$this->uploadGridFsFile) {
4348
$gridFsFileQuery = $this->getGridFsFileQuery();
4449
$changed = $gridFsFileQuery;
45-
$changed['flowUpdated'] = new \MongoDate();
46-
$this->uploadGridFsFile = $this->config->getGridFs()->findAndModify($gridFsFileQuery, $changed, null,
47-
['upsert' => true, 'new' => true]);
50+
$changed['flowUpdated'] = new UTCDateTime();
51+
$this->uploadGridFsFile = $this->config->getGridFs()->getFilesCollection()->findOneAndReplace($gridFsFileQuery, $changed,
52+
['upsert' => true, 'returnDocument' => FindOneAndReplace::RETURN_DOCUMENT_AFTER]);
4853
}
4954

5055
return $this->uploadGridFsFile;
@@ -56,10 +61,10 @@ protected function getGridFsFile()
5661
*/
5762
public function chunkExists($index)
5863
{
59-
return $this->config->getGridFs()->chunks->find([
64+
return $this->config->getGridFs()->getChunksCollection()->findOne([
6065
'files_id' => $this->getGridFsFile()['_id'],
6166
'n' => (intval($index) - 1)
62-
])->limit(1)->hasNext();
67+
]) !== null;
6368
}
6469

6570
public function checkChunk()
@@ -71,7 +76,7 @@ public function checkChunk()
7176
* Save chunk
7277
* @param $additionalUpdateOptions array additional options for the mongo update/upsert operation.
7378
* @return bool
74-
* @throws \Exception if upload size is invalid or some other unexpected error occurred.
79+
* @throws Exception if upload size is invalid or some other unexpected error occurred.
7580
*/
7681
public function saveChunk($additionalUpdateOptions = [])
7782
{
@@ -89,19 +94,19 @@ public function saveChunk($additionalUpdateOptions = [])
8994
($actualChunkSize < $this->request->getDefaultChunkSize() &&
9095
$this->request->getCurrentChunkNumber() != $this->request->getTotalChunks())
9196
) {
92-
throw new \Exception("Invalid upload! (size: {$actualChunkSize})");
97+
throw new Exception("Invalid upload! (size: $actualChunkSize)");
9398
}
94-
$chunk['data'] = new \MongoBinData($data, 0); // \MongoBinData::GENERIC is not defined for older mongo drivers
95-
$this->config->getGridFs()->chunks->update($chunkQuery, $chunk, array_merge(['upsert' => true], $additionalUpdateOptions));
99+
$chunk['data'] = new Binary($data, Binary::TYPE_GENERIC);
100+
$this->config->getGridFs()->getChunksCollection()->replaceOne($chunkQuery, $chunk, array_merge(['upsert' => true], $additionalUpdateOptions));
96101
unlink($file['tmp_name']);
97102

98103
$this->ensureIndices();
99104

100105
return true;
101-
} catch (\Exception $e) {
106+
} catch (Exception $e) {
102107
// try to remove a possibly (partly) stored chunk:
103108
if (isset($chunkQuery)) {
104-
$this->config->getGridFs()->chunks->remove($chunkQuery);
109+
$this->config->getGridFs()->getChunksCollection()->deleteMany($chunkQuery);
105110
}
106111
throw $e;
107112
}
@@ -113,25 +118,24 @@ public function saveChunk($additionalUpdateOptions = [])
113118
public function validateFile()
114119
{
115120
$totalChunks = intval($this->request->getTotalChunks());
116-
$storedChunks = $this->config->getGridFs()->chunks
117-
->find(['files_id' => $this->getGridFsFile()['_id']])
118-
->count();
121+
$storedChunks = $this->config->getGridFs()->getChunksCollection()
122+
->countDocuments(['files_id' => $this->getGridFsFile()['_id']]);
119123
return $totalChunks === $storedChunks;
120124
}
121125

122126

123127
/**
124128
* Merge all chunks to single file
125129
* @param $metadata array additional metadata for final file
126-
* @return \MongoId|bool of saved file or false if file was already saved
127-
* @throws \Exception
130+
* @return ObjectId|bool of saved file or false if file was already saved
131+
* @throws Exception
128132
*/
129133
public function saveToGridFs($metadata = null)
130134
{
131135
$file = $this->getGridFsFile();
132136
$file['flowStatus'] = 'finished';
133137
$file['metadata'] = $metadata;
134-
$result = $this->config->getGridFs()->findAndModify($this->getGridFsFileQuery(), $file);
138+
$result = $this->config->getGridFs()->getFilesCollection()->findOneAndReplace($this->getGridFsFileQuery(), $file);
135139
// on second invocation no more file can be found, as the flowStatus changed:
136140
if (is_null($result)) {
137141
return false;
@@ -142,7 +146,7 @@ public function saveToGridFs($metadata = null)
142146

143147
public function save($destination)
144148
{
145-
throw new \Exception("Must not use 'save' on MongoFile - use 'saveToGridFs'!");
149+
throw new Exception("Must not use 'save' on MongoFile - use 'saveToGridFs'!");
146150
}
147151

148152
public function deleteChunks()
@@ -152,14 +156,10 @@ public function deleteChunks()
152156

153157
public function ensureIndices()
154158
{
155-
$chunksCollection = $this->config->getGridFs()->chunks;
159+
$chunksCollection = $this->config->getGridFs()->getChunksCollection();
156160
$indexKeys = ['files_id' => 1, 'n' => 1];
157161
$indexOptions = ['unique' => true, 'background' => true];
158-
if(method_exists($chunksCollection, 'createIndex')) { // only available for PECL mongo >= 1.5.0
159-
$chunksCollection->createIndex($indexKeys, $indexOptions);
160-
} else {
161-
$chunksCollection->ensureIndex($indexKeys, $indexOptions);
162-
}
162+
$chunksCollection->createIndex($indexKeys, $indexOptions);
163163
}
164164

165165
/**

src/Flow/Mongo/MongoUploader.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Flow\Mongo;
44

5-
use Flow\FileOpenException;
5+
use MongoDB\GridFS\Bucket;
66

77
/**
88
* @codeCoverageIgnore
@@ -12,20 +12,17 @@ class MongoUploader
1212
/**
1313
* Delete chunks older than expiration time.
1414
*
15-
* @param \MongoGridFS $gridFs
15+
* @param Bucket $gridFs
1616
* @param int $expirationTime seconds
17-
*
18-
* @throws FileOpenException
1917
*/
2018
public static function pruneChunks($gridFs, $expirationTime = 172800)
2119
{
22-
$result = $gridFs->remove([
23-
'flowUpdated' => ['$lt' => new \MongoDate(time() - $expirationTime)],
20+
$result = $gridFs->find([
21+
'flowUpdated' => ['$lt' => new \MongoDB\BSON\UTCDateTime(time() - $expirationTime)],
2422
'flowStatus' => 'uploading'
2523
]);
26-
27-
if (!$result) {
28-
throw new FileOpenException("Could not remove chunks!");
24+
foreach ($result as $file) {
25+
$gridFs->delete($file['_id']);
2926
}
3027
}
3128
}

src/Flow/Mongo/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Usage
33

44
* Must use 'forceChunkSize=true' on client side.
55
* Chunk preprocessor not supported.
6-
* One should ensure indices on the gridfs collection on the property 'flowIdentifier'.
6+
* One should ensure indices on the gridfs files collection on the property 'flowIdentifier'.
77

88
Besides the points above, the usage is analogous to the 'normal' flow-php:
99

@@ -41,7 +41,7 @@ if ($file->validateFile()) {
4141
Delete unfinished files
4242
-----------------------
4343

44-
For this you should setup cron, which would check each chunk upload time.
44+
For this you should set up cron, which would check each chunk upload time.
4545
If chunk is uploaded long time ago, then chunk should be deleted.
4646

4747
Helper method for checking this:

0 commit comments

Comments
 (0)