/** * testBasic method * * @return void */ public function testBasic() { $path = __DIR__; $Folder = new Folder($path); $result = $Folder->pwd(); $this->assertEquals($path, $result); $result = Folder::addPathElement($path, 'test'); $expected = $path . DIRECTORY_SEPARATOR . 'test'; $this->assertEquals($expected, $result); $result = $Folder->cd(TEST_ROOT); $expected = TEST_ROOT; $this->assertEquals($expected, $result); $result = $Folder->cd(TEST_ROOT . DIRECTORY_SEPARATOR . 'non-existent'); $this->assertFalse($result); }
/** * Get the real path (taking ".." and such into account) * * @param string $path Path to resolve * @return string The resolved path */ public function realpath($path) { $path = str_replace('/', DIRECTORY_SEPARATOR, trim($path)); if (strpos($path, '..') === false) { if (!Folder::isAbsolute($path)) { $path = Folder::addPathElement($this->_path, $path); } return $path; } $parts = explode(DIRECTORY_SEPARATOR, $path); $newparts = []; $newpath = ''; if ($path[0] === DIRECTORY_SEPARATOR) { $newpath = DIRECTORY_SEPARATOR; } while (($part = array_shift($parts)) !== null) { if ($part === '.' || $part === '') { continue; } if ($part === '..') { if (!empty($newparts)) { array_pop($newparts); continue; } return false; } $newparts[] = $part; } $newpath .= implode(DIRECTORY_SEPARATOR, $newparts); return Folder::slashTerm($newpath); }