Skip to content

Commit 2d4c4b3

Browse files
authored
Merge pull request #34 from tinect/features
feat: support deep listContents
2 parents 2ed5193 + 4072567 commit 2d4c4b3

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
lines changed

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
processIsolation="false"
99
stopOnFailure="false"
1010
verbose="true"
11+
bootstrap="vendor/autoload.php"
1112
>
1213
<testsuites>
1314
<testsuite name="tests">

src/BunnyCDNAdapter.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PlatformCommunity\Flysystem\BunnyCDN;
44

5+
use Exception;
56
use League\Flysystem\Config;
67
use League\Flysystem\DirectoryAttributes;
78
use League\Flysystem\DirectoryListing;
@@ -113,15 +114,15 @@ public function listContents(string $path = '', bool $deep = false): iterable
113114
// @codeCoverageIgnoreEnd
114115

115116
foreach ($entries as $item) {
116-
yield $this->normalizeObject($item);
117-
}
118-
119-
// return new DirectoryListing($contents, $deep);
120-
// return array_map(function($item) {
121-
// return $this->normalizeObject($item);
122-
// }, $entries);
117+
$content = $this->normalizeObject($item);
118+
yield $content;
123119

124-
// return $entries;
120+
if ($deep && $content instanceof DirectoryAttributes) {
121+
foreach ($this->listContents($content->path(), $deep) as $deepItem) {
122+
yield $deepItem;
123+
}
124+
}
125+
}
125126
}
126127

127128
/**
@@ -184,9 +185,6 @@ private function extractExtraMetadata(array $bunny_file_array): array
184185
*
185186
* @param string $path
186187
* @return string
187-
*
188-
* @throws Exceptions\BunnyCDNException
189-
* @throws Exceptions\NotFoundException
190188
*/
191189
public function detectMimeType(string $path): string
192190
{
@@ -199,7 +197,7 @@ public function detectMimeType(string $path): string
199197
}
200198

201199
return $mimeType;
202-
} catch (\Exception $e) {
200+
} catch (Exception) {
203201
return '';
204202
}
205203
}
@@ -219,12 +217,17 @@ public function writeStream($path, $contents, Config $config): void
219217
* @param $path
220218
* @return resource
221219
*
222-
* @throws Exceptions\BunnyCDNException
223-
* @throws Exceptions\NotFoundException
220+
* @throws UnableToReadFile
224221
*/
225222
public function readStream($path)
226223
{
227-
return $this->client->stream($path);
224+
try {
225+
return $this->client->stream($path);
226+
// @codeCoverageIgnoreStart
227+
} catch (Exceptions\BunnyCDNException|Exceptions\NotFoundException $e) {
228+
throw UnableToReadFile::fromLocation($path, $e->getMessage());
229+
}
230+
// @codeCoverageIgnoreEnd
228231
}
229232

230233
/**
@@ -279,19 +282,15 @@ public function visibility(string $path): FileAttributes
279282
{
280283
try {
281284
return new FileAttributes($this->getObject($path)->path(), null, $this->pullzone_url ? 'public' : 'private');
282-
} catch (UnableToReadFile $e) {
285+
} catch (UnableToReadFile|TypeError $e) {
283286
throw new UnableToRetrieveMetadata($e->getMessage());
284-
} catch (TypeError $e) {
285-
throw new UnableToRetrieveMetadata('Cannot retrieve visibility of folder');
286287
}
287288
}
288289

289290
/**
290291
* @param string $path
291292
* @return FileAttributes
292293
*
293-
* @throws Exceptions\BunnyCDNException
294-
* @throws Exceptions\NotFoundException
295294
* @codeCoverageIgnore
296295
*/
297296
public function mimeType(string $path): FileAttributes
@@ -323,13 +322,13 @@ public function mimeType(string $path): FileAttributes
323322
return $object;
324323
} catch (UnableToReadFile $e) {
325324
throw new UnableToRetrieveMetadata($e->getMessage());
326-
} catch (TypeError $e) {
325+
} catch (TypeError) {
327326
throw new UnableToRetrieveMetadata('Cannot retrieve mimeType of folder');
328327
}
329328
}
330329

331330
/**
332-
* @param $path
331+
* @param string $path
333332
* @return mixed
334333
*/
335334
protected function getObject(string $path = ''): StorageAttributes
@@ -342,13 +341,15 @@ protected function getObject(string $path = ''): StorageAttributes
342341

343342
if (count($list) === 1) {
344343
return $list[0];
345-
} elseif (count($list) > 1) {
344+
}
345+
346+
if (count($list) > 1) {
346347
// @codeCoverageIgnoreStart
347348
throw UnableToReadFile::fromLocation($path, 'More than one file was returned for path:"'.$path.'", contact package author.');
348-
// @codeCoverageIgnoreEnd
349-
} else {
350-
throw UnableToReadFile::fromLocation($path, 'Error 404:"'.$path.'"');
349+
// @codeCoverageIgnoreEnd
351350
}
351+
352+
throw UnableToReadFile::fromLocation($path, 'Error 404:"'.$path.'"');
352353
}
353354

354355
/**
@@ -361,7 +362,7 @@ public function lastModified(string $path): FileAttributes
361362
return $this->getObject($path);
362363
} catch (UnableToReadFile $e) {
363364
throw new UnableToRetrieveMetadata($e->getMessage());
364-
} catch (TypeError $e) {
365+
} catch (TypeError) {
365366
throw new UnableToRetrieveMetadata('Last Modified only accepts files as parameters, not directories');
366367
}
367368
}
@@ -376,7 +377,7 @@ public function fileSize(string $path): FileAttributes
376377
return $this->getObject($path);
377378
} catch (UnableToReadFile $e) {
378379
throw new UnableToRetrieveMetadata($e->getMessage());
379-
} catch (TypeError $e) {
380+
} catch (TypeError) {
380381
throw new UnableToRetrieveMetadata('Cannot retrieve size of folder');
381382
}
382383
}
@@ -443,6 +444,7 @@ public function fileExists(string $path): bool
443444
* @param string $path
444445
* @return string
445446
* @codeCoverageIgnore
447+
* @noinspection PhpUnused
446448
*/
447449
public function getUrl(string $path): string
448450
{

tests/FlysystemTestSuite.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,6 @@ public function setting_visibility(): void
8787
$this->markTestSkipped('No visibility supported');
8888
}
8989

90-
public function listing_contents_recursive(): void
91-
{
92-
$this->markTestSkipped('No recursive supported');
93-
}
94-
9590
/**
9691
* @test
9792
*/

0 commit comments

Comments
 (0)