├── tests ├── testData │ └── safsiLocalDiskTest │ │ └── 0 │ │ └── simple.txt ├── README ├── AllTests.php ├── AbstractSafsiTest.php └── SafsiLocalDiskTest.php ├── .gitignore ├── lib ├── safsi_exception_IoException.php ├── safsi_exception_NotImplementedException.php ├── safsi_exception_Exception.php ├── safsi_core_IListItem.php ├── safsi_core_IFile.php ├── safsi_localdisk_Util.php ├── safsi_core_ListItem.php ├── safsi_core_File.php ├── safsi_core_IFileSystem.php ├── safsi_core_Util.php ├── safsi_core_AbstractFileSystem.php └── safsi_localdisk_FileSystem.php ├── README └── LICENSE /tests/testData/safsiLocalDiskTest/0/simple.txt: -------------------------------------------------------------------------------- 1 | hello world. -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .buildpath 2 | .project 3 | .settings 4 | ._* 5 | build 6 | dist 7 | repos 8 | -------------------------------------------------------------------------------- /tests/README: -------------------------------------------------------------------------------- 1 | Testing with PHPUnit (http://www.phpunit.de/) 2 | 3 | From the tests/ directory: 4 | 5 | phpunit AllTests 6 | 7 | From any other directory: 8 | 9 | phpunit AllTests /path/to/AllTests.php 10 | phpunit AllTests tests/AllTests.php 11 | 12 | -------------------------------------------------------------------------------- /lib/safsi_exception_IoException.php: -------------------------------------------------------------------------------- 1 | 12 | -------------------------------------------------------------------------------- /lib/safsi_exception_NotImplementedException.php: -------------------------------------------------------------------------------- 1 | 12 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Safsi - A simple abstract file system interface for PHP 2 | http://code.google.com/p/safsi-php/ 3 | http://safsi-php.org/ 4 | 5 | 6 | Safsi is a simple abstract file system interface for PHP. The goal is to 7 | provide a simple interface for accessing files in a consistent way, 8 | whether dealing with file locally on disk or dealing with files that 9 | exist remotely. For example, the same code should work for data stored 10 | on a local disk, data stored on s3 and data stored on CloudFiles. 11 | -------------------------------------------------------------------------------- /lib/safsi_exception_Exception.php: -------------------------------------------------------------------------------- 1 | cause = $cause; 10 | } 11 | 12 | public function cause() { 13 | return $this->cause; 14 | } 15 | 16 | } 17 | 18 | require_once('safsi_exception_IoException.php'); 19 | require_once('safsi_exception_NotImplementedException.php'); 20 | 21 | ?> 22 | -------------------------------------------------------------------------------- /tests/AllTests.php: -------------------------------------------------------------------------------- 1 | addTestSuite($testClassName); 12 | } 13 | return $suite; 14 | } 15 | } 16 | ?> 17 | -------------------------------------------------------------------------------- /lib/safsi_core_IListItem.php: -------------------------------------------------------------------------------- 1 | 41 | -------------------------------------------------------------------------------- /lib/safsi_core_IFile.php: -------------------------------------------------------------------------------- 1 | 59 | -------------------------------------------------------------------------------- /lib/safsi_localdisk_Util.php: -------------------------------------------------------------------------------- 1 | read()) { 33 | if ( $entry == '.' || $entry == '..' ) { 34 | continue; 35 | } 36 | self::RM_DIR(safsi_core_Util::GENERATE_PATH_FROM_PARTS($directory, $entry)); 37 | } 38 | $dh->close(); 39 | return rmdir($directory); 40 | } else { 41 | return unlink($directory); 42 | } 43 | } 44 | 45 | } 46 | ?> 47 | -------------------------------------------------------------------------------- /lib/safsi_core_ListItem.php: -------------------------------------------------------------------------------- 1 | isDirectory = $isDirectory; 22 | $this->path = $path; 23 | $this->basename = basename($path); 24 | $this->dirname = dirname($path); 25 | if ( $this->dirname == '.' ) $this->dirname = ''; 26 | } 27 | 28 | /** 29 | * Is directory? 30 | * @return bool Is directory? 31 | */ 32 | public function isDirectory() { 33 | } 34 | 35 | /** 36 | * Base name 37 | * @return string base name 38 | */ 39 | public function basename() { 40 | return $this->basename; 41 | } 42 | 43 | /** 44 | * Directory name 45 | * @return string directory name 46 | */ 47 | public function dirname() { 48 | return $this->dirname; 49 | } 50 | 51 | /** 52 | * Path 53 | * @return string path 54 | */ 55 | public function path() { 56 | return $this->path; 57 | } 58 | 59 | } 60 | 61 | ?> 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010, Dragonfly Development Inc 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Dragonfly Development Inc nor the names of its 13 | contributors may be used to endorse or promote products derived from 14 | this software without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 | POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /tests/AbstractSafsiTest.php: -------------------------------------------------------------------------------- 1 | generatePathFromParts( 26 | sys_get_temp_dir(), 27 | $prefix . '-' . uniqid(rand(),TRUE) 28 | ); 29 | mkdir($tempDir); 30 | return $tempDir; 31 | } 32 | 33 | /** 34 | * Remove a directory 35 | * 36 | * Recursively remove a directory and all of its contents. 37 | */ 38 | protected function rmDir($directory) { 39 | return call_user_func(array('safsi_localdisk_Util', 'RM_DIR'), $directory); 40 | } 41 | 42 | protected function handleLingeringException($e = null) { 43 | 44 | if ( $e instanceof PHPUnit_Framework_AssertionFailedError ) { 45 | throw $e; 46 | } 47 | 48 | if ( $e !== null ) { 49 | $this->fail('Caught an unknown exception: ' . $e); 50 | } 51 | 52 | } 53 | 54 | } 55 | 56 | ?> 57 | -------------------------------------------------------------------------------- /lib/safsi_core_File.php: -------------------------------------------------------------------------------- 1 | path = $path; 24 | $this->data = $data; 25 | $this->contentType = $contentType; 26 | $this->contentLength = $contentLength; 27 | $this->checksum = $checksum; 28 | $this->basename = basename($path); 29 | $this->dirname = dirname($path); 30 | if ( $this->dirname == '.' ) $this->dirname = ''; 31 | } 32 | 33 | /** 34 | * Is directory? 35 | * @return bool Is directory? 36 | */ 37 | public function isDirectory() { 38 | } 39 | 40 | /** 41 | * Base name 42 | * @return string base name 43 | */ 44 | public function basename() { 45 | return $this->basename; 46 | } 47 | 48 | /** 49 | * Directory name 50 | * @return string directory name 51 | */ 52 | public function dirname() { 53 | return $this->dirname; 54 | } 55 | 56 | /** 57 | * Path 58 | * @return string path 59 | */ 60 | public function path() { 61 | return $this->path; 62 | } 63 | 64 | /** 65 | * Data 66 | * @return mixed data 67 | */ 68 | public function data() { 69 | return $this->data; 70 | } 71 | 72 | /** 73 | * Content Type 74 | * @return string Content Type 75 | */ 76 | public function contentType() { 77 | return $this->contentType; 78 | } 79 | 80 | /** 81 | * Content Length 82 | * @return string Content Length 83 | */ 84 | public function contentLength() { 85 | return $this->contentLength; 86 | } 87 | 88 | /** 89 | * Checksum 90 | * @return string Checksum 91 | */ 92 | public function checksum() { 93 | return $this->checksum; 94 | } 95 | 96 | } 97 | 98 | ?> 99 | -------------------------------------------------------------------------------- /lib/safsi_core_IFileSystem.php: -------------------------------------------------------------------------------- 1 | 109 | -------------------------------------------------------------------------------- /lib/safsi_core_Util.php: -------------------------------------------------------------------------------- 1 | 0) ) { 117 | $default = $fres; 118 | } 119 | finfo_close($finfo); 120 | } 121 | } else { 122 | // Default to standard PHP method. 123 | $default = mime_content_type($filename); 124 | } 125 | } catch (Exception $e) { 126 | // noop 127 | } 128 | return $default; 129 | } 130 | 131 | } 132 | ?> 133 | -------------------------------------------------------------------------------- /lib/safsi_core_AbstractFileSystem.php: -------------------------------------------------------------------------------- 1 | $listItem ) { 28 | if ( ! $this->isIgnored($listItem->basename()) ) { 29 | $listItemsOut[] = $listItem; 30 | } 31 | // Try not to use too much memory. 32 | unset($listItems[$listItemsIdx]); 33 | } 34 | return $listItemsOut; 35 | } 36 | 37 | /** 38 | * Is a file ignored? 39 | * @param string $name File name 40 | * @return bool Is ignored? 41 | */ 42 | protected function isIgnored($name) { 43 | if ( $this->filterRegex === null ) { 44 | // TODO This method should be configured somehow. 45 | $globalIgnores = '*.o *.lo *.la *.al .libs *.so *.so.[0-9]* *.a *.pyc *.pyo *.rej *~ #*# .#* .*.swp .DS_Store .svn'; 46 | $this->filterRegex = '%^(' . implode('|', explode(' ', preg_replace('|\*|s', '.*', preg_replace('|\.|s', '\.', $globalIgnores)))) . ')$%'; 47 | } 48 | return preg_match($this->filterRegex, $name); 49 | } 50 | 51 | /** 52 | * Normalize a path 53 | * 54 | * Useful for taking input from either a string path or an 55 | * object that has a path method. 56 | * @param mixed $path Path 57 | * @return string $path 58 | */ 59 | protected function normalizePath($path = null) { 60 | if ( $path === null ) return ''; 61 | return preg_replace('|^/+|', '', method_exists($path, 'path') ? $path->path() : $path); 62 | } 63 | 64 | /** 65 | * List items at path 66 | * @param mixed $path Directory 67 | * @return bool Exists 68 | */ 69 | final public function listItems($path = null) { 70 | return $this->filterExcludedListItems( 71 | $this->listItemsInternal($path, $this) 72 | ); 73 | } 74 | 75 | /** 76 | * List items at path 77 | * @param mixed $path Directory 78 | * @return bool Exists 79 | */ 80 | abstract protected function listItemsInternal($path = null); 81 | 82 | /** 83 | * Write data to path 84 | * @param mixed $path Path 85 | * @param mixed $data Data 86 | * @param string $contentType Content Type 87 | * @param string $contentLength Content Length 88 | * @param string $checksum Checksum 89 | * @return bool Success 90 | */ 91 | final public function write($path, $data, $contentType = null, $contentLength = null, $checksum = null) { 92 | list($contentType, $contentLength, $checksum) = safsi_core_Util::GET_META_FOR_DATA( 93 | $data, 94 | $contentType, 95 | $contentLength, 96 | $checksum 97 | ); 98 | return $this->writeInternal( 99 | $path, 100 | $data, 101 | $contentType, 102 | $contentLength, 103 | $checksum 104 | ); 105 | } 106 | 107 | /** 108 | * Write data to path 109 | * @param mixed $path Path 110 | * @param mixed $data Data 111 | * @param string $contentType Content Type 112 | * @param string $contentLength Content Length 113 | * @param string $checksum Checksum 114 | * @return bool Success 115 | */ 116 | abstract public function writeInternal($path, $data, $contentType = null, $contentLength = null, $checksum = null); 117 | 118 | /** 119 | * Write contents of source path to path 120 | * @param mixed $path Path 121 | * @param mixed $sourcePath Source path 122 | * @param string $contentType Content Type 123 | * @param string $contentLength Content Length 124 | * @param string $checksum Checksum 125 | * @return bool Success 126 | */ 127 | final public function writeFromPath($path, $sourcePath, $contentType = null, $contentLength = null, $checksum = null) { 128 | list($contentType, $contentLength, $checksum) = safsi_core_Util::GET_META_FOR_PATH( 129 | $sourcePath, 130 | $contentType, 131 | $contentLength, 132 | $checksum 133 | ); 134 | return $this->writeFromPathInternal( 135 | $path, 136 | $sourcePath, 137 | $contentType, 138 | $contentLength, 139 | $checksum 140 | ); 141 | } 142 | 143 | /** 144 | * Write contents of source path to path 145 | * @param mixed $path Path 146 | * @param mixed $sourcePath Source path 147 | * @param string $contentType Content Type 148 | * @param string $contentLength Content Length 149 | * @param string $checksum Checksum 150 | * @return bool Success 151 | */ 152 | abstract public function writeFromPathInternal($path, $sourcePath, $contentType = null, $contentLength = null, $checksum = null); 153 | 154 | /** 155 | * Construct a safsi_core_IListItem instance. 156 | * @param bool $isDirectory Is directory? 157 | * @param string $path Path 158 | * @return safsi_core_IListItem 159 | */ 160 | public function constructListItem($isDirectory, $path) { 161 | return new safsi_core_ListItem($isDirectory, $path); 162 | } 163 | 164 | /** 165 | * Construct a safsi_core_IFile instance. 166 | * @param string $path Path 167 | * @return safsi_core_IFile 168 | */ 169 | public function constructFile($path, $data = null, $contentType = null, $contentLength = null, $checksum = null) { 170 | return new safsi_core_File( 171 | $path, 172 | $data, 173 | $contentType, 174 | $contentLength, 175 | $checksum 176 | ); 177 | } 178 | 179 | } 180 | 181 | ?> 182 | -------------------------------------------------------------------------------- /lib/safsi_localdisk_FileSystem.php: -------------------------------------------------------------------------------- 1 | root = preg_replace('|/+$|', '', $root); 27 | } 28 | 29 | /** 30 | * Delete a directory. 31 | * @param mixed $path Directory path 32 | * @param bool $recursive Recursively delete directory? 33 | * @return bool Success 34 | */ 35 | public function deleteDirectory($path, $recursive = false) { 36 | $actualPath = $this->normalizeLocaldiskPath($path); 37 | if ( file_exists($actualPath) ) { 38 | if ( is_dir($actualPath) ) { 39 | safsi_localdisk_Util::RM_DIR($actualPath); 40 | } else { 41 | // TODO: Throw "not a directory" exception? 42 | return false; 43 | } 44 | } 45 | return NULL; 46 | } 47 | 48 | /* 49 | * Delete a file. 50 | * @param mixed $path File path 51 | * @return bool Success 52 | */ 53 | public function deleteFile($path) { 54 | $actualPath = $this->normalizeLocaldiskPath($path); 55 | if ( file_exists($actualPath) ) { 56 | return unlink($actualPath); 57 | } 58 | return NULL; 59 | } 60 | 61 | /** 62 | * Check to see if a path exists 63 | * @return bool Exists 64 | */ 65 | public function exists($path) { 66 | return file_exists($this->normalizeLocaldiskPath($path)); 67 | } 68 | 69 | /** 70 | * List items at path 71 | * @param mixed $path Directory 72 | * @return bool Exists 73 | */ 74 | protected function listItemsInternal($path = null) { 75 | $path = $this->normalizePath($path); 76 | $actualPath = $this->normalizeLocaldiskPath($path); 77 | $listItems = array(); 78 | if ( ! is_dir($actualPath) ) { return $listItems; } 79 | if ( $dirHandle = opendir($actualPath) ) { 80 | while (($file = readdir($dirHandle)) !== false) { 81 | if ( $file == '.' or $file == '..' ) continue; 82 | $actualFilePath = $path ? $actualPath . '/' . $file : $file; 83 | $filePath = $path ? $path . '/' . $file : $file; 84 | $listItems[] = $this->constructListItem( 85 | is_dir($actualFilePath), 86 | $filePath 87 | ); 88 | } 89 | closedir($dirHandle); 90 | } 91 | return $listItems; 92 | } 93 | 94 | /** 95 | * Read data from path 96 | * @param mixed $path Path 97 | * @return safsi_core_IFile File 98 | */ 99 | public function read($path) { 100 | $actualPath = $this->normalizeLocaldiskPath($path); 101 | list($contentType, $contentLength, $checksum) = $this->getMeta($path); 102 | return $this->constructFile( 103 | $path, 104 | file_get_contents($actualPath), 105 | $contentType, 106 | $contentLength, 107 | $checksum 108 | ); 109 | } 110 | 111 | /** 112 | * Read data from path 113 | * @param mixed $path Path 114 | * @param mixed $resource Target 115 | * @return safsi_core_IFile File 116 | */ 117 | public function readStream($path, $resource) { 118 | $actualPath = $this->normalizeLocaldiskPath($path); 119 | list($contentType, $contentLength, $checksum) = $this->getMeta($path); 120 | $source = fopen($actualPath, 'rb'); 121 | while (!feof($source)) { 122 | fwrite($resource, fread($source, 1024)); 123 | } 124 | fclose($source); 125 | return $this->constructFile( 126 | $path, 127 | null, 128 | $contentType, 129 | $contentLength, 130 | $checksum 131 | ); 132 | } 133 | 134 | /** 135 | * Write data to path 136 | * @param mixed $path Path 137 | * @param mixed $data Data 138 | * @param string $contentType Content Type 139 | * @param string $contentLength Content Length 140 | * @param string $checksum Checksum 141 | * @return bool Success 142 | */ 143 | public function writeInternal($path, $data, $contentType = null, $contentLength = null, $checksum = null) { 144 | $this->mkdirs($path); 145 | $actualPath = $this->normalizeLocaldiskPath($path); 146 | return file_put_contents($actualPath, $data); 147 | } 148 | 149 | /** 150 | * Write stream to path 151 | * @param mixed $path Path 152 | * @param mixed $data Data 153 | * @param string $contentType Content Type 154 | * @param string $contentLength Content Length 155 | * @param string $checksum Checksum 156 | * @return bool Success 157 | */ 158 | public function writeStream($path, $resource, $contentType = null, $contentLength = null, $checksum = null) { 159 | $this->mkdirs($path); 160 | $actualPath = $this->normalizeLocaldiskPath($path); 161 | $out = fopen($actualPath, 'wb'); 162 | while (!feof($resource)) { 163 | fwrite($out, fread($resource, 1024)); 164 | } 165 | fclose($out); 166 | return true; 167 | } 168 | 169 | /** 170 | * Write contents of source path to path 171 | * @param mixed $path Path 172 | * @param mixed $sourcePath Source path 173 | * @param string $contentType Content Type 174 | * @param string $contentLength Content Length 175 | * @param string $checksum Checksum 176 | * @return bool Success 177 | */ 178 | public function writeFromPathInternal($path, $sourcePath, $contentType = null, $contentLength = null, $checksum = null) { 179 | $this->mkdirs($path); 180 | $actualPath = $this->normalizeLocaldiskPath($path); 181 | return copy($sourcePath, $actualPath); 182 | } 183 | 184 | /** 185 | * Normalize a localdisk path 186 | * @param string $path Path 187 | * @return string Path 188 | */ 189 | protected function normalizeLocaldiskPath($path) { 190 | return $this->root . '/' . $this->normalizePath($path); 191 | } 192 | 193 | /** 194 | * Get meta data for a path 195 | * @param string $path Path 196 | * @return array Array containing content type, content length and checksum 197 | */ 198 | protected function getMeta($path) { 199 | return safsi_core_Util::GET_META_FOR_PATH( 200 | $this->normalizeLocaldiskPath($path) 201 | ); 202 | } 203 | 204 | /** 205 | * Make directories up to the path 206 | * @param string $path Path 207 | */ 208 | protected function mkdirs($path) { 209 | 210 | $dir = dirname($this->normalizeLocaldiskPath($path)); 211 | 212 | $pathSoFar = ''; 213 | 214 | foreach ( explode('/', $dir) as $part ) { 215 | 216 | // Skip "empty" directory parts. 217 | if ( ! strlen($part) ) continue; 218 | 219 | // Include this part on our path. 220 | $pathSoFar .= '/' . $part; 221 | 222 | // Skip "existing" directory parts. 223 | if ( file_exists($pathSoFar) ) continue; 224 | 225 | // Try to create the directory. 226 | if ( ! mkdir($pathSoFar) ) return false; 227 | 228 | } 229 | 230 | // Assume it is all good here. 231 | return true; 232 | 233 | } 234 | 235 | } 236 | 237 | ?> 238 | -------------------------------------------------------------------------------- /tests/SafsiLocalDiskTest.php: -------------------------------------------------------------------------------- 1 | generatePathFromParts( 21 | dirname(__FILE__), 22 | 'testData/safsiLocalDiskTest/0' 23 | ); 24 | 25 | $data = 'hello world.'; 26 | 27 | // md5sum of 'hello world.' 28 | $checksum = '3c4292ae95be58e0c58e4e5511f09647'; 29 | $contentLength = 12; 30 | 31 | $fs = new safsi_localdisk_FileSystem($root); 32 | 33 | $testFilename = 'simple.txt'; 34 | 35 | $this->assertTrue( 36 | $fs->exists($testFilename), 37 | 'Filename does not exist' 38 | ); 39 | 40 | $file = $fs->read($testFilename); 41 | 42 | $this->assertEquals($checksum, $file->checksum(), 'Incorrect checksum'); 43 | 44 | $this->assertEquals($data, $file->data(), 'Incorrect data'); 45 | 46 | $this->assertEquals( 47 | $contentLength, 48 | $file->contentLength(), 49 | 'Incorrect content length' 50 | ); 51 | 52 | } 53 | 54 | /** 55 | * Test listing items. 56 | */ 57 | public function testList() { 58 | 59 | $root = $this->generatePathFromParts( 60 | dirname(__FILE__), 61 | 'testData/safsiLocalDiskTest/0' 62 | ); 63 | 64 | $fs = new safsi_localdisk_FileSystem($root); 65 | 66 | $listItems = $fs->listItems(); 67 | 68 | $this->assertEquals(1, count($listItems), 'Incorrect number of items'); 69 | 70 | $listItem = $listItems[0]; 71 | 72 | $testFilename = 'simple.txt'; 73 | 74 | $this->assertEquals( 75 | $testFilename, 76 | $listItem->basename(), 77 | 'Incorrect file name' 78 | ); 79 | 80 | } 81 | 82 | /** 83 | * Test reading to a resource stream. 84 | */ 85 | public function testReadToStream() { 86 | 87 | $root = $this->generatePathFromParts( 88 | dirname(__FILE__), 89 | 'testData/safsiLocalDiskTest/0' 90 | ); 91 | 92 | $testFilename = 'simple.txt'; 93 | $data = 'hello world.'; 94 | 95 | $fs = new safsi_localdisk_FileSystem($root); 96 | 97 | $tmp = tmpfile(); 98 | $file = $fs->readStream($testFilename, $tmp); 99 | fseek($tmp, 0); 100 | $tmpData = fread($tmp, 1024); 101 | fclose($tmp); 102 | 103 | $this->assertEquals(null, $file->data(), 'Incorrect data'); 104 | $this->assertEquals($data, $tmpData, 'Incorrect data'); 105 | 106 | } 107 | 108 | /** 109 | * Test writing 110 | */ 111 | public function testWrite() { 112 | 113 | $tempDir = $this->mkTempDir(); 114 | 115 | $data = 'hello world.'; 116 | $checksum = '3c4292ae95be58e0c58e4e5511f09647'; 117 | $contentLength = 12; 118 | 119 | $testFilename = 'deep/path/write-test.txt'; 120 | 121 | $fs = new safsi_localdisk_FileSystem($tempDir); 122 | 123 | $fs->write($testFilename, $data); 124 | 125 | $file = $fs->read($testFilename); 126 | 127 | $this->assertEquals($checksum, $file->checksum(), 'Incorrect checksum'); 128 | 129 | $this->assertEquals($data, $file->data(), 'Incorrect data'); 130 | $this->assertEquals( 131 | $contentLength, 132 | $file->contentLength(), 133 | 'Incorrect content length' 134 | ); 135 | 136 | $this->rmDir($tempDir); 137 | 138 | } 139 | 140 | /** 141 | * Test stream writing 142 | */ 143 | public function testWriteStream() { 144 | 145 | $tempDir = $this->mkTempDir(); 146 | 147 | $data = 'hello world.'; 148 | $checksum = '3c4292ae95be58e0c58e4e5511f09647'; 149 | $contentLength = 12; 150 | 151 | $testFilename = 'deep/path/write-test.txt'; 152 | 153 | $source = $this->generatePathFromParts( 154 | dirname(__FILE__), 155 | 'testData/safsiLocalDiskTest/0/simple.txt' 156 | ); 157 | 158 | $stream = fopen($source, 'rb'); 159 | 160 | $fs = new safsi_localdisk_FileSystem($tempDir); 161 | 162 | $fs->writeStream($testFilename, $stream); 163 | 164 | fclose($stream); 165 | 166 | $file = $fs->read($testFilename); 167 | 168 | $this->assertEquals($checksum, $file->checksum(), 'Incorrect checksum'); 169 | 170 | $this->assertEquals($data, $file->data(), 'Incorrect data'); 171 | $this->assertEquals( 172 | $contentLength, 173 | $file->contentLength(), 174 | 'Incorrect content length' 175 | ); 176 | 177 | $this->rmDir($tempDir); 178 | 179 | } 180 | 181 | /** 182 | * Test path writing 183 | */ 184 | public function testWriteFromPath() { 185 | 186 | $tempDir = $this->mkTempDir(); 187 | 188 | $data = 'hello world.'; 189 | $checksum = '3c4292ae95be58e0c58e4e5511f09647'; 190 | $contentLength = 12; 191 | 192 | $testFilename = 'deep/path/write-test.txt'; 193 | 194 | $source = $this->generatePathFromParts( 195 | dirname(__FILE__), 196 | 'testData/safsiLocalDiskTest/0/simple.txt' 197 | ); 198 | 199 | $fs = new safsi_localdisk_FileSystem($tempDir); 200 | 201 | $fs->writeFromPath($testFilename, $source); 202 | 203 | $file = $fs->read($testFilename); 204 | 205 | $this->assertEquals($checksum, $file->checksum(), 'Incorrect checksum'); 206 | 207 | $this->assertEquals($data, $file->data(), 'Incorrect data'); 208 | $this->assertEquals( 209 | $contentLength, 210 | $file->contentLength(), 211 | 'Incorrect content length' 212 | ); 213 | 214 | $this->rmDir($tempDir); 215 | 216 | } 217 | 218 | /** 219 | * Test delete file 220 | */ 221 | public function testDeleteFile() { 222 | 223 | $tempDir = $this->mkTempDir(); 224 | 225 | $testFilename = 'deep/path/write-test.txt'; 226 | 227 | $source = $this->generatePathFromParts( 228 | dirname(__FILE__), 229 | 'testData/safsiLocalDiskTest/0/simple.txt' 230 | ); 231 | 232 | $fs = new safsi_localdisk_FileSystem($tempDir); 233 | 234 | $fs->writeFromPath($testFilename, $source); 235 | 236 | $this->assertTrue( 237 | $fs->exists($testFilename), 238 | 'Filename does not exist' 239 | ); 240 | 241 | $fs->deleteFile($testFilename); 242 | 243 | $this->assertFalse( 244 | $fs->exists($testFilename), 245 | 'Filename exists' 246 | ); 247 | 248 | $this->rmDir($tempDir); 249 | 250 | } 251 | 252 | 253 | /** 254 | * Test delete directory 255 | */ 256 | public function testDeleteDirectory() { 257 | 258 | $tempDir = $this->mkTempDir(); 259 | 260 | $testFilename = 'deep/path/write-test.txt'; 261 | 262 | $source = $this->generatePathFromParts( 263 | dirname(__FILE__), 264 | 'testData/safsiLocalDiskTest/0/simple.txt' 265 | ); 266 | 267 | $fs = new safsi_localdisk_FileSystem($tempDir); 268 | 269 | $fs->writeFromPath($testFilename, $source); 270 | 271 | $this->assertTrue( 272 | $fs->exists($testFilename), 273 | 'Filename does not exist' 274 | ); 275 | 276 | $fs->deleteDirectory('deep'); 277 | 278 | $this->assertFalse( 279 | $fs->exists($testFilename), 280 | 'Filename exists' 281 | ); 282 | 283 | $this->rmDir($tempDir); 284 | 285 | } 286 | 287 | 288 | } 289 | 290 | ?> 291 | --------------------------------------------------------------------------------