Skip to content

Commit

Permalink
feat(Directory): copy no longer returns a Future, it will instead res…
Browse files Browse the repository at this point in the history
…olve the future internally
  • Loading branch information
razshare committed Apr 5, 2024
1 parent 3f20209 commit 310c587
Showing 1 changed file with 30 additions and 34 deletions.
64 changes: 30 additions & 34 deletions src/lib/Core/Directory.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?php
namespace CatPaw\Core;

use function Amp\async;
use function Amp\File\createDirectoryRecursively;
use function Amp\File\deleteDirectory;
use function Amp\File\isDirectory;
use function Amp\File\isFile;
use function Amp\File\listFiles;
use Amp\Future;

use FilesystemIterator;
use RegexIterator;
Expand Down Expand Up @@ -125,45 +123,43 @@ public static function list(string $directoryName):Unsafe {

/**
* Copy a directory.
* @param string $from
* @param string $to
* @param false|string $pattern regex pattern to match while scanning.
* @return Future<Unsafe<void>>
* @param string $from
* @param string $to
* @param false|string $pattern regex pattern to match while scanning.
* @return Unsafe<void>
*/
function copy(string $from, string $to, false|string $pattern = false):Future {
return async(static function() use ($from, $to, $pattern) {
if (!isDirectory($from)) {
return error("Directory $from not found.");
}
function copy(string $from, string $to, false|string $pattern = false):Unsafe {
if (!isDirectory($from)) {
return error("Directory $from not found.");
}

try {
$iterator = new FilesystemIterator($from);
} catch(Throwable $e) {
return error($e);
}
try {
$iterator = new FilesystemIterator($from);
} catch(Throwable $e) {
return error($e);
}

if (false !== $pattern) {
$iterator = new RegexIterator(
$iterator,
$pattern,
RegexIterator::GET_MATCH
);
}
if (false !== $pattern) {
$iterator = new RegexIterator(
$iterator,
$pattern,
RegexIterator::GET_MATCH
);
}

$key = str_starts_with($from, './')?substr($from, 1):$from;
$key = str_starts_with($from, './')?substr($from, 1):$from;

for ($iterator->rewind();$iterator->valid();$iterator->next()) {
foreach ($iterator->current() as $fileName) {
$parts = explode($key, $fileName, 2);
$relativeFileName = end($parts);
File::copy($fileName, "$to/$relativeFileName")->await()->try($error);
if ($error) {
return error($error);
}
for ($iterator->rewind();$iterator->valid();$iterator->next()) {
foreach ($iterator->current() as $fileName) {
$parts = explode($key, $fileName, 2);
$relativeFileName = end($parts);
File::copy($fileName, "$to/$relativeFileName")->try($error);
if ($error) {
return error($error);
}
}
return ok();
});
}
return ok();
}

private function __construct() {
Expand Down

0 comments on commit 310c587

Please sign in to comment.