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

[1.x] Refactor filesystem helpers #1699

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 9 additions & 28 deletions packages/framework/src/Foundation/Kernel/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
use Hyde\Foundation\PharSupport;
use Illuminate\Support\Collection;

use function collect;
use function Hyde\normalize_slashes;
use function Hyde\path_join;
use function file_exists;
use function str_replace;
use function array_map;
use function is_string;
use function is_array;
use function str_starts_with;
use function unslash;
Expand Down Expand Up @@ -70,6 +70,7 @@ public function path(string $path = ''): string
* Input types are matched, meaning that if the input is a string so will the output be.
*
* @param string|array<string> $path
* @return ($path is string ? string : array<string>)
*/
public function pathToAbsolute(string|array $path): string|array
{
Expand Down Expand Up @@ -99,9 +100,7 @@ public function mediaPath(string $path = ''): string
return $this->path(Hyde::getMediaDirectory());
}

$path = unslash($path);

return $this->path(Hyde::getMediaDirectory()."/$path");
return $this->path(path_join(Hyde::getMediaDirectory(), unslash($path)));
}

/**
Expand All @@ -113,9 +112,7 @@ public function sitePath(string $path = ''): string
return $this->path(Hyde::getOutputDirectory());
}

$path = unslash($path);

return $this->path(Hyde::getOutputDirectory()."/$path");
return $this->path(path_join(Hyde::getOutputDirectory(), unslash($path)));
}

/**
Expand Down Expand Up @@ -153,15 +150,9 @@ public function vendorPath(string $path = '', string $package = 'framework'): st
*/
public function touch(string|array $path): bool
{
if (is_string($path)) {
return collect($path)->map(function (string $path): bool {
return touch($this->path($path));
}

foreach ($path as $p) {
touch($this->path($p));
}

return true;
})->contains(false) === false;
}

/**
Expand All @@ -171,27 +162,17 @@ public function touch(string|array $path): bool
*/
public function unlink(string|array $path): bool
{
if (is_string($path)) {
return collect($path)->map(function (string $path): bool {
return unlink($this->path($path));
}

foreach ($path as $p) {
unlink($this->path($p));
}

return true;
})->contains(false) === false;
}

/**
* Unlink a file in the project's directory, but only if it exists.
*/
public function unlinkIfExists(string $path): bool
{
if (file_exists($this->path($path))) {
return unlink($this->path($path));
}

return false;
return file_exists($this->path($path)) && unlink($this->path($path));
}

/** @return \Illuminate\Support\Collection<int, string> */
Expand Down
Loading