Skip to content

Commit fc12c77

Browse files
committed
Add stream debug methods, update docs, and remove unused methods
Classes using CollectionWrapper should rely on its public API rather than work with its files and chunks collections directly.
1 parent 6b9e7b0 commit fc12c77

File tree

3 files changed

+67
-23
lines changed

3 files changed

+67
-23
lines changed

src/GridFS/CollectionWrapper.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
*/
1818
class CollectionWrapper
1919
{
20+
private $bucketName;
2021
private $chunksCollection;
22+
private $databaseName;
2123
private $checkedIndexes = false;
2224
private $filesCollection;
2325

@@ -33,6 +35,9 @@ class CollectionWrapper
3335
*/
3436
public function __construct(Manager $manager, $databaseName, $bucketName, array $collectionOptions = [])
3537
{
38+
$this->databaseName = (string) $databaseName;
39+
$this->bucketName = (string) $bucketName;
40+
3641
$this->filesCollection = new Collection($manager, $databaseName, sprintf('%s.files', $bucketName), $collectionOptions);
3742
$this->chunksCollection = new Collection($manager, $databaseName, sprintf('%s.chunks', $bucketName), $collectionOptions);
3843
}
@@ -135,10 +140,14 @@ public function findFiles($filter, array $options = [])
135140
return $this->filesCollection->find($filter, $options);
136141
}
137142

138-
// TODO: Remove this
139-
public function getChunksCollection()
143+
/**
144+
* Return the bucket name.
145+
*
146+
* @return string
147+
*/
148+
public function getBucketName()
140149
{
141-
return $this->chunksCollection;
150+
return $this->bucketName;
142151
}
143152

144153
/**
@@ -160,10 +169,14 @@ public function getChunksIteratorByFilesId($id)
160169
return new IteratorIterator($cursor);
161170
}
162171

163-
// TODO: Remove this
164-
public function getFilesCollection()
172+
/**
173+
* Return the database name.
174+
*
175+
* @return string
176+
*/
177+
public function getDatabaseName()
165178
{
166-
return $this->filesCollection;
179+
return $this->databaseName;
167180
}
168181

169182
/**

src/GridFS/ReadableStream.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class ReadableStream
2020
private $chunkSize;
2121
private $chunkOffset = 0;
2222
private $chunksIterator;
23+
private $collectionWrapper;
2324
private $firstCheck = true;
2425
private $id;
2526
private $iteratorEmpty = false;
@@ -52,10 +53,28 @@ public function __construct(CollectionWrapper $collectionWrapper, stdClass $file
5253
$this->length = $file->length;
5354

5455
$this->chunksIterator = $collectionWrapper->getChunksIteratorByFilesId($this->id);
56+
$this->collectionWrapper = $collectionWrapper;
5557
$this->numChunks = ceil($this->length / $this->chunkSize);
5658
$this->initEmptyBuffer();
5759
}
5860

61+
/**
62+
* Return internal properties for debugging purposes.
63+
*
64+
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
65+
* @return array
66+
*/
67+
public function __debugInfo()
68+
{
69+
return [
70+
'bucketName' => $this->collectionWrapper->getBucketName(),
71+
'databaseName' => $this->collectionWrapper->getDatabaseName(),
72+
'id' => $this->id,
73+
'chunkSize' => $this->chunkSize,
74+
'length' => $this->length,
75+
];
76+
}
77+
5978
public function close()
6079
{
6180
fclose($this->buffer);
@@ -129,7 +148,7 @@ public function downloadToStream($destination)
129148
}
130149

131150
/**
132-
* Return the ID of the GridFS file document for this stream.
151+
* Return the stream's ID (i.e. file document identifier).
133152
*
134153
* @return integer
135154
*/
@@ -139,7 +158,7 @@ public function getId()
139158
}
140159

141160
/**
142-
* Return the stream size in bytes.
161+
* Return the stream's size in bytes.
143162
*
144163
* @return integer
145164
*/

src/GridFS/WritableStream.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ public function __construct(CollectionWrapper $collectionWrapper, $filename, arr
8989
] + array_intersect_key($options, ['aliases' => 1, 'contentType' => 1, 'metadata' => 1]);
9090
}
9191

92+
/**
93+
* Return internal properties for debugging purposes.
94+
*
95+
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
96+
* @return array
97+
*/
98+
public function __debugInfo()
99+
{
100+
return [
101+
'bucketName' => $this->collectionWrapper->getBucketName(),
102+
'databaseName' => $this->collectionWrapper->getDatabaseName(),
103+
'file' => $this->file,
104+
];
105+
}
106+
92107
/**
93108
* Closes an active stream and flushes all buffered data to GridFS.
94109
*/
@@ -111,26 +126,23 @@ public function close()
111126
$this->isClosed = true;
112127
}
113128

114-
public function getChunkSize()
115-
{
116-
return $this->chunkSize;
117-
}
118-
119-
public function getFile()
120-
{
121-
return $this->file;
122-
}
123-
129+
/**
130+
* Return the stream's ID (i.e. file document identifier).
131+
*
132+
* @return integer
133+
*/
124134
public function getId()
125135
{
126136
return $this->file['_id'];
127137
}
128138

129-
public function getLength()
130-
{
131-
return $this->length;
132-
}
133-
139+
/**
140+
* Return the stream's size in bytes.
141+
*
142+
* Note: this value will increase as more data is written to the stream.
143+
*
144+
* @return integer
145+
*/
134146
public function getSize()
135147
{
136148
return $this->length;

0 commit comments

Comments
 (0)