BUGFIX Director::fileExists() fails on windows with absolute paths (#2935) - thanks to ajshort!

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@64098 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-10-12 16:16:25 +00:00
parent 7f40b2b953
commit 97fed5ae57
2 changed files with 37 additions and 4 deletions

View File

@ -460,16 +460,27 @@ class Director {
static function getAbsURL($url) {
return Director::baseURL() . $url;
}
/**
* Returns true if a given path is absolute. Works under both *nix and windows
* systems
*
* @param string $path
* @return bool
*/
public static function is_absolute($path) {
if($path[0] == '/' || $path[0] == '\\') return true;
return preg_match('/^[a-zA-Z]:[\\\\\/]/', $path) == 1;
}
/**
* Given a filesystem reference relative to the site root, return the full file-system path.
*
* @param string $file
* @return string
*/
static function getAbsFile($file) {
if($file[0] == '/') return $file;
return Director::baseFolder() . '/' . $file;
public static function getAbsFile($file) {
return self::is_absolute($file) ? $file : Director::baseFolder() . '/' . $file;
}
/**

View File

@ -46,5 +46,27 @@ class DirectorTest extends SapphireTest {
$this->assertEquals(Director::protocolAndHost().BASE_URL.'/', Director::absoluteBaseURL(BASE_URL));
$this->assertEquals(Director::protocolAndHost().BASE_URL . '/subfolder/test', Director::absoluteURL('subfolder/test'));
}
/**
* Tests that {@link Director::is_absolute()} works under different environment types
*/
public function testIsAbsolute() {
$expected = array (
'C:/something' => true,
'd:\\' => true,
'e/' => false,
's:/directory' => true,
'/var/www' => true,
'\\Something' => true,
'something/c:' => false,
'folder' => false,
'a/c:/' => false
);
foreach($expected as $path => $result) {
$this->assertEquals(Director::is_absolute($path), $result, "Test result for $path");
}
}
}
?>