public function testNormalizePath() { $this->assertSame('/foo', CM_File_Filesystem::normalizePath('/foo')); $this->assertSame('/foo', CM_File_Filesystem::normalizePath('/foo/')); $this->assertSame('/', CM_File_Filesystem::normalizePath('/')); $this->assertSame('/', CM_File_Filesystem::normalizePath('')); $this->assertSame('/', CM_File_Filesystem::normalizePath('//')); $this->assertSame('/foo/mega', CM_File_Filesystem::normalizePath('/foo/bar/../mega')); $this->assertSame('/', CM_File_Filesystem::normalizePath('/../..')); $this->assertSame('/foo/bar', CM_File_Filesystem::normalizePath('/foo/./bar')); $this->assertSame('/foo/bar', CM_File_Filesystem::normalizePath('/foo/./bar///')); $this->assertSame('/foo', CM_File_Filesystem::normalizePath('../foo')); $this->assertSame('/foo', CM_File_Filesystem::normalizePath('foo')); }
/** * @param string $pathAbsolute * @return string * @throws CM_Exception */ protected function _getRelativePath($pathAbsolute) { $pathAbsolute = CM_File_Filesystem::normalizePath($pathAbsolute); if (0 !== strpos($pathAbsolute, $this->_pathPrefix)) { throw new CM_Exception('Path is out of filesystem directory.', null, ['path' => $pathAbsolute]); } if ($pathAbsolute === $this->_pathPrefix) { return ''; } return ltrim(substr($pathAbsolute, strlen($this->_pathPrefix)), '/'); }
/** * @param string $path * @return static */ public function joinPath($path) { $path = implode('/', func_get_args()); $pathNew = CM_File_Filesystem::normalizePath($this->getPath() . '/' . $path); return new static($pathNew, $this->_filesystem); }
/** * @param string $pathPrefix * @param string[] $fileList * @param string[] $dirList * @param boolean $recursive * @throws CM_Exception */ private function _listByPrefix($pathPrefix, array &$fileList, array &$dirList, $recursive) { if ($this->_isLink($pathPrefix)) { return; } $pathPrefix = CM_File_Filesystem::normalizePath($pathPrefix); $pathPrefixAbsolute = $this->_getAbsolutePath($pathPrefix); $filenameList = @scandir($pathPrefixAbsolute); if (false === $filenameList) { throw new CM_Exception('Cannot scan directory', null, ['path' => $pathPrefixAbsolute]); } $filenameList = array_diff($filenameList, array('.', '..')); $fileListLocal = array(); foreach ($filenameList as $filename) { $path = ltrim($pathPrefix . '/' . $filename, '/'); $pathAbsolute = $pathPrefixAbsolute . '/' . $filename; if (is_dir($pathAbsolute)) { if ($recursive) { $this->_listByPrefix($path, $fileList, $dirList, true); } $dirList[] = $path; } else { $fileListLocal[] = $path; } } foreach ($fileListLocal as $filePath) { $fileList[] = $filePath; } }