Skip to content

Commit

Permalink
added FileSystem::unixSlashes() & platformSlashes()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 2, 2023
1 parent d21685e commit f730f7f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Utils/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,24 @@ public static function joinPaths(string ...$paths): string
{
return self::normalizePath(implode('/', $paths));
}


/**
* Converts backslashes to slashes.
*/
public static function unixSlashes(string $path): string
{
return strtr($path, '\\', '/');
}


/**
* Converts slashes to platform-specific directory separators.
*/
public static function platformSlashes(string $path): string
{
return DIRECTORY_SEPARATOR === '/'
? strtr($path, '\\', '/')
: str_replace(':\\\\', '://', strtr($path, '/', '\\')); // protocol://
}
}
18 changes: 18 additions & 0 deletions tests/Utils/FileSystem.platformSlashes.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Nette\Utils\FileSystem;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


test('', function () {
Assert::same(DIRECTORY_SEPARATOR, FileSystem::platformSlashes('\\'));
Assert::same(DIRECTORY_SEPARATOR, FileSystem::platformSlashes('/'));
});

test('protocol', function () {
Assert::same('file://path' . DIRECTORY_SEPARATOR . 'file', FileSystem::platformSlashes('file://path/file'));
});
14 changes: 14 additions & 0 deletions tests/Utils/FileSystem.unixSlashes.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Nette\Utils\FileSystem;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


test('', function () {
Assert::same('/', FileSystem::unixSlashes('\\'));
Assert::same('/', FileSystem::unixSlashes('/'));
});

0 comments on commit f730f7f

Please sign in to comment.