Skip to content

Commit

Permalink
Merge pull request #1699 from hydephp/refactor-filesystem-helpers
Browse files Browse the repository at this point in the history
[1.x] Refactor filesystem helpers
  • Loading branch information
caendesilva authored Apr 29, 2024
2 parents ef46df7 + 258c223 commit c013d93
Showing 1 changed file with 9 additions and 28 deletions.
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

0 comments on commit c013d93

Please sign in to comment.