Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
2b93dab1
Commit
2b93dab1
authored
Jun 06, 2016
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename GridFSCollectionsWrapper to CollectionWrapper
parent
400e829c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
45 additions
and
45 deletions
+45
-45
Bucket.php
src/GridFS/Bucket.php
+21
-21
CollectionWrapper.php
src/GridFS/CollectionWrapper.php
+5
-5
GridFSDownload.php
src/GridFS/GridFSDownload.php
+6
-6
GridFSUpload.php
src/GridFS/GridFSUpload.php
+10
-10
StreamWrapper.php
src/GridFS/StreamWrapper.php
+3
-3
No files found.
src/GridFS/Bucket.php
View file @
2b93dab1
...
@@ -22,7 +22,7 @@ class Bucket
...
@@ -22,7 +22,7 @@ class Bucket
private
static
$streamWrapper
;
private
static
$streamWrapper
;
private
static
$defaultChunkSizeBytes
=
261120
;
private
static
$defaultChunkSizeBytes
=
261120
;
private
$collection
s
Wrapper
;
private
$collectionWrapper
;
private
$databaseName
;
private
$databaseName
;
private
$options
;
private
$options
;
...
@@ -74,7 +74,7 @@ class Bucket
...
@@ -74,7 +74,7 @@ class Bucket
$collectionOptions
=
array_intersect_key
(
$options
,
[
'readPreference'
=>
1
,
'writeConcern'
=>
1
]);
$collectionOptions
=
array_intersect_key
(
$options
,
[
'readPreference'
=>
1
,
'writeConcern'
=>
1
]);
$this
->
collection
sWrapper
=
new
GridFSCollections
Wrapper
(
$manager
,
$databaseName
,
$options
[
'bucketName'
],
$collectionOptions
);
$this
->
collection
Wrapper
=
new
Collection
Wrapper
(
$manager
,
$databaseName
,
$options
[
'bucketName'
],
$collectionOptions
);
$this
->
registerStreamWrapper
(
$manager
);
$this
->
registerStreamWrapper
(
$manager
);
}
}
...
@@ -89,12 +89,12 @@ class Bucket
...
@@ -89,12 +89,12 @@ class Bucket
*/
*/
public
function
delete
(
ObjectId
$id
)
public
function
delete
(
ObjectId
$id
)
{
{
$file
=
$this
->
collection
s
Wrapper
->
getFilesCollection
()
->
findOne
([
'_id'
=>
$id
]);
$file
=
$this
->
collectionWrapper
->
getFilesCollection
()
->
findOne
([
'_id'
=>
$id
]);
$this
->
collection
s
Wrapper
->
getFilesCollection
()
->
deleteOne
([
'_id'
=>
$id
]);
$this
->
collectionWrapper
->
getFilesCollection
()
->
deleteOne
([
'_id'
=>
$id
]);
$this
->
collection
s
Wrapper
->
getChunksCollection
()
->
deleteMany
([
'files_id'
=>
$id
]);
$this
->
collectionWrapper
->
getChunksCollection
()
->
deleteMany
([
'files_id'
=>
$id
]);
if
(
$file
===
null
)
{
if
(
$file
===
null
)
{
throw
FileNotFoundException
::
byId
(
$id
,
$this
->
collection
s
Wrapper
->
getFilesCollection
()
->
getNameSpace
());
throw
FileNotFoundException
::
byId
(
$id
,
$this
->
collectionWrapper
->
getFilesCollection
()
->
getNameSpace
());
}
}
}
}
...
@@ -108,16 +108,16 @@ class Bucket
...
@@ -108,16 +108,16 @@ class Bucket
*/
*/
public
function
downloadToStream
(
ObjectId
$id
,
$destination
)
public
function
downloadToStream
(
ObjectId
$id
,
$destination
)
{
{
$file
=
$this
->
collection
s
Wrapper
->
getFilesCollection
()
->
findOne
(
$file
=
$this
->
collectionWrapper
->
getFilesCollection
()
->
findOne
(
[
'_id'
=>
$id
],
[
'_id'
=>
$id
],
[
'typeMap'
=>
[
'root'
=>
'stdClass'
]]
[
'typeMap'
=>
[
'root'
=>
'stdClass'
]]
);
);
if
(
$file
===
null
)
{
if
(
$file
===
null
)
{
throw
FileNotFoundException
::
byId
(
$id
,
$this
->
collection
s
Wrapper
->
getFilesCollection
()
->
getNameSpace
());
throw
FileNotFoundException
::
byId
(
$id
,
$this
->
collectionWrapper
->
getFilesCollection
()
->
getNameSpace
());
}
}
$gridFsStream
=
new
GridFSDownload
(
$this
->
collection
s
Wrapper
,
$file
);
$gridFsStream
=
new
GridFSDownload
(
$this
->
collectionWrapper
,
$file
);
$gridFsStream
->
downloadToStream
(
$destination
);
$gridFsStream
->
downloadToStream
(
$destination
);
}
}
...
@@ -149,7 +149,7 @@ class Bucket
...
@@ -149,7 +149,7 @@ class Bucket
{
{
$options
+=
[
'revision'
=>
-
1
];
$options
+=
[
'revision'
=>
-
1
];
$file
=
$this
->
findFileRevision
(
$filename
,
$options
[
'revision'
]);
$file
=
$this
->
findFileRevision
(
$filename
,
$options
[
'revision'
]);
$gridFsStream
=
new
GridFSDownload
(
$this
->
collection
s
Wrapper
,
$file
);
$gridFsStream
=
new
GridFSDownload
(
$this
->
collectionWrapper
,
$file
);
$gridFsStream
->
downloadToStream
(
$destination
);
$gridFsStream
->
downloadToStream
(
$destination
);
}
}
...
@@ -160,7 +160,7 @@ class Bucket
...
@@ -160,7 +160,7 @@ class Bucket
public
function
drop
()
public
function
drop
()
{
{
$this
->
collection
s
Wrapper
->
dropCollections
();
$this
->
collectionWrapper
->
dropCollections
();
}
}
/**
/**
...
@@ -173,12 +173,12 @@ class Bucket
...
@@ -173,12 +173,12 @@ class Bucket
*/
*/
public
function
find
(
$filter
,
array
$options
=
[])
public
function
find
(
$filter
,
array
$options
=
[])
{
{
return
$this
->
collection
s
Wrapper
->
getFilesCollection
()
->
find
(
$filter
,
$options
);
return
$this
->
collectionWrapper
->
getFilesCollection
()
->
find
(
$filter
,
$options
);
}
}
public
function
getCollectionsWrapper
()
public
function
getCollectionsWrapper
()
{
{
return
$this
->
collection
s
Wrapper
;
return
$this
->
collectionWrapper
;
}
}
public
function
getDatabaseName
()
public
function
getDatabaseName
()
...
@@ -212,13 +212,13 @@ class Bucket
...
@@ -212,13 +212,13 @@ class Bucket
*/
*/
public
function
openDownloadStream
(
ObjectId
$id
)
public
function
openDownloadStream
(
ObjectId
$id
)
{
{
$file
=
$this
->
collection
s
Wrapper
->
getFilesCollection
()
->
findOne
(
$file
=
$this
->
collectionWrapper
->
getFilesCollection
()
->
findOne
(
[
'_id'
=>
$id
],
[
'_id'
=>
$id
],
[
'typeMap'
=>
[
'root'
=>
'stdClass'
]]
[
'typeMap'
=>
[
'root'
=>
'stdClass'
]]
);
);
if
(
$file
===
null
)
{
if
(
$file
===
null
)
{
throw
FileNotFoundException
::
byId
(
$id
,
$this
->
collection
s
Wrapper
->
getFilesCollection
()
->
getNameSpace
());
throw
FileNotFoundException
::
byId
(
$id
,
$this
->
collectionWrapper
->
getFilesCollection
()
->
getNameSpace
());
}
}
return
$this
->
openDownloadStreamByFile
(
$file
);
return
$this
->
openDownloadStreamByFile
(
$file
);
...
@@ -273,7 +273,7 @@ class Bucket
...
@@ -273,7 +273,7 @@ class Bucket
$options
+=
[
'chunkSizeBytes'
=>
$this
->
options
[
'chunkSizeBytes'
]];
$options
+=
[
'chunkSizeBytes'
=>
$this
->
options
[
'chunkSizeBytes'
]];
$streamOptions
=
[
$streamOptions
=
[
'collection
sWrapper'
=>
$this
->
collections
Wrapper
,
'collection
Wrapper'
=>
$this
->
collection
Wrapper
,
'uploadOptions'
=>
$options
,
'uploadOptions'
=>
$options
,
];
];
...
@@ -291,10 +291,10 @@ class Bucket
...
@@ -291,10 +291,10 @@ class Bucket
*/
*/
public
function
rename
(
ObjectId
$id
,
$newFilename
)
public
function
rename
(
ObjectId
$id
,
$newFilename
)
{
{
$filesCollection
=
$this
->
collection
s
Wrapper
->
getFilesCollection
();
$filesCollection
=
$this
->
collectionWrapper
->
getFilesCollection
();
$result
=
$filesCollection
->
updateOne
([
'_id'
=>
$id
],
[
'$set'
=>
[
'filename'
=>
$newFilename
]]);
$result
=
$filesCollection
->
updateOne
([
'_id'
=>
$id
],
[
'$set'
=>
[
'filename'
=>
$newFilename
]]);
if
(
$result
->
getModifiedCount
()
==
0
)
{
if
(
$result
->
getModifiedCount
()
==
0
)
{
throw
FileNotFoundException
::
byId
(
$id
,
$this
->
collection
s
Wrapper
->
getFilesCollection
()
->
getNameSpace
());
throw
FileNotFoundException
::
byId
(
$id
,
$this
->
collectionWrapper
->
getFilesCollection
()
->
getNameSpace
());
}
}
}
}
...
@@ -314,7 +314,7 @@ class Bucket
...
@@ -314,7 +314,7 @@ class Bucket
public
function
uploadFromStream
(
$filename
,
$source
,
array
$options
=
[])
public
function
uploadFromStream
(
$filename
,
$source
,
array
$options
=
[])
{
{
$options
+=
[
'chunkSizeBytes'
=>
$this
->
options
[
'chunkSizeBytes'
]];
$options
+=
[
'chunkSizeBytes'
=>
$this
->
options
[
'chunkSizeBytes'
]];
$gridFsStream
=
new
GridFSUpload
(
$this
->
collection
s
Wrapper
,
$filename
,
$options
);
$gridFsStream
=
new
GridFSUpload
(
$this
->
collectionWrapper
,
$filename
,
$options
);
return
$gridFsStream
->
uploadFromStream
(
$source
);
return
$gridFsStream
->
uploadFromStream
(
$source
);
}
}
...
@@ -329,7 +329,7 @@ class Bucket
...
@@ -329,7 +329,7 @@ class Bucket
$sortOrder
=
1
;
$sortOrder
=
1
;
}
}
$filesCollection
=
$this
->
collection
s
Wrapper
->
getFilesCollection
();
$filesCollection
=
$this
->
collectionWrapper
->
getFilesCollection
();
$file
=
$filesCollection
->
findOne
(
$file
=
$filesCollection
->
findOne
(
[
'filename'
=>
$filename
],
[
'filename'
=>
$filename
],
[
[
...
@@ -349,7 +349,7 @@ class Bucket
...
@@ -349,7 +349,7 @@ class Bucket
private
function
openDownloadStreamByFile
(
$file
)
private
function
openDownloadStreamByFile
(
$file
)
{
{
$options
=
[
$options
=
[
'collection
sWrapper'
=>
$this
->
collections
Wrapper
,
'collection
Wrapper'
=>
$this
->
collection
Wrapper
,
'file'
=>
$file
,
'file'
=>
$file
,
];
];
...
...
src/GridFS/
GridFSCollections
Wrapper.php
→
src/GridFS/
Collection
Wrapper.php
View file @
2b93dab1
...
@@ -5,14 +5,13 @@ namespace MongoDB\GridFS;
...
@@ -5,14 +5,13 @@ namespace MongoDB\GridFS;
use
MongoDB\Collection
;
use
MongoDB\Collection
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\WriteConcern
;
/**
/**
*
GridFSCollections
Wrapper abstracts the GridFS files and chunks collections.
*
Collection
Wrapper abstracts the GridFS files and chunks collections.
*
*
* @internal
* @internal
*/
*/
class
GridFSCollections
Wrapper
class
Collection
Wrapper
{
{
private
$chunksCollection
;
private
$chunksCollection
;
private
$ensuredIndexes
=
false
;
private
$ensuredIndexes
=
false
;
...
@@ -34,8 +33,9 @@ class GridFSCollectionsWrapper
...
@@ -34,8 +33,9 @@ class GridFSCollectionsWrapper
$this
->
chunksCollection
=
new
Collection
(
$manager
,
$databaseName
,
sprintf
(
'%s.chunks'
,
$bucketName
),
$collectionOptions
);
$this
->
chunksCollection
=
new
Collection
(
$manager
,
$databaseName
,
sprintf
(
'%s.chunks'
,
$bucketName
),
$collectionOptions
);
}
}
public
function
dropCollections
(){
public
function
dropCollections
()
$this
->
filesCollection
->
drop
();
{
$this
->
filesCollection
->
drop
();
$this
->
chunksCollection
->
drop
();
$this
->
chunksCollection
->
drop
();
}
}
...
...
src/GridFS/GridFSDownload.php
View file @
2b93dab1
...
@@ -19,7 +19,7 @@ class GridFSDownload
...
@@ -19,7 +19,7 @@ class GridFSDownload
private
$bytesSeen
=
0
;
private
$bytesSeen
=
0
;
private
$chunkOffset
=
0
;
private
$chunkOffset
=
0
;
private
$chunksIterator
;
private
$chunksIterator
;
private
$collection
s
Wrapper
;
private
$collectionWrapper
;
private
$file
;
private
$file
;
private
$firstCheck
=
true
;
private
$firstCheck
=
true
;
private
$iteratorEmpty
=
false
;
private
$iteratorEmpty
=
false
;
...
@@ -28,17 +28,17 @@ class GridFSDownload
...
@@ -28,17 +28,17 @@ class GridFSDownload
/**
/**
* Constructs a GridFS download stream.
* Constructs a GridFS download stream.
*
*
* @param
GridFSCollectionsWrapper $collectionsWrapper GridFS collections
wrapper
* @param
CollectionWrapper $collectionWrapper GridFS collection
wrapper
* @param stdClass
$file GridFS file document
* @param stdClass $file GridFS file document
* @throws CorruptFileException
* @throws CorruptFileException
*/
*/
public
function
__construct
(
GridFSCollectionsWrapper
$collections
Wrapper
,
stdClass
$file
)
public
function
__construct
(
CollectionWrapper
$collection
Wrapper
,
stdClass
$file
)
{
{
$this
->
collection
sWrapper
=
$collections
Wrapper
;
$this
->
collection
Wrapper
=
$collection
Wrapper
;
$this
->
file
=
$file
;
$this
->
file
=
$file
;
try
{
try
{
$cursor
=
$this
->
collection
s
Wrapper
->
getChunksCollection
()
->
find
(
$cursor
=
$this
->
collectionWrapper
->
getChunksCollection
()
->
find
(
[
'files_id'
=>
$this
->
file
->
_id
],
[
'files_id'
=>
$this
->
file
->
_id
],
[
'sort'
=>
[
'n'
=>
1
]]
[
'sort'
=>
[
'n'
=>
1
]]
);
);
...
...
src/GridFS/GridFSUpload.php
View file @
2b93dab1
...
@@ -19,7 +19,7 @@ class GridFSUpload
...
@@ -19,7 +19,7 @@ class GridFSUpload
private
$bufferLength
=
0
;
private
$bufferLength
=
0
;
private
$chunkOffset
=
0
;
private
$chunkOffset
=
0
;
private
$chunkSize
;
private
$chunkSize
;
private
$collection
s
Wrapper
;
private
$collectionWrapper
;
private
$ctx
;
private
$ctx
;
private
$file
;
private
$file
;
private
$indexChecker
;
private
$indexChecker
;
...
@@ -44,12 +44,12 @@ class GridFSUpload
...
@@ -44,12 +44,12 @@ class GridFSUpload
* * metadata (document): User data for the "metadata" field of the files
* * metadata (document): User data for the "metadata" field of the files
* collection document.
* collection document.
*
*
* @param
GridFSCollectionsWrapper $collectionsWrapper GridFS collections
wrapper
* @param
CollectionWrapper $collectionWrapper GridFS collection
wrapper
* @param string
$filename
File name
* @param string
$filename
File name
* @param array
$options
Upload options
* @param array
$options
Upload options
* @throws InvalidArgumentException
* @throws InvalidArgumentException
*/
*/
public
function
__construct
(
GridFSCollectionsWrapper
$collections
Wrapper
,
$filename
,
array
$options
=
[])
public
function
__construct
(
CollectionWrapper
$collection
Wrapper
,
$filename
,
array
$options
=
[])
{
{
$options
+=
[
'chunkSizeBytes'
=>
261120
];
$options
+=
[
'chunkSizeBytes'
=>
261120
];
...
@@ -66,7 +66,7 @@ class GridFSUpload
...
@@ -66,7 +66,7 @@ class GridFSUpload
}
}
$this
->
chunkSize
=
$options
[
'chunkSizeBytes'
];
$this
->
chunkSize
=
$options
[
'chunkSizeBytes'
];
$this
->
collection
sWrapper
=
$collections
Wrapper
;
$this
->
collection
Wrapper
=
$collection
Wrapper
;
$this
->
buffer
=
fopen
(
'php://temp'
,
'w+'
);
$this
->
buffer
=
fopen
(
'php://temp'
,
'w+'
);
$this
->
ctx
=
hash_init
(
'md5'
);
$this
->
ctx
=
hash_init
(
'md5'
);
...
@@ -189,8 +189,8 @@ class GridFSUpload
...
@@ -189,8 +189,8 @@ class GridFSUpload
private
function
abort
()
private
function
abort
()
{
{
$this
->
collection
s
Wrapper
->
getChunksCollection
()
->
deleteMany
([
'files_id'
=>
$this
->
file
[
'_id'
]]);
$this
->
collectionWrapper
->
getChunksCollection
()
->
deleteMany
([
'files_id'
=>
$this
->
file
[
'_id'
]]);
$this
->
collection
s
Wrapper
->
getFilesCollection
()
->
deleteOne
([
'_id'
=>
$this
->
file
[
'_id'
]]);
$this
->
collectionWrapper
->
getFilesCollection
()
->
deleteOne
([
'_id'
=>
$this
->
file
[
'_id'
]]);
$this
->
isClosed
=
true
;
$this
->
isClosed
=
true
;
}
}
...
@@ -215,7 +215,7 @@ class GridFSUpload
...
@@ -215,7 +215,7 @@ class GridFSUpload
$this
->
file
[
'length'
]
=
$this
->
length
;
$this
->
file
[
'length'
]
=
$this
->
length
;
$this
->
file
[
'md5'
]
=
$md5
;
$this
->
file
[
'md5'
]
=
$md5
;
$this
->
collection
s
Wrapper
->
insertFile
(
$this
->
file
);
$this
->
collectionWrapper
->
insertFile
(
$this
->
file
);
return
$this
->
file
[
'_id'
];
return
$this
->
file
[
'_id'
];
}
}
...
@@ -235,7 +235,7 @@ class GridFSUpload
...
@@ -235,7 +235,7 @@ class GridFSUpload
hash_update
(
$this
->
ctx
,
$data
);
hash_update
(
$this
->
ctx
,
$data
);
$this
->
collection
s
Wrapper
->
insertChunk
(
$toUpload
);
$this
->
collectionWrapper
->
insertChunk
(
$toUpload
);
$this
->
length
+=
strlen
(
$data
);
$this
->
length
+=
strlen
(
$data
);
$this
->
chunkOffset
++
;
$this
->
chunkOffset
++
;
}
}
...
...
src/GridFS/StreamWrapper.php
View file @
2b93dab1
...
@@ -26,7 +26,7 @@ class StreamWrapper
...
@@ -26,7 +26,7 @@ class StreamWrapper
public
function
openReadStream
()
public
function
openReadStream
()
{
{
$context
=
stream_context_get_options
(
$this
->
context
);
$context
=
stream_context_get_options
(
$this
->
context
);
$this
->
gridFSStream
=
new
GridFSDownload
(
$this
->
collection
s
Wrapper
,
$context
[
'gridfs'
][
'file'
]);
$this
->
gridFSStream
=
new
GridFSDownload
(
$this
->
collectionWrapper
,
$context
[
'gridfs'
][
'file'
]);
$this
->
id
=
$this
->
gridFSStream
->
getId
();
$this
->
id
=
$this
->
gridFSStream
->
getId
();
return
true
;
return
true
;
...
@@ -36,7 +36,7 @@ class StreamWrapper
...
@@ -36,7 +36,7 @@ class StreamWrapper
{
{
$context
=
stream_context_get_options
(
$this
->
context
);
$context
=
stream_context_get_options
(
$this
->
context
);
$options
=
$context
[
'gridfs'
][
'uploadOptions'
];
$options
=
$context
[
'gridfs'
][
'uploadOptions'
];
$this
->
gridFSStream
=
new
GridFSUpload
(
$this
->
collection
s
Wrapper
,
$this
->
identifier
,
$options
);
$this
->
gridFSStream
=
new
GridFSUpload
(
$this
->
collectionWrapper
,
$this
->
identifier
,
$options
);
$this
->
id
=
$this
->
gridFSStream
->
getId
();
$this
->
id
=
$this
->
gridFSStream
->
getId
();
return
true
;
return
true
;
...
@@ -68,7 +68,7 @@ class StreamWrapper
...
@@ -68,7 +68,7 @@ class StreamWrapper
{
{
$this
->
initProtocol
(
$path
);
$this
->
initProtocol
(
$path
);
$context
=
stream_context_get_options
(
$this
->
context
);
$context
=
stream_context_get_options
(
$this
->
context
);
$this
->
collection
sWrapper
=
$context
[
'gridfs'
][
'collections
Wrapper'
];
$this
->
collection
Wrapper
=
$context
[
'gridfs'
][
'collection
Wrapper'
];
$this
->
mode
=
$mode
;
$this
->
mode
=
$mode
;
switch
(
$this
->
mode
)
{
switch
(
$this
->
mode
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment