Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rethink classes and remove unneccessary classes #1

Merged
merged 1 commit into from Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 44 additions & 22 deletions Source/Directory.php
Expand Up @@ -2,6 +2,9 @@

namespace PhpRepos\FileManager\Directory;

use PhpRepos\FileManager\FilesystemCollection;
use PhpRepos\FileManager\FilesystemTree;
use PhpRepos\FileManager\Path;
use function PhpRepos\FileManager\File\preserve_copy as preserve_copy_file;

function chmod(string $path, int $permission): bool
Expand Down Expand Up @@ -54,18 +57,47 @@ function is_empty(string $path): bool
return scandir($path) == ['.', '..'];
}

function ls(string $path): array
function ls(string $path): FilesystemCollection
{
$list = scandir($path);
$path = Path::from_string($path);

return array_values(array_filter($list, fn ($item) => ! str_starts_with($item, '.')));
return array_reduce(
array_values(array_filter(scandir($path), fn ($item) => ! str_starts_with($item, '.'))),
fn (FilesystemCollection $collection, string $item) => $collection->put($path->append($item)),
new FilesystemCollection()
);
}

function ls_all(string $path): array
function ls_all(string $path): FilesystemCollection
{
$list = scandir($path);
$path = Path::from_string($path);

return array_values(array_filter($list, fn ($item) => ! in_array($item, ['.', '..'])));
return array_reduce(
array_values(array_filter(scandir($path), fn ($item) => ! in_array($item, ['.', '..']))),
fn (FilesystemCollection $collection, string $item) => $collection->put($path->append($item)),
new FilesystemCollection()
);
}

function ls_recursively(string $path): FilesystemTree
{
$path = Path::from_string($path);

$tree = new FilesystemTree($path);

$add_leaves = function (Path $vertex) use ($tree, &$add_leaves) {
ls_all($vertex)
->each(function (Path $object) use ($tree, &$vertex, &$add_leaves) {
$tree->edge($vertex, $object);
if (is_dir($object)) {
$add_leaves($object);
}
});
};

$add_leaves($path);

return $tree;
}

function make(string $path, int $permission = 0775): bool
Expand Down Expand Up @@ -100,22 +132,12 @@ function preserve_copy(string $origin, string $destination): bool

function preserve_copy_recursively(string $origin, string $destination): bool
{
$result = true;

foreach (ls_all($origin) as $item) {
if (! $result) {
break;
}

$origin_item = $origin . DIRECTORY_SEPARATOR . $item;
$destination_item = $destination . DIRECTORY_SEPARATOR . $item;

$result = is_dir($origin_item)
? (preserve_copy($origin_item, $destination_item) && preserve_copy_recursively($origin_item, $destination_item))
: preserve_copy_file($origin_item, $destination_item);
}

return $result;
return ls_all($origin)
->reduce(function (bool $carry, Path $path) use ($origin, $destination) {
return $carry && is_dir($path)
? (preserve_copy($path, $path->relocate($origin, $destination)) && preserve_copy_recursively($path, $path->relocate($origin, $destination)))
: preserve_copy_file($path, $path->relocate($origin, $destination));
}, true);
}

function renew(string $path): void
Expand Down
13 changes: 13 additions & 0 deletions Source/Filename.php
@@ -0,0 +1,13 @@
<?php

namespace PhpRepos\FileManager;

use PhpRepos\Datatype\Text;

class Filename extends Text
{
public function __construct(string $init)
{
parent::__construct($init);
}
}
43 changes: 0 additions & 43 deletions Source/Filesystem/Address.php

This file was deleted.

192 changes: 0 additions & 192 deletions Source/Filesystem/Directory.php

This file was deleted.