/** * @param string $adapterClassName * @param array $options * @param string|null $secondaryClassName * @param array|null $secondaryOptions * @return CM_File_Filesystem */ public function createFilesystem($adapterClassName, array $options, $secondaryClassName = null, array $secondaryOptions = null) { $adapter = $this->createAdapter($adapterClassName, $options); $filesystem = new CM_File_Filesystem($adapter); if (null !== $secondaryClassName && null !== $secondaryOptions) { $secondaryAdapter = $this->createAdapter($secondaryClassName, $secondaryOptions); $secondaryFilesystem = new CM_File_Filesystem($secondaryAdapter); $filesystem->addSecondary($secondaryFilesystem); } return $filesystem; }
public function testRestoreByCopyingOldVersion() { $this->_filesystem->write('bar', 'mega1'); sleep(1); $this->_filesystem->write('bar', 'mega2'); sleep(1); $this->_filesystem->delete('bar'); sleep(1); $this->_filesystem->write('bar', 'mega3'); sleep(1); $this->_filesystem->write('bar', 'mega4'); $versionList = $this->_restore->getVersions('bar'); /** @var DateTime[] $lastModifiedList */ $lastModifiedList = Functional\map($versionList, function (CMService_AwsS3Versioning_Response_Version $version) { return $version->getLastModified(); }); $this->_restore->restoreByCopyingOldVersion('bar', $lastModifiedList[0]->add(new DateInterval('PT1S'))); $this->assertCount(5, $this->_restore->getVersions('bar')); $this->assertSame('mega4', $this->_filesystem->read('bar')); $this->_restore->restoreByCopyingOldVersion('bar', $lastModifiedList[1]); $this->assertCount(6, $this->_restore->getVersions('bar')); $this->assertSame('mega3', $this->_filesystem->read('bar')); $this->_restore->restoreByCopyingOldVersion('bar', $lastModifiedList[2]); $this->assertCount(7, $this->_restore->getVersions('bar')); $this->assertSame(false, $this->_filesystem->exists('bar')); $this->_restore->restoreByCopyingOldVersion('bar', $lastModifiedList[4]); $this->assertCount(8, $this->_restore->getVersions('bar')); $this->assertSame('mega1', $this->_filesystem->read('bar')); $this->_restore->restoreByCopyingOldVersion('bar', $lastModifiedList[4]->sub(new DateInterval('PT1S'))); $this->assertCount(9, $this->_restore->getVersions('bar')); $this->assertSame(false, $this->_filesystem->exists('bar')); }
/** * @param CM_Comparable $other * @return boolean */ public function equals(CM_Comparable $other = null) { if (empty($other)) { return false; } /** @var CM_File $other */ $samePath = $this->getPath() === $other->getPath(); $sameFilesystemAdapter = $this->_filesystem->equals($other->_filesystem); return $samePath && $sameFilesystemAdapter; }
public function testEquals() { $dirTmp = CM_Bootloader::getInstance()->getDirTmp(); $filesystem1 = new CM_File_Filesystem(new CM_File_Filesystem_Adapter_Local($dirTmp)); $filesystem2 = new CM_File_Filesystem(new CM_File_Filesystem_Adapter_Local($dirTmp)); $filesystem3 = new CM_File_Filesystem(new CM_File_Filesystem_Adapter_Local($dirTmp)); $filesystemSecondary1 = new CM_File_Filesystem(new CM_File_Filesystem_Adapter_Local($dirTmp . 'sec1')); $filesystemSecondary2 = new CM_File_Filesystem(new CM_File_Filesystem_Adapter_Local($dirTmp . 'sec2')); $filesystemSecondary3 = new CM_File_Filesystem(new CM_File_Filesystem_Adapter_Local($dirTmp . 'sec1')); $this->assertTrue($filesystem1->equals($filesystem2)); $filesystem1->addSecondary($filesystemSecondary1); $this->assertFalse($filesystem1->equals($filesystem2)); $filesystem2->addSecondary($filesystemSecondary1); $this->assertTrue($filesystem1->equals($filesystem2)); $filesystem3->addSecondary($filesystemSecondary2); $this->assertFalse($filesystem1->equals($filesystem3)); $filesystem1->addSecondary($filesystemSecondary1); $filesystem2->addSecondary($filesystemSecondary3); $this->assertTrue($filesystem1->equals($filesystem2)); $filesystem1->addSecondary($filesystemSecondary1); $filesystem2->addSecondary($filesystemSecondary2); $this->assertFalse($filesystem1->equals($filesystem2)); }
/** * @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)), '/'); }
public function testListByPrefixDoNotFollowSymlinks() { $filesystem = new CM_File_Filesystem($this->_adapter); $file = new CM_File('foo/bar/foo', $filesystem); $file->ensureParentDirectory(); $file->write('hello'); symlink($filesystem->getAdapter()->getPathPrefix() . '/foo', $filesystem->getAdapter()->getPathPrefix() . '/link'); $this->assertSame(array('files' => array('foo/bar/foo'), 'dirs' => array('foo/bar', 'foo', 'link')), $this->_adapter->listByPrefix('')); }
/** * @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; } }
/** * @param CM_File_Filesystem $filesystem * @param int $limit * @param int $poolLimit * @return string[] */ protected function _getRandomFiles(CM_File_Filesystem $filesystem, $limit, $poolLimit) { $files = []; $directories = ['/']; do { $path = array_shift($directories); $entries = $filesystem->listByPrefix($path, true); $files = array_merge($files, $entries['files']); $directories = array_merge($directories, $entries['dirs']); shuffle($directories); } while (count($files) < $poolLimit && count($directories) > 0); shuffle($files); return array_slice($files, 0, $limit); }