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
32600bb0
Commit
32600bb0
authored
May 10, 2016
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Relocate GridFS exception classes and use static constructors
parent
c3e4774e
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
107 additions
and
47 deletions
+107
-47
GridFSCorruptFileException.php
src/Exception/GridFSCorruptFileException.php
+0
-7
GridFSFileNotFoundException.php
src/Exception/GridFSFileNotFoundException.php
+0
-11
Bucket.php
src/GridFS/Bucket.php
+11
-11
CorruptFileException.php
src/GridFS/Exception/CorruptFileException.php
+43
-0
FileNotFoundException.php
src/GridFS/Exception/FileNotFoundException.php
+35
-0
GridFSDownload.php
src/GridFS/GridFSDownload.php
+7
-7
BucketFunctionalTest.php
tests/GridFS/BucketFunctionalTest.php
+6
-6
SpecificationTests.php
tests/GridFS/SpecificationTests.php
+5
-5
No files found.
src/Exception/GridFSCorruptFileException.php
deleted
100644 → 0
View file @
c3e4774e
<?php
namespace
MongoDB\Exception
;
class
GridFSCorruptFileException
extends
\MongoDB\Driver\Exception\RuntimeException
implements
Exception
{
}
src/Exception/GridFSFileNotFoundException.php
deleted
100644 → 0
View file @
c3e4774e
<?php
namespace
MongoDB\Exception
;
class
GridFSFileNotFoundException
extends
\MongoDB\Driver\Exception\RuntimeException
implements
Exception
{
public
function
__construct
(
$filename
,
$namespace
)
{
parent
::
__construct
(
sprintf
(
'Unable to find file "%s" in namespace "%s"'
,
$filename
,
$namespace
));
}
}
src/GridFS/Bucket.php
View file @
32600bb0
...
@@ -7,8 +7,8 @@ use MongoDB\Driver\Cursor;
...
@@ -7,8 +7,8 @@ use MongoDB\Driver\Cursor;
use
MongoDB\Driver\Manager
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Exception\GridFSFileNotFoundException
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\GridFS\Exception\FileNotFoundException
;
use
MongoDB\Operation\Find
;
use
MongoDB\Operation\Find
;
/**
/**
...
@@ -84,7 +84,7 @@ class Bucket
...
@@ -84,7 +84,7 @@ class Bucket
* attempt to delete orphaned chunks.
* attempt to delete orphaned chunks.
*
*
* @param ObjectId $id ObjectId of the file
* @param ObjectId $id ObjectId of the file
* @throws
GridFS
FileNotFoundException
* @throws FileNotFoundException
*/
*/
public
function
delete
(
ObjectId
$id
)
public
function
delete
(
ObjectId
$id
)
{
{
...
@@ -93,7 +93,7 @@ class Bucket
...
@@ -93,7 +93,7 @@ class Bucket
$this
->
collectionsWrapper
->
getChunksCollection
()
->
deleteMany
([
'files_id'
=>
$id
]);
$this
->
collectionsWrapper
->
getChunksCollection
()
->
deleteMany
([
'files_id'
=>
$id
]);
if
(
$file
===
null
)
{
if
(
$file
===
null
)
{
throw
new
GridFSFileNotFoundException
(
$id
,
$this
->
collectionsWrapper
->
getFilesCollection
()
->
getNameSpace
());
throw
FileNotFoundException
::
byId
(
$id
,
$this
->
collectionsWrapper
->
getFilesCollection
()
->
getNameSpace
());
}
}
}
}
...
@@ -103,7 +103,7 @@ class Bucket
...
@@ -103,7 +103,7 @@ class Bucket
*
*
* @param ObjectId $id ObjectId of the file
* @param ObjectId $id ObjectId of the file
* @param resource $destination Writable Stream
* @param resource $destination Writable Stream
* @throws
GridFS
FileNotFoundException
* @throws FileNotFoundException
*/
*/
public
function
downloadToStream
(
ObjectId
$id
,
$destination
)
public
function
downloadToStream
(
ObjectId
$id
,
$destination
)
{
{
...
@@ -113,7 +113,7 @@ class Bucket
...
@@ -113,7 +113,7 @@ class Bucket
);
);
if
(
$file
===
null
)
{
if
(
$file
===
null
)
{
throw
new
GridFSFileNotFoundException
(
$id
,
$this
->
collectionsWrapper
->
getFilesCollection
()
->
getNameSpace
());
throw
FileNotFoundException
::
byId
(
$id
,
$this
->
collectionsWrapper
->
getFilesCollection
()
->
getNameSpace
());
}
}
$gridFsStream
=
new
GridFSDownload
(
$this
->
collectionsWrapper
,
$file
);
$gridFsStream
=
new
GridFSDownload
(
$this
->
collectionsWrapper
,
$file
);
...
@@ -142,7 +142,7 @@ class Bucket
...
@@ -142,7 +142,7 @@ class Bucket
* @param string $filename File name
* @param string $filename File name
* @param resource $destination Writable Stream
* @param resource $destination Writable Stream
* @param array $options Download options
* @param array $options Download options
* @throws
GridFS
FileNotFoundException
* @throws FileNotFoundException
*/
*/
public
function
downloadToStreamByName
(
$filename
,
$destination
,
array
$options
=
[])
public
function
downloadToStreamByName
(
$filename
,
$destination
,
array
$options
=
[])
{
{
...
@@ -207,7 +207,7 @@ class Bucket
...
@@ -207,7 +207,7 @@ class Bucket
*
*
* @param ObjectId $id ObjectId of the file
* @param ObjectId $id ObjectId of the file
* @return resource
* @return resource
* @throws
GridFS
FileNotFoundException
* @throws FileNotFoundException
*/
*/
public
function
openDownloadStream
(
ObjectId
$id
)
public
function
openDownloadStream
(
ObjectId
$id
)
{
{
...
@@ -217,7 +217,7 @@ class Bucket
...
@@ -217,7 +217,7 @@ class Bucket
);
);
if
(
$file
===
null
)
{
if
(
$file
===
null
)
{
throw
new
GridFSFileNotFoundException
(
$id
,
$this
->
collectionsWrapper
->
getFilesCollection
()
->
getNameSpace
());
throw
FileNotFoundException
::
byId
(
$id
,
$this
->
collectionsWrapper
->
getFilesCollection
()
->
getNameSpace
());
}
}
return
$this
->
openDownloadStreamByFile
(
$file
);
return
$this
->
openDownloadStreamByFile
(
$file
);
...
@@ -245,7 +245,7 @@ class Bucket
...
@@ -245,7 +245,7 @@ class Bucket
* @param string $filename File name
* @param string $filename File name
* @param array $options Download options
* @param array $options Download options
* @return resource
* @return resource
* @throws
GridFS
FileNotFoundException
* @throws FileNotFoundException
*/
*/
public
function
openDownloadStreamByName
(
$filename
,
array
$options
=
[])
public
function
openDownloadStreamByName
(
$filename
,
array
$options
=
[])
{
{
...
@@ -293,7 +293,7 @@ class Bucket
...
@@ -293,7 +293,7 @@ class Bucket
$filesCollection
=
$this
->
collectionsWrapper
->
getFilesCollection
();
$filesCollection
=
$this
->
collectionsWrapper
->
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
new
GridFSFileNotFoundException
(
$id
,
$this
->
collectionsWrapper
->
getFilesCollection
()
->
getNameSpace
());
throw
FileNotFoundException
::
byId
(
$id
,
$this
->
collectionsWrapper
->
getFilesCollection
()
->
getNameSpace
());
}
}
}
}
...
@@ -339,7 +339,7 @@ class Bucket
...
@@ -339,7 +339,7 @@ class Bucket
);
);
if
(
$file
===
null
)
{
if
(
$file
===
null
)
{
throw
new
GridFSFileNotFoundException
(
$filename
,
$filesCollection
->
getNameSpace
());
throw
FileNotFoundException
::
byFilenameAndRevision
(
$filename
,
$revision
,
$filesCollection
->
getNameSpace
());
}
}
return
$file
;
return
$file
;
...
...
src/GridFS/Exception/CorruptFileException.php
0 → 100644
View file @
32600bb0
<?php
namespace
MongoDB\GridFS\Exception
;
use
MongoDB\Exception\RuntimeException
;
class
CorruptFileException
extends
RuntimeException
{
/**
* Thrown when a chunk is not found for an expected index.
*
* @param integer $expectedIndex Expected index number
* @return self
*/
public
static
function
missingChunk
(
$expectedIndex
)
{
return
new
static
(
sprintf
(
'Chunk not found for index "%d"'
,
$expectedIndex
));
}
/**
* Thrown when a chunk has an unexpected index number.
*
* @param integer $index Actual index number (i.e. "n" field)
* @param integer $expectedIndex Expected index number
* @return self
*/
public
static
function
unexpectedIndex
(
$index
,
$expectedIndex
)
{
return
new
static
(
sprintf
(
'Expected chunk to have index "%d" but found "%d"'
,
$expectedIndex
,
$index
));
}
/**
* Thrown when a chunk has an unexpected data size.
*
* @param integer $size Actual size (i.e. "data" field length)
* @param integer $expectedSize Expected size
* @return self
*/
public
static
function
unexpectedSize
(
$size
,
$expectedSize
)
{
return
new
static
(
sprintf
(
'Expected chunk to have size "%d" but found "%d"'
,
$expectedSize
,
$size
));
}
}
src/GridFS/Exception/FileNotFoundException.php
0 → 100644
View file @
32600bb0
<?php
namespace
MongoDB\GridFS\Exception
;
use
MongoDB\Exception\RuntimeException
;
class
FileNotFoundException
extends
RuntimeException
{
/**
* Thrown when a file cannot be found by its filename and revision.
*
* @param string $filename Filename
* @param integer $revision Revision
* @param string $namespace Namespace for the files collection
* @return self
*/
public
static
function
byFilenameAndRevision
(
$filename
,
$revision
,
$namespace
)
{
return
new
static
(
sprintf
(
'File with name "%s" and revision "%d" not found in "%s"'
,
$filename
,
$revision
,
$namespace
));
}
/**
* Thrown when a file cannot be found by its ID.
*
* @param mixed $id File ID
* @param string $namespace Namespace for the files collection
* @return self
*/
public
static
function
byId
(
$id
,
$namespace
)
{
$json
=
\MongoDB\BSON\toJSON
(
\MongoDB\BSON\fromPHP
([
'_id'
=>
$id
]));
return
new
static
(
sprintf
(
'File "%s" not found in "%s"'
,
$json
,
$namespace
));
}
}
src/GridFS/GridFSDownload.php
View file @
32600bb0
...
@@ -3,7 +3,7 @@
...
@@ -3,7 +3,7 @@
namespace
MongoDB\GridFS
;
namespace
MongoDB\GridFS
;
use
MongoDB\Driver\Exception\Exception
;
use
MongoDB\Driver\Exception\Exception
;
use
MongoDB\
Exception\GridFS
CorruptFileException
;
use
MongoDB\
GridFS\Exception\
CorruptFileException
;
use
stdClass
;
use
stdClass
;
/**
/**
...
@@ -30,7 +30,7 @@ class GridFSDownload
...
@@ -30,7 +30,7 @@ class GridFSDownload
*
*
* @param GridFSCollectionsWrapper $collectionsWrapper GridFS collections wrapper
* @param GridFSCollectionsWrapper $collectionsWrapper GridFS collections wrapper
* @param stdClass $file GridFS file document
* @param stdClass $file GridFS file document
* @throws
GridFS
CorruptFileException
* @throws CorruptFileException
*/
*/
public
function
__construct
(
GridFSCollectionsWrapper
$collectionsWrapper
,
stdClass
$file
)
public
function
__construct
(
GridFSCollectionsWrapper
$collectionsWrapper
,
stdClass
$file
)
{
{
...
@@ -43,8 +43,8 @@ class GridFSDownload
...
@@ -43,8 +43,8 @@ class GridFSDownload
[
'sort'
=>
[
'n'
=>
1
]]
[
'sort'
=>
[
'n'
=>
1
]]
);
);
}
catch
(
Exception
$e
)
{
}
catch
(
Exception
$e
)
{
// TODO: Why do we replace a driver exception with
GridFS
CorruptFileException here?
// TODO: Why do we replace a driver exception with CorruptFileException here?
throw
new
GridFS
CorruptFileException
();
throw
new
CorruptFileException
();
}
}
$this
->
chunksIterator
=
new
\IteratorIterator
(
$cursor
);
$this
->
chunksIterator
=
new
\IteratorIterator
(
$cursor
);
...
@@ -138,11 +138,11 @@ class GridFSDownload
...
@@ -138,11 +138,11 @@ class GridFSDownload
}
}
if
(
!
$this
->
chunksIterator
->
valid
())
{
if
(
!
$this
->
chunksIterator
->
valid
())
{
throw
new
GridFSCorruptFileException
(
);
throw
CorruptFileException
::
missingChunk
(
$this
->
chunkOffset
);
}
}
if
(
$this
->
chunksIterator
->
current
()
->
n
!=
$this
->
chunkOffset
)
{
if
(
$this
->
chunksIterator
->
current
()
->
n
!=
$this
->
chunkOffset
)
{
throw
new
GridFSCorruptFileException
(
);
throw
CorruptFileException
::
unexpectedIndex
(
$this
->
chunksIterator
->
current
()
->
n
,
$this
->
chunkOffset
);
}
}
$actualChunkSize
=
strlen
(
$this
->
chunksIterator
->
current
()
->
data
->
getData
());
$actualChunkSize
=
strlen
(
$this
->
chunksIterator
->
current
()
->
data
->
getData
());
...
@@ -152,7 +152,7 @@ class GridFSDownload
...
@@ -152,7 +152,7 @@ class GridFSDownload
:
$this
->
file
->
chunkSize
;
:
$this
->
file
->
chunkSize
;
if
(
$actualChunkSize
!=
$expectedChunkSize
)
{
if
(
$actualChunkSize
!=
$expectedChunkSize
)
{
throw
new
GridFSCorruptFileException
(
);
throw
CorruptFileException
::
unexpectedSize
(
$actualChunkSize
,
$expectedChunkSize
);
}
}
$this
->
bytesSeen
+=
$actualChunkSize
;
$this
->
bytesSeen
+=
$actualChunkSize
;
...
...
tests/GridFS/BucketFunctionalTest.php
View file @
32600bb0
...
@@ -73,7 +73,7 @@ class BucketFunctionalTest extends FunctionalTestCase
...
@@ -73,7 +73,7 @@ class BucketFunctionalTest extends FunctionalTestCase
}
catch
(
\MongoDB\Exception\Exception
$e
)
{
}
catch
(
\MongoDB\Exception\Exception
$e
)
{
$error
=
$e
;
$error
=
$e
;
}
}
$fileNotFound
=
'\MongoDB\
Exception\GridFS
FileNotFoundException'
;
$fileNotFound
=
'\MongoDB\
GridFS\Exception\
FileNotFoundException'
;
$this
->
assertTrue
(
$error
instanceof
$fileNotFound
);
$this
->
assertTrue
(
$error
instanceof
$fileNotFound
);
$this
->
assertEquals
(
0
,
$this
->
bucket
->
getCollectionsWrapper
()
->
getFilesCollection
()
->
count
());
$this
->
assertEquals
(
0
,
$this
->
bucket
->
getCollectionsWrapper
()
->
getFilesCollection
()
->
count
());
$this
->
assertEquals
(
0
,
$this
->
bucket
->
getCollectionsWrapper
()
->
getChunksCollection
()
->
count
());
$this
->
assertEquals
(
0
,
$this
->
bucket
->
getCollectionsWrapper
()
->
getChunksCollection
()
->
count
());
...
@@ -116,7 +116,7 @@ class BucketFunctionalTest extends FunctionalTestCase
...
@@ -116,7 +116,7 @@ class BucketFunctionalTest extends FunctionalTestCase
}
catch
(
\MongoDB\Exception\Exception
$e
)
{
}
catch
(
\MongoDB\Exception\Exception
$e
)
{
$error
=
$e
;
$error
=
$e
;
}
}
$corruptFileError
=
'\MongoDB\
Exception\GridFSCO
rruptFileException'
;
$corruptFileError
=
'\MongoDB\
GridFS\Exception\Co
rruptFileException'
;
$this
->
assertTrue
(
$error
instanceof
$corruptFileError
);
$this
->
assertTrue
(
$error
instanceof
$corruptFileError
);
}
}
public
function
testErrorsOnMissingChunk
()
public
function
testErrorsOnMissingChunk
()
...
@@ -131,7 +131,7 @@ class BucketFunctionalTest extends FunctionalTestCase
...
@@ -131,7 +131,7 @@ class BucketFunctionalTest extends FunctionalTestCase
}
catch
(
\MongoDB\Exception\Exception
$e
)
{
}
catch
(
\MongoDB\Exception\Exception
$e
)
{
$error
=
$e
;
$error
=
$e
;
}
}
$corruptFileError
=
'\MongoDB\
Exception\GridFSCO
rruptFileException'
;
$corruptFileError
=
'\MongoDB\
GridFS\Exception\Co
rruptFileException'
;
$this
->
assertTrue
(
$error
instanceof
$corruptFileError
);
$this
->
assertTrue
(
$error
instanceof
$corruptFileError
);
}
}
public
function
testUploadEnsureIndexes
()
public
function
testUploadEnsureIndexes
()
...
@@ -177,7 +177,7 @@ class BucketFunctionalTest extends FunctionalTestCase
...
@@ -177,7 +177,7 @@ class BucketFunctionalTest extends FunctionalTestCase
}
catch
(
\MongoDB\Exception\Exception
$e
)
{
}
catch
(
\MongoDB\Exception\Exception
$e
)
{
$error
=
$e
;
$error
=
$e
;
}
}
$fileNotFound
=
'\MongoDB\
Exception\GridFS
FileNotFoundException'
;
$fileNotFound
=
'\MongoDB\
GridFS\Exception\
FileNotFoundException'
;
$this
->
assertTrue
(
$error
instanceof
$fileNotFound
);
$this
->
assertTrue
(
$error
instanceof
$fileNotFound
);
}
}
public
function
testGetVersion
()
public
function
testGetVersion
()
...
@@ -194,7 +194,7 @@ class BucketFunctionalTest extends FunctionalTestCase
...
@@ -194,7 +194,7 @@ class BucketFunctionalTest extends FunctionalTestCase
$this
->
assertEquals
(
"bar"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
[
'revision'
=>
-
2
])));
$this
->
assertEquals
(
"bar"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
[
'revision'
=>
-
2
])));
$this
->
assertEquals
(
"foo"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
[
'revision'
=>
-
3
])));
$this
->
assertEquals
(
"foo"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
[
'revision'
=>
-
3
])));
$fileNotFound
=
'\MongoDB\
Exception\GridFS
FileNotFoundException'
;
$fileNotFound
=
'\MongoDB\
GridFS\Exception\
FileNotFoundException'
;
$error
=
null
;
$error
=
null
;
try
{
try
{
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
[
'revision'
=>
3
]);
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
[
'revision'
=>
3
]);
...
@@ -278,7 +278,7 @@ class BucketFunctionalTest extends FunctionalTestCase
...
@@ -278,7 +278,7 @@ class BucketFunctionalTest extends FunctionalTestCase
}
catch
(
\MongoDB\Exception\Exception
$e
)
{
}
catch
(
\MongoDB\Exception\Exception
$e
)
{
$error
=
$e
;
$error
=
$e
;
}
}
$fileNotFound
=
'\MongoDB\
Exception\GridFS
FileNotFoundException'
;
$fileNotFound
=
'\MongoDB\
GridFS\Exception\
FileNotFoundException'
;
$this
->
assertTrue
(
$error
instanceof
$fileNotFound
);
$this
->
assertTrue
(
$error
instanceof
$fileNotFound
);
$this
->
assertEquals
(
"testing"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"second_name"
)));
$this
->
assertEquals
(
"testing"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"second_name"
)));
...
...
tests/GridFS/SpecificationTests.php
View file @
32600bb0
...
@@ -52,11 +52,11 @@ class SpecificationTests extends FunctionalTestCase
...
@@ -52,11 +52,11 @@ class SpecificationTests extends FunctionalTestCase
}
catch
(
\MongoDB\Exception\Exception
$e
)
{
}
catch
(
\MongoDB\Exception\Exception
$e
)
{
$error
=
$e
;
$error
=
$e
;
}
}
$errors
=
[
'FileNotFound'
=>
'\MongoDB\
Exception\GridFS
FileNotFoundException'
,
$errors
=
[
'FileNotFound'
=>
'\MongoDB\
GridFS\Exception\
FileNotFoundException'
,
'ChunkIsMissing'
=>
'\MongoDB\
Exception\GridFS
CorruptFileException'
,
'ChunkIsMissing'
=>
'\MongoDB\
GridFS\Exception\
CorruptFileException'
,
'ExtraChunk'
=>
'\MongoDB\
Exception\GridFS
CorruptFileException'
,
'ExtraChunk'
=>
'\MongoDB\
GridFS\Exception\
CorruptFileException'
,
'ChunkIsWrongSize'
=>
'\MongoDB\
Exception\GridFS
CorruptFileException'
,
'ChunkIsWrongSize'
=>
'\MongoDB\
GridFS\Exception\
CorruptFileException'
,
'RevisionNotFound'
=>
'\MongoDB\
Exception\GridFS
FileNotFoundException'
'RevisionNotFound'
=>
'\MongoDB\
GridFS\Exception\
FileNotFoundException'
];
];
if
(
!
isset
(
$test
[
'assert'
][
'error'
]))
{
if
(
!
isset
(
$test
[
'assert'
][
'error'
]))
{
$this
->
assertNull
(
$error
);
$this
->
assertNull
(
$error
);
...
...
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