Skip to content

Commit ecb50cd

Browse files
authored
Merge pull request #929 from hydephp/refactor-path-assembly
Refactor Filesystem path assembly helpers
2 parents e2caf5b + 20053cb commit ecb50cd

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
lines changed

packages/framework/src/Foundation/Filesystem.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Hyde\Pages\MarkdownPage;
1616
use Hyde\Pages\MarkdownPost;
1717
use Illuminate\Support\Collection;
18+
use function Hyde\system_path_join;
1819
use function is_array;
1920
use function is_string;
2021
use function str_replace;
@@ -60,7 +61,7 @@ public function path(string $path = ''): string
6061

6162
$path = unslash($path);
6263

63-
return $this->implode($this->getBasePath(), $path);
64+
return system_path_join($this->getBasePath(), $path);
6465
}
6566

6667
/**
@@ -208,7 +209,7 @@ public function getModelSourcePath(string $model, string $path = ''): string
208209

209210
$path = unslash($path);
210211

211-
return $this->path($this->implode(DiscoveryService::getModelSourceDirectory($model), $path));
212+
return $this->path(system_path_join(DiscoveryService::getModelSourceDirectory($model), $path));
212213
}
213214

214215
public function getBladePagePath(string $path = ''): string
@@ -236,12 +237,4 @@ public function smartGlob(string $pattern, int $flags = 0): Collection
236237
return collect(\Hyde\Facades\Filesystem::glob($pattern, $flags))
237238
->map(fn (string $path): string => $this->pathToRelative($path));
238239
}
239-
240-
/**
241-
* Implode path components into a string with directory separators.
242-
*/
243-
public static function implode(string $base, string ...$paths): string
244-
{
245-
return implode(DIRECTORY_SEPARATOR, array_merge([$base], $paths));
246-
}
247240
}

packages/framework/src/helpers.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,25 @@ function yaml_decode(string $input, int $flags = 0): mixed
9999
return Yaml::parse($input, $flags);
100100
}
101101
}
102+
103+
if (! function_exists('\Hyde\path_join')) {
104+
function path_join(string $directory, string ...$paths): string
105+
{
106+
return implode('/', array_merge([$directory], $paths));
107+
}
108+
}
109+
110+
if (! function_exists('\Hyde\system_path_join')) {
111+
function system_path_join(string $directory, string ...$paths): string
112+
{
113+
return implode(DIRECTORY_SEPARATOR, array_merge([$directory], $paths));
114+
}
115+
}
116+
117+
if (! function_exists('\Hyde\normalize_slashes')) {
118+
function normalize_slashes(string $string): string
119+
{
120+
return str_replace('\\', '/', $string);
121+
}
122+
}
102123
}

packages/framework/tests/Feature/Foundation/FilesystemTest.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,6 @@ public function test_path_to_relative_helper_does_not_modify_non_project_paths()
390390
}
391391
}
392392

393-
public function test_implode_helper_merges_path_components_into_a_string_with_directory_separators()
394-
{
395-
$this->assertSame($this->systemPath('foo'), Filesystem::implode('foo'));
396-
$this->assertSame($this->systemPath('foo/bar'), Filesystem::implode('foo', 'bar'));
397-
$this->assertSame($this->systemPath('foo/bar/baz'), Filesystem::implode('foo', 'bar', 'baz'));
398-
}
399-
400393
protected function systemPath(string $path): string
401394
{
402395
return str_replace('/', DIRECTORY_SEPARATOR, $path);

packages/framework/tests/Feature/HelpersTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,36 @@ public function test_hyde_yaml_decode_function_accepts_parameters()
149149
\Hyde\yaml_decode('foo: bar', 128)
150150
);
151151
}
152+
153+
/** @covers ::\Hyde\path_join */
154+
public function test_hyde_path_join_function()
155+
{
156+
$this->assertSame('foo/bar', \Hyde\path_join('foo', 'bar'));
157+
}
158+
159+
/** @covers ::\Hyde\path_join */
160+
public function test_hyde_path_join_function_with_multiple_paths()
161+
{
162+
$this->assertSame('foo/bar/baz', \Hyde\path_join('foo', 'bar', 'baz'));
163+
}
164+
165+
/** @covers ::\Hyde\system_path_join */
166+
public function test_hyde_system_path_join_function()
167+
{
168+
$this->assertSame('foo'.DIRECTORY_SEPARATOR.'bar', \Hyde\system_path_join('foo', 'bar'));
169+
}
170+
171+
/** @covers ::\Hyde\system_path_join */
172+
public function test_hyde_system_path_join_function_with_multiple_paths()
173+
{
174+
$this->assertSame('foo'.DIRECTORY_SEPARATOR.'bar'.DIRECTORY_SEPARATOR.'baz', \Hyde\system_path_join('foo', 'bar', 'baz'));
175+
}
176+
177+
/** @covers ::\Hyde\normalize_slashes */
178+
public function test_hyde_normalize_slashes_function()
179+
{
180+
$this->assertSame('foo/bar', \Hyde\normalize_slashes('foo'.DIRECTORY_SEPARATOR.'bar'));
181+
$this->assertSame('foo/bar', \Hyde\normalize_slashes('foo\\bar'));
182+
$this->assertSame('foo/bar', \Hyde\normalize_slashes('foo/bar'));
183+
}
152184
}

0 commit comments

Comments
 (0)