Skip to content

Commit

Permalink
Merge pull request #930 from hydephp/refactor-filesystem-to-use-unix-…
Browse files Browse the repository at this point in the history
…directory-separators

Refactor Filesystem to use Unix directory separators
  • Loading branch information
caendesilva committed Feb 8, 2023
2 parents d55e80b + cbaaf1b commit 78bc01d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 78 deletions.
27 changes: 12 additions & 15 deletions packages/framework/src/Foundation/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
use Hyde\Pages\MarkdownPage;
use Hyde\Pages\MarkdownPost;
use Illuminate\Support\Collection;
use function Hyde\system_path_join;
use function Hyde\normalize_slashes;
use function Hyde\path_join;
use function is_array;
use function is_string;
use function str_replace;
Expand Down Expand Up @@ -61,7 +62,7 @@ public function path(string $path = ''): string

$path = unslash($path);

return system_path_join($this->getBasePath(), $path);
return path_join($this->getBasePath(), $path);
}

/**
Expand All @@ -80,14 +81,12 @@ public function pathToAbsolute(string|array $path): string|array

/**
* Decode an absolute path created with a Hyde::path() helper into its relative counterpart.
*
* @todo Normalize slashes to forward slashes?
*/
public function pathToRelative(string $path): string
{
return str_starts_with($path, $this->path())
return normalize_slashes(str_starts_with($path, $this->path())
? unslash(str_replace($this->path(), '', $path))
: $path;
: $path);
}

/**
Expand All @@ -96,12 +95,12 @@ public function pathToRelative(string $path): string
public function mediaPath(string $path = ''): string
{
if (empty($path)) {
return Hyde::path(Hyde::getMediaDirectory());
return $this->path(Hyde::getMediaDirectory());
}

$path = unslash($path);

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

/**
Expand All @@ -110,12 +109,12 @@ public function mediaPath(string $path = ''): string
public function sitePath(string $path = ''): string
{
if (empty($path)) {
return Hyde::path(Site::getOutputDirectory());
return $this->path(Site::getOutputDirectory());
}

$path = unslash($path);

return Hyde::path(Site::getOutputDirectory().DIRECTORY_SEPARATOR.$path);
return $this->path(Site::getOutputDirectory()."/$path");
}

/**
Expand All @@ -124,12 +123,12 @@ public function sitePath(string $path = ''): string
public function siteMediaPath(string $path = ''): string
{
if (empty($path)) {
return Hyde::sitePath(Hyde::getMediaOutputDirectory());
return $this->sitePath(Hyde::getMediaOutputDirectory());
}

$path = unslash($path);

return Hyde::sitePath(Hyde::getMediaOutputDirectory().DIRECTORY_SEPARATOR.$path);
return $this->sitePath(Hyde::getMediaOutputDirectory()."/$path");
}

/**
Expand Down Expand Up @@ -207,9 +206,7 @@ public function getModelSourcePath(string $model, string $path = ''): string
return $this->path(DiscoveryService::getModelSourceDirectory($model));
}

$path = unslash($path);

return $this->path(system_path_join(DiscoveryService::getModelSourceDirectory($model), $path));
return $this->path(path_join(DiscoveryService::getModelSourceDirectory($model), unslash($path)));
}

public function getBladePagePath(string $path = ''): string
Expand Down
7 changes: 0 additions & 7 deletions packages/framework/src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,6 @@ function path_join(string $directory, string ...$paths): string
}
}

if (! function_exists('\Hyde\system_path_join')) {
function system_path_join(string $directory, string ...$paths): string
{
return implode(DIRECTORY_SEPARATOR, array_merge([$directory], $paths));
}
}

if (! function_exists('\Hyde\normalize_slashes')) {
function normalize_slashes(string $string): string
{
Expand Down
50 changes: 20 additions & 30 deletions packages/framework/tests/Feature/Foundation/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Hyde\Pages\MarkdownPage;
use Hyde\Pages\MarkdownPost;
use Hyde\Testing\TestCase;
use function Hyde\normalize_slashes;

/**
* @covers \Hyde\Foundation\HydeKernel
Expand Down Expand Up @@ -62,31 +63,31 @@ public function test_path_method_returns_base_path_when_not_supplied_with_argume
public function test_path_method_returns_path_relative_to_base_path_when_supplied_with_argument()
{
Hyde::getInstance()->setBasePath('/foo');
$this->assertEquals('/foo'.DIRECTORY_SEPARATOR.'foo/bar.php', $this->filesystem->path('foo/bar.php'));
$this->assertEquals('/foo/foo/bar.php', $this->filesystem->path('foo/bar.php'));
}

public function test_path_method_returns_qualified_file_path_when_supplied_with_argument()
{
Hyde::getInstance()->setBasePath('/foo');
$this->assertEquals('/foo'.DIRECTORY_SEPARATOR.'file.php', $this->filesystem->path('file.php'));
$this->assertEquals('/foo/file.php', $this->filesystem->path('file.php'));
}

public function test_path_method_returns_expected_value_for_nested_path_arguments()
{
Hyde::getInstance()->setBasePath('/foo');
$this->assertEquals('/foo'.DIRECTORY_SEPARATOR.'directory/file.php', $this->filesystem->path('directory/file.php'));
$this->assertEquals('/foo/directory/file.php', $this->filesystem->path('directory/file.php'));
}

public function test_path_method_strips_trailing_directory_separators_from_argument()
{
Hyde::getInstance()->setBasePath('/foo');
$this->assertEquals('/foo'.DIRECTORY_SEPARATOR.'file.php', $this->filesystem->path('\\/file.php/'));
$this->assertEquals('/foo/file.php', $this->filesystem->path('\\/file.php/'));
}

public function test_path_method_returns_expected_value_regardless_of_trailing_directory_separators_in_argument()
{
Hyde::getInstance()->setBasePath('/foo');
$this->assertEquals('/foo'.DIRECTORY_SEPARATOR.'bar/file.php', $this->filesystem->path('\\/bar/file.php/'));
$this->assertEquals('/foo/bar/file.php', $this->filesystem->path('\\/bar/file.php/'));
}

public function test_vendor_path_method_exists()
Expand All @@ -107,7 +108,7 @@ public function test_vendor_path_method_returns_qualified_file_path_when_supplie
public function test_vendor_path_method_returns_expected_value_regardless_of_trailing_directory_separators_in_argument()
{
Hyde::getInstance()->setBasePath('/foo');
$this->assertEquals('/foo'.DIRECTORY_SEPARATOR.'vendor/hyde/framework/file.php', $this->filesystem->vendorPath('\\//file.php/'));
$this->assertEquals('/foo/vendor/hyde/framework/file.php', $this->filesystem->vendorPath('\\//file.php/'));
}

public function test_copy_method()
Expand Down Expand Up @@ -190,22 +191,22 @@ public function test_get_model_source_path_method_returns_path_for_model_classes
public function test_get_model_source_path_method_returns_path_to_file_for_model_classes()
{
$this->assertEquals(
Hyde::path('_posts'.DIRECTORY_SEPARATOR.'foo.md'),
Hyde::path('_posts/foo.md'),
Hyde::getModelSourcePath(MarkdownPost::class, 'foo.md')
);

$this->assertEquals(
Hyde::path('_pages'.DIRECTORY_SEPARATOR.'foo.md'),
Hyde::path('_pages/foo.md'),
Hyde::getModelSourcePath(MarkdownPage::class, 'foo.md')
);

$this->assertEquals(
Hyde::path('_docs'.DIRECTORY_SEPARATOR.'foo.md'),
Hyde::path('_docs/foo.md'),
Hyde::getModelSourcePath(DocumentationPage::class, 'foo.md')
);

$this->assertEquals(
Hyde::path('_pages'.DIRECTORY_SEPARATOR.'foo.md'),
Hyde::path('_pages/foo.md'),
Hyde::getModelSourcePath(BladePage::class, 'foo.md')
);
}
Expand Down Expand Up @@ -253,7 +254,7 @@ public function test_helper_for_media_path()
public function test_helper_for_media_path_returns_path_to_file_within_the_directory()
{
$this->assertEquals(
Hyde::path('_media'.DIRECTORY_SEPARATOR.'foo.css'),
Hyde::path('_media/foo.css'),
Hyde::mediaPath('foo.css')
);
}
Expand All @@ -269,23 +270,23 @@ public function test_get_media_path_returns_absolute_path()
public function test_helper_for_media_output_path()
{
$this->assertEquals(
Hyde::path('_site'.DIRECTORY_SEPARATOR.'media'),
Hyde::path('_site/media'),
Hyde::siteMediaPath()
);
}

public function test_helper_for_media_output_path_returns_path_to_file_within_the_directory()
{
$this->assertEquals(
Hyde::path('_site'.DIRECTORY_SEPARATOR.'media'.DIRECTORY_SEPARATOR.'foo.css'),
Hyde::path('_site/media/foo.css'),
Hyde::siteMediaPath('foo.css')
);
}

public function test_get_media_output_path_returns_absolute_path()
{
$this->assertEquals(
Hyde::path('_site'.DIRECTORY_SEPARATOR.'media'),
Hyde::path('_site/media'),
Hyde::siteMediaPath()
);
}
Expand All @@ -301,7 +302,7 @@ public function test_helper_for_site_output_path()
public function test_helper_for_site_output_path_returns_path_to_file_within_the_directory()
{
$this->assertEquals(
Hyde::path('_site'.DIRECTORY_SEPARATOR.'foo.html'),
Hyde::path('_site/foo.html'),
Hyde::sitePath('foo.html')
);
}
Expand All @@ -317,7 +318,7 @@ public function test_get_site_output_path_returns_absolute_path()
public function test_site_output_path_helper_ignores_trailing_slashes()
{
$this->assertEquals(
Hyde::path('_site'.DIRECTORY_SEPARATOR.'foo.html'),
Hyde::path('_site/foo.html'),
Hyde::sitePath('/foo.html/')
);
}
Expand Down Expand Up @@ -348,12 +349,11 @@ public function test_pathToAbsolute_can_convert_array_of_paths()

public function test_path_to_relative_helper_decodes_hyde_path_into_relative()
{
$s = DIRECTORY_SEPARATOR;
$this->assertEquals('foo', Hyde::pathToRelative(Hyde::path('foo')));
$this->assertEquals('foo', Hyde::pathToRelative(Hyde::path('/foo/')));
$this->assertEquals('foo.md', Hyde::pathToRelative(Hyde::path('foo.md')));
$this->assertEquals("foo{$s}bar", Hyde::pathToRelative(Hyde::path("foo{$s}bar")));
$this->assertEquals("foo{$s}bar.md", Hyde::pathToRelative(Hyde::path("foo{$s}bar.md")));
$this->assertEquals('foo/bar', Hyde::pathToRelative(Hyde::path('foo/bar')));
$this->assertEquals('foo/bar.md', Hyde::pathToRelative(Hyde::path('foo/bar.md')));
}

public function test_path_to_relative_helper_does_not_modify_already_relative_paths()
Expand Down Expand Up @@ -381,17 +381,7 @@ public function test_path_to_relative_helper_does_not_modify_non_project_paths()
];

foreach ($testStrings as $testString) {
$this->assertEquals(
$this->systemPath($testString),
Hyde::pathToRelative(
$this->systemPath($testString)
)
);
$this->assertEquals(normalize_slashes($testString), Hyde::pathToRelative($testString));
}
}

protected function systemPath(string $path): string
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}
}
13 changes: 0 additions & 13 deletions packages/framework/tests/Feature/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,9 @@ public function test_hyde_path_join_function_with_multiple_paths()
$this->assertSame('foo/bar/baz', \Hyde\path_join('foo', 'bar', 'baz'));
}

/** @covers ::\Hyde\system_path_join */
public function test_hyde_system_path_join_function()
{
$this->assertSame('foo'.DIRECTORY_SEPARATOR.'bar', \Hyde\system_path_join('foo', 'bar'));
}

/** @covers ::\Hyde\system_path_join */
public function test_hyde_system_path_join_function_with_multiple_paths()
{
$this->assertSame('foo'.DIRECTORY_SEPARATOR.'bar'.DIRECTORY_SEPARATOR.'baz', \Hyde\system_path_join('foo', 'bar', 'baz'));
}

/** @covers ::\Hyde\normalize_slashes */
public function test_hyde_normalize_slashes_function()
{
$this->assertSame('foo/bar', \Hyde\normalize_slashes('foo'.DIRECTORY_SEPARATOR.'bar'));
$this->assertSame('foo/bar', \Hyde\normalize_slashes('foo\\bar'));
$this->assertSame('foo/bar', \Hyde\normalize_slashes('foo/bar'));
}
Expand Down
14 changes: 7 additions & 7 deletions packages/framework/tests/Feature/HydeKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ public function test_filesystem_helper_returns_the_kernel_filesystem_instance()
public function test_path_returns_qualified_path_for_given_path()
{
$this->assertSame(Hyde::getBasePath(), Hyde::path());
$this->assertSame(Hyde::getBasePath().DIRECTORY_SEPARATOR.'foo', Hyde::path('foo'));
$this->assertSame(Hyde::getBasePath().'/foo', Hyde::path('foo'));
}

public function test_vendor_path_returns_qualified_path_for_given_path()
{
$this->assertSame(Hyde::getBasePath().DIRECTORY_SEPARATOR.'vendor/hyde/framework', Hyde::vendorPath());
$this->assertSame(Hyde::getBasePath().DIRECTORY_SEPARATOR.'vendor/hyde/framework/foo', Hyde::vendorPath('foo'));
$this->assertSame(Hyde::getBasePath().'/vendor/hyde/framework', Hyde::vendorPath());
$this->assertSame(Hyde::getBasePath().'/vendor/hyde/framework/foo', Hyde::vendorPath('foo'));
}

public function test_copy_helper_copies_file_from_given_path_to_given_path()
Expand Down Expand Up @@ -263,7 +263,7 @@ public function test_fluent_model_source_path_helpers()
$this->assertSame(Hyde::path('_posts'), Hyde::getMarkdownPostPath());
$this->assertSame(Hyde::path('_docs'), Hyde::getDocumentationPagePath());
$this->assertSame(Hyde::path('_site'), Hyde::sitePath());
$this->assertSame(Hyde::path('_site'.DIRECTORY_SEPARATOR.'media'), Hyde::siteMediaPath());
$this->assertSame(Hyde::path('_site/media'), Hyde::siteMediaPath());
}

public function test_path_to_relative_helper_returns_relative_path_for_given_path()
Expand Down Expand Up @@ -394,20 +394,20 @@ public function test_get_media_output_directory_name_uses_trimmed_version_of_med

public function test_can_get_site_media_output_directory()
{
$this->assertSame(Hyde::path('_site'.DIRECTORY_SEPARATOR.'media'), Hyde::siteMediaPath());
$this->assertSame(Hyde::path('_site/media'), Hyde::siteMediaPath());
}

public function test_get_site_media_output_directory_uses_trimmed_version_of_media_source_directory()
{
Hyde::setMediaDirectory('_foo');
$this->assertSame(Hyde::path('_site'.DIRECTORY_SEPARATOR.'foo'), Hyde::siteMediaPath());
$this->assertSame(Hyde::path('_site/foo'), Hyde::siteMediaPath());
}

public function test_get_site_media_output_directory_uses_configured_site_output_directory()
{
Hyde::setOutputDirectory(Hyde::path('foo'));
Hyde::setMediaDirectory('bar');
$this->assertSame(Hyde::path('foo'.DIRECTORY_SEPARATOR.'bar'), Hyde::siteMediaPath());
$this->assertSame(Hyde::path('foo/bar'), Hyde::siteMediaPath());
}

public function test_media_output_directory_can_be_changed_in_configuration()
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/tests/Feature/HydeServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function test_provider_registers_blade_view_discovery_location_for_config

$this->provider->register();

$this->assertEquals([Hyde::path('_pages')], config('view.paths'));
$this->assertEquals([realpath(Hyde::path('_pages'))], config('view.paths'));
}

public function test_blade_view_locations_are_only_registered_once_per_key()
Expand All @@ -195,7 +195,7 @@ public function test_blade_view_locations_are_only_registered_once_per_key()
$this->provider->register();
$this->provider->register();

$this->assertEquals([Hyde::path('_pages')], config('view.paths'));
$this->assertEquals([realpath(Hyde::path('_pages'))], config('view.paths'));
}

public function test_provider_registers_console_commands()
Expand Down
8 changes: 4 additions & 4 deletions packages/framework/tests/Unit/HydePathHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public function test_string_is_returned()
public function test_returned_directory_contains_content_expected_to_be_in_the_project_directory()
{
$this->assertTrue(
file_exists(Hyde::path().DIRECTORY_SEPARATOR.'hyde') &&
file_exists(Hyde::path().DIRECTORY_SEPARATOR.'_pages') &&
file_exists(Hyde::path().DIRECTORY_SEPARATOR.'_posts') &&
file_exists(Hyde::path().DIRECTORY_SEPARATOR.'_site')
file_exists(Hyde::path('hyde')) &&
file_exists(Hyde::path('_pages')) &&
file_exists(Hyde::path('_posts')) &&
file_exists(Hyde::path('_site'))
);
}
}

0 comments on commit 78bc01d

Please sign in to comment.