Skip to content

Commit

Permalink
Improve support of relative path (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Feb 8, 2024
1 parent 0d14e06 commit e096ea2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/File/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public static function removeDot(string $fileName): string
* @param array<string> $ignoredDir
*/
public static function getFileName(
string $absolutePath,
string $path,
?string $baseDir = null,
array $ignoredDir = []
): ?string {
$split = self::splitPath($absolutePath, $baseDir, $ignoredDir);
$split = self::splitPath($path, $baseDir, $ignoredDir);
if ([] === $split) {
return null;
}
Expand All @@ -37,11 +37,11 @@ public static function getFileName(
* @return list<string>
*/
public static function getDirectories(
string $absolutePath,
string $path,
?string $baseDir = null,
array $ignoredDir = []
): array {
$split = self::splitPath($absolutePath, $baseDir, $ignoredDir);
$split = self::splitPath($path, $baseDir, $ignoredDir);
array_pop($split);

return $split;
Expand All @@ -53,24 +53,24 @@ public static function getDirectories(
* @return list<string>
*/
private static function splitPath(
string $absolutePath,
string $path,
?string $baseDir = null,
array $ignoredDir = []
): array {
$baseDir = trim($baseDir ?? '', \DIRECTORY_SEPARATOR);

if ('' === $baseDir) {
if ('' === $baseDir || str_starts_with($path, $baseDir.\DIRECTORY_SEPARATOR)) {
$baseDirPosition = 0;
$baseDir = \DIRECTORY_SEPARATOR;
$baseDir .= \DIRECTORY_SEPARATOR;
} else {
$baseDir = \DIRECTORY_SEPARATOR.$baseDir.\DIRECTORY_SEPARATOR;
$baseDirPosition = strrpos($absolutePath, $baseDir);
$baseDirPosition = strrpos($path, $baseDir);
if (false === $baseDirPosition) {
return [];
}
}

$path = substr($absolutePath, $baseDirPosition + \strlen($baseDir));
$path = substr($path, $baseDirPosition + \strlen($baseDir));
foreach ($ignoredDir as $ignoredDirectory) {
$ignoredDirectory = trim($ignoredDirectory, \DIRECTORY_SEPARATOR).\DIRECTORY_SEPARATOR;
if (str_starts_with($path, $ignoredDirectory)) {
Expand Down
18 changes: 18 additions & 0 deletions tests/File/FileHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,23 @@ public static function getDirectoriesDataProvider(): iterable
[],
['foo_directory'],
];
yield [
'./foo_directory/directory/file.twig',
'foo_directory',
[],
['directory'],
];
yield [
'foo_directory/directory/file.twig',
'foo_directory',
[],
['directory'],
];
yield [
'foo_directory/directory/file.twig',
'foo',
[],
[],
];
}
}

0 comments on commit e096ea2

Please sign in to comment.