Skip to content

Commit

Permalink
Add listAll() and search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Oct 29, 2023
1 parent 2747eed commit 2e00b83
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 21 deletions.
14 changes: 12 additions & 2 deletions src/AbstractStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,29 @@ abstract public function mkdir(string $directory): void;
*/
abstract public function rmdir(string $directory): void;

/**
* List all
*
* @param ?string $search
* @return array
*/
abstract public function listAll(?string $search = null): array;

/**
* List directories
*
* @param ?string $search
* @return array
*/
abstract public function listDirs(): array;
abstract public function listDirs(?string $search = null): array;

/**
* List files
*
* @param ?string $search
* @return array
*/
abstract public function listFiles(): array;
abstract public function listFiles(?string $search = null): array;

/**
* Put file
Expand Down
45 changes: 43 additions & 2 deletions src/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,32 @@ abstract public function mkdir(string $directory): void;
*/
abstract public function rmdir(string $directory): void;

/**
* List all
*
* @param ?string $search
* @return array
*/
function listAll(?string $search = null): array
{
return array_merge($this->listDirs($search), $this->listFiles($search));
}

/**
* List directories
*
* @param ?string $search
* @return array
*/
abstract public function listDirs(): array;
abstract public function listDirs(?string $search = null): array;

/**
* List files
*
* @param ?string $search
* @return array
*/
abstract public function listFiles(): array;
abstract public function listFiles(?string $search = null): array;

/**
* Put file
Expand Down Expand Up @@ -314,4 +327,32 @@ protected function scrub(string $value): string
return $value;
}

/**
* Search and filter values
*
* @param array $objects
* @param string $search
* @return array
*/
protected function searchFilter(array $objects, string $search): array
{
if (str_starts_with($search, '*')) {
$search = substr($search, 1);
$objects = array_filter($objects, function ($value) use ($search) {
return str_ends_with($value, $search);
});
} else if (str_ends_with($search, '*')) {
$search = substr($search, 0, -1);
$objects = array_filter($objects, function ($value) use ($search) {
return str_starts_with($value, $search);
});
} else {
$objects = array_filter($objects, function ($value) use ($search) {
return ($value == $search);
});
}

return $objects;
}

}
14 changes: 12 additions & 2 deletions src/Adapter/Azure.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,10 @@ public function rmdir(string $directory): void
/**
* List directories
*
* @param ?string $search
* @return array
*/
public function listDirs(): array
public function listDirs(?string $search = null): array
{
$dirs = [];

Expand Down Expand Up @@ -248,15 +249,20 @@ public function listDirs(): array
}
}

if ($search !== null) {
$dirs = $this->searchFilter($dirs, $search);
}

return $dirs;
}

/**
* List files
*
* @param ?string $search
* @return array
*/
public function listFiles(): array
public function listFiles(?string $search = null): array
{
$files = [];

Expand Down Expand Up @@ -290,6 +296,10 @@ public function listFiles(): array
}
}

if ($search !== null) {
$files = $this->searchFilter($files, $search);
}

return $files;
}

Expand Down
28 changes: 22 additions & 6 deletions src/Adapter/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,45 @@ public function rmdir(string $directory): void
/**
* List directories
*
* @param ?string $search
* @return array
*/
public function listDirs(): array
public function listDirs(?string $search = null): array
{
$directory = $this->directory;
return array_values(array_filter(scandir($directory), function($value) use ($directory) {
$directory = $this->directory;
$directories = array_map(function($value) {
return $value. DIRECTORY_SEPARATOR;
}, array_values(array_filter(scandir($directory), function($value) use ($directory) {
return (($value != '.') && ($value != '..') && file_exists($directory . DIRECTORY_SEPARATOR . $value) &&
is_dir($directory . DIRECTORY_SEPARATOR . $value));
}));
})));

if ($search !== null) {
$directories = $this->searchFilter($directories, $search);
}

return $directories;
}

/**
* List files
*
* @param ?string $search
* @return array
*/
public function listFiles(): array
public function listFiles(?string $search = null): array
{
$directory = $this->directory;
return array_values(array_filter(scandir($directory), function($value) use ($directory) {
$files = array_values(array_filter(scandir($directory), function($value) use ($directory) {
return (($value != '.') && ($value != '..') && file_exists($directory . DIRECTORY_SEPARATOR . $value) &&
!is_dir($directory . DIRECTORY_SEPARATOR . $value) && is_file($directory . DIRECTORY_SEPARATOR . $value));
}));

if ($search !== null) {
$files = $this->searchFilter($files, $search);
}

return $files;
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/Adapter/S3.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ public function rmdir(string $directory): void
/**
* List directories
*
* @param ?string $search
* @return array
*/
public function listDirs(): array
public function listDirs(?string $search = null): array
{
$dirs = [];
$params = ['Bucket' => str_replace('s3://', '', $this->baseDirectory)];
Expand All @@ -148,16 +149,21 @@ public function listDirs(): array
}
}

if ($search !== null) {
$dirs = $this->searchFilter($dirs, $search);
}

return $dirs;

}

/**
* List files
*
* @param ?string $search
* @return array
*/
public function listFiles(): array
public function listFiles(?string $search = null): array
{
$files = [];
$params = ['Bucket' => str_replace('s3://', '', $this->baseDirectory), 'Delimiter' => '/'];
Expand All @@ -175,6 +181,10 @@ public function listFiles(): array
}
}

if ($search !== null) {
$files = $this->searchFilter($files, $search);
}

return $files;
}

Expand Down
21 changes: 17 additions & 4 deletions src/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,37 @@ public function rmdir(string $directory): void
$this->adapter->rmdir($directory);
}

/**
* List all
*
* @param ?string $search
* @return array
*/
public function listAll(?string $search = null): array
{
return $this->adapter->listAll($search);
}

/**
* List directories
*
* @param ?string $search
* @return array
*/
public function listDirs(): array
public function listDirs(?string $search = null): array
{
return $this->adapter->listDirs();
return $this->adapter->listDirs($search);
}

/**
* List files
*
* @param ?string $search
* @return array
*/
public function listFiles(): array
public function listFiles(?string $search = null): array
{
return $this->adapter->listFiles();
return $this->adapter->listFiles($search);
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,29 @@ public function mkdir(string $directory): void;
*/
public function rmdir(string $directory): void;

/**
* List all
*
* @param ?string $search
* @return array
*/
public function listAll(?string $search = null): array;

/**
* List directories
*
* @param ?string $search
* @return array
*/
public function listDirs(): array;
public function listDirs(?string $search = null): array;

/**
* List files
*
* @param ?string $search
* @return array
*/
public function listFiles(): array;
public function listFiles(?string $search = null): array;

/**
* Put file
Expand Down
16 changes: 16 additions & 0 deletions tests/Adapter/AzureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ public function testListFiles()
$this->assertTrue(is_array($this->storage->listFiles()));
}

/**
* @group skip
*/
public function testListFilesSearch()
{
$this->assertTrue(is_array($this->storage->listFiles('uploaded.txt')));
}

/**
* @group skip
*/
Expand Down Expand Up @@ -326,6 +334,14 @@ public function testGetFileType()
$this->assertFalse($this->storage->getFileType('bad'));
}

/**
* @group skip
*/
public function testListDirsSearch()
{
$this->assertTrue(is_array($this->storage->listDirs('*foo/')));
}

/**
* @group skip
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/Adapter/S3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ public function testListFiles()
$this->assertTrue(is_array($this->storage->listFiles()));
}

/**
* @group skip
*/
public function testListFilesSearch()
{
$this->assertTrue(is_array($this->storage->listFiles('uploaded.txt')));
}

/**
* @group skip
*/
Expand Down Expand Up @@ -316,6 +324,14 @@ public function testListDirs()
$this->assertTrue(is_array($this->storage->listDirs()));
}

/**
* @group skip
*/
public function testListDirsSearch()
{
$this->assertTrue(is_array($this->storage->listDirs('*test/')));
}

/**
* @group skip
*/
Expand Down
Loading

0 comments on commit 2e00b83

Please sign in to comment.