Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,17 +576,11 @@ public function mirror(string $originDir, string $targetDir, ?\Traversable $iter
}

/**
* Returns whether the file path is an absolute path.
* Returns whether the given path is absolute.
*/
public function isAbsolutePath(string $file): bool
{
return '' !== $file && (strspn($file, '/\\', 0, 1)
|| (\strlen($file) > 3 && ctype_alpha($file[0])
&& ':' === $file[1]
&& strspn($file, '/\\', 2, 1)
)
|| null !== parse_url($file, \PHP_URL_SCHEME)
);
return Path::isAbsolute($file);
}

/**
Expand Down
40 changes: 10 additions & 30 deletions src/Symfony/Component/Filesystem/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,38 +358,18 @@ public static function changeExtension(string $path, string $extension): string
return substr($path, 0, -\strlen($actualExtension)).$extension;
}

/**
* Returns whether the given path is absolute.
*/
public static function isAbsolute(string $path): bool
{
if ('' === $path) {
return false;
}

// Strip scheme
if (false !== ($schemeSeparatorPosition = strpos($path, '://')) && 1 !== $schemeSeparatorPosition) {
$path = substr($path, $schemeSeparatorPosition + 3);
}

$firstCharacter = $path[0];

// UNIX root "/" or "\" (Windows style)
if ('/' === $firstCharacter || '\\' === $firstCharacter) {
return true;
}

// Windows root
if (\strlen($path) > 1 && ctype_alpha($firstCharacter) && ':' === $path[1]) {
// Special case: "C:"
if (2 === \strlen($path)) {
return true;
}

// Normal case: "C:/ or "C:\"
if ('/' === $path[2] || '\\' === $path[2]) {
return true;
}
}

return false;
return '' !== $path && (strspn($path, '/\\', 0, 1)
|| (\strlen($path) > 3 && ctype_alpha($path[0])
&& ':' === $path[1]
&& strspn($path, '/\\', 2, 1)
)
|| null !== parse_url($path, \PHP_URL_SCHEME)
);
}

public static function isRelative(string $path): bool
Expand Down
29 changes: 24 additions & 5 deletions src/Symfony/Component/Filesystem/Tests/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,31 +364,50 @@ public function testChangeExtension(string $path, string $extension, string $pat

public static function provideIsAbsolutePathTests(): \Generator
{
// UNIX-style absolute paths
yield ['/css/style.css', true];
yield ['/', true];
yield ['css/style.css', false];
yield ['', false];

// UNIX-style absolute paths with backslashes
yield ['\\css\\style.css', true];
yield ['\\', true];
yield ['css\\style.css', false];

// Windows-style absolute paths
yield ['C:/css/style.css', true];
yield ['D:/', true];
yield ['C:///windows', true];
yield ['C://test', true];

// Windows-style absolute paths with backslashes
yield ['E:\\css\\style.css', true];
yield ['F:\\', true];

// Windows special case (drive only)
yield ['C:', true];

// URLs and stream wrappers are considered absolute
yield ['phar:///css/style.css', true];
yield ['phar:///', true];
yield ['http://example.com', true];
yield ['ftp://user@server/path', true];
yield ['vfs://root/file.txt', true];

// "C:" without a slash is treated as a scheme by parse_url()
yield ['C:css/style.css', true];

// Relative paths
yield ['/var/lib', true];
yield ['c:\\\\var\\lib', true]; // c:\\var\lib
yield ['\\var\\lib', true];
yield ['var/lib', false];
yield ['../var/lib', false];
yield ['', false];

// Windows special case
yield ['C:', true];

// Not considered absolute
yield ['C:css/style.css', false];
// Empty path
yield ['', false];
}

/**
Expand Down
Loading