├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── examples ├── ExampleDir │ ├── ExampleChildDir │ │ └── childFile.txt │ ├── file1.txt │ └── file2.txt ├── FileExample.php ├── FileReaderExample.php ├── FileWriterExample.php ├── LockManagerExample.php ├── content.txt ├── example.txt ├── example_read.txt ├── example_write.txt ├── index.html ├── lock │ └── .gitignore └── tmp │ └── .gitignore ├── phpunit.xml.dist ├── src ├── Exception │ ├── FileException.php │ ├── FileFilterException.php │ ├── FileReaderException.php │ ├── FileWriterException.php │ ├── LockException.php │ ├── LockHandlerException.php │ └── LockManagerException.php ├── File.php ├── FileAbstract.php ├── FileFilterInterface.php ├── FileFilterType.php ├── FileInfoInterface.php ├── FileInterface.php ├── FileReader.php ├── FileReaderAbstract.php ├── FileReaderInterface.php ├── FileWriter.php ├── FileWriterInterface.php ├── Lock.php ├── LockHandler.php ├── LockHandlerInterface.php ├── LockInterface.php └── LockManager.php └── tests ├── ExampleDir ├── ExampleChildDir │ └── .gitkeep ├── testFile1.txt └── testFile2.txt ├── FileReaderTest.php ├── FileTest.php ├── FileWriterTest.php ├── LockTest.php ├── example.txt ├── example_read.txt ├── example_write1.txt ├── example_write2.txt ├── lock └── .gitignore └── tmp └── .gitignore /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea 3 | vendor 4 | composer.phar 5 | composer.lock 6 | phpunit.xml -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | - 7.0 8 | - 7.1 9 | 10 | matrix: 11 | include: 12 | - php: 5.3 13 | dist: precise 14 | 15 | before_install: 16 | - composer --prefer-source install 17 | 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Sven Sanzenbacher 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # naucon File Package 2 | 3 | ## About 4 | 5 | This package contains php classes to access, change, copy, delete, move, rename files and directories. 6 | 7 | 8 | ### Features 9 | 10 | * File 11 | * Extending SplFileInfo 12 | * change Privileges 13 | * create file or directories 14 | * copy files and directories 15 | * move files and directories 16 | * delete files and directories (recursive) 17 | * iterate files an directories 18 | * FileReader 19 | * read file content 20 | * iterate lines 21 | * FileWriter 22 | * write file content 23 | * write lines to file 24 | * remove file content (clear) 25 | * truncat file content 26 | * Lock (file lock mechanism) 27 | 28 | 29 | ### Compatibility 30 | 31 | * PHP5.3 32 | 33 | 34 | ## Installation 35 | 36 | install the latest version via composer 37 | 38 | composer require naucon/file 39 | 40 | 41 | ## Basic Usage 42 | 43 | ### File 44 | 45 | The `File` class provides methods to access and change file attributes as well as basic file operations like copy, delete, move, rename to a given absolute file or directory path. 46 | Since PHP 5.1.2 the Standard PHP Library (SPL) contains a class `SplFileInfo` that can access file attributes but do not change or perform any basic file operations. 47 | The `File` class inherit from the `SplFileInfo` class to ensure compatibility. 48 | 49 | Create a instance of `File` class with a absolute file path string. 50 | 51 | $examplePath = __DIR__ . '/example.txt'; 52 | 53 | use Naucon\File\File; 54 | $fileObject = new File($examplePath); 55 | 56 | Example: 57 | 58 | echo 'File ' . $fileObject->getPathname() . ' do' . (($fileObject->exists()) ? '' : ' not') . ' exist.'; 59 | echo '
'; 60 | echo 'File is' . (($fileObject->isReadable()) ? '' : ' not') . ' readable.'; 61 | echo '
'; 62 | echo 'File is' . (($fileObject->isWritable()) ? '' : ' not') . ' writeable.'; 63 | echo '
'; 64 | echo '
'; 65 | 66 | echo 'File size: ' . $fileObject->getSize() . ' bytes'; 67 | echo '
'; 68 | echo 'Access Time: ' . $fileObject->lastAccessed()->format('d.m.Y H:s'); 69 | echo '
'; 70 | echo 'Change Time: ' . $fileObject->lastChanged()->format('d.m.Y H:s'); 71 | echo '
'; 72 | echo 'Modification Time: ' . $fileObject->lastModified()->format('d.m.Y H:s'); 73 | echo '
'; 74 | echo '
'; 75 | 76 | echo 'File Owner: ' . $fileObject->getOwnerName() . ' (' . $fileObject->getOwner() . ')'; 77 | echo '
'; 78 | echo 'File Group: ' . $fileObject->getGroupName() . ' (' . $fileObject->getGroup() . ')'; 79 | echo '
'; 80 | echo 'File permission: ' . $fileObject->getPermission(); 81 | echo '
'; 82 | echo '
'; 83 | 84 | 85 | #### create directory 86 | 87 | To create a directory, make a instance of `File` with a absolute path of the new directory and call the method `mkdir()` or `mkdirs()`. 88 | 89 | $newDirectoryPath = __DIR__ . '/tmp'; 90 | 91 | use Naucon\File\File; 92 | $fileObject = new File($newDirectoryPath); 93 | $fileObject->mkdir(); 94 | 95 | The method `mkdirs()` will not only create the given directory instead it create every directories of the path recursive. 96 | 97 | $newDirectoryPath = __DIR__ . '/tmp/foo/bar'; 98 | 99 | use Naucon\File\File; 100 | $fileObject = new File($newDirectoryPath); 101 | $fileObject->mkdirs(); 102 | 103 | 104 | #### rename 105 | 106 | To rename a file or directory, first create a instance of `File` with the absolute path of the file or directory. 107 | Afterward call the method `rename()` with the new file or directory name (file with extension). 108 | 109 | $fileObject->rename('example_foo.txt'); 110 | 111 | 112 | #### copy 113 | 114 | To copy a file or directory, first create a instance of `File` with the absolute path of the source file or directory. 115 | Afterward call the method `copy()` with a absolute path of the target directory. 116 | 117 | $sourcePath = __DIR__ . '/example.txt'; 118 | $targetPath = __DIR__ . '/tmp/target/'; 119 | 120 | use Naucon\File\File; 121 | $fileObject = new File($sourcePath); 122 | $fileObject->copy($targetPath); 123 | 124 | 125 | #### move 126 | 127 | To move a file or directory, first create a instance of `File` with the absolute path of the source file or directory. 128 | Afterward call the method `move()` with a absolute path of the target directory. 129 | 130 | $sourcePath = __DIR__ . '/example.txt'; 131 | $targetPath = __DIR__ . '/tmp/target/move/'; 132 | 133 | use Naucon\File\File; 134 | $fileObject = new File($sourcePath); 135 | $fileObject->move($targetPath); 136 | 137 | 138 | #### delete 139 | 140 | To delete a file or directory, first create a instance of `File` with the absolute path of the file or directory. 141 | Afterward call the method `delete()` or `deleteAll()`. 142 | 143 | $sourcePath = __DIR__ . '/tmp/example.txt'; 144 | 145 | use Naucon\File\File; 146 | $fileObject = new File($sourcePath); 147 | $fileObject->delete(); 148 | 149 | The method `delete()` will delete the file or directory. It can only remove empty directories. 150 | To delete a directory recursive with its files and sub directories call `deleteAll()`. 151 | 152 | $sourcePath = __DIR__ . '/tmp/'; 153 | 154 | use Naucon\File\File; 155 | $fileObject = new File($sourcePath); 156 | $fileObject->deleteAll(); 157 | 158 | 159 | #### iterate 160 | 161 | The `File` class provides the methods `listAll()` and `listFiles()` to access the files an directories of a given directory. 162 | The methods return a instance of `FilesystemIterator` class (SPL). The instance can be iterated with the `foreach()` command to retrieve the files and directories. 163 | 164 | $path = __DIR__ . '/ExampleDir'; 165 | 166 | use Naucon\File\File; 167 | $fileObject = new File($path); 168 | $iteratorObject = $fileObject->listAll(); 169 | 170 | foreach ($iteratorObject as $subFileObject) { 171 | $subFileObject->getBasename() . '
'; 172 | if ($subFileObject->isDir()) { 173 | foreach ($subFileObject->listAll() as $subChildFileObject) { 174 | echo $subChildFileObject->getBasename() . '
'; 175 | } 176 | } 177 | } 178 | 179 | The method `listFiles()` filters the result to file only. There for it implements the `FileFilterType` filter class. 180 | 181 | $iteratorObject = $fileObject->listFiles(); 182 | 183 | foreach ($iteratorObject as $subFileObject) { 184 | echo $subFileObject->getBasename() . '
'; 185 | } 186 | 187 | 188 | To filter the result use a implementation of `FilterIterator`. The package contains already the filter class `FileFilterType` to filter after the file type (dir|file. 189 | 190 | $iterator = new FileFilterType($fileObject->listAll(), 'dir'); 191 | foreach ($iteratorObject as $subFileObject) { 192 | echo $subFileObject->getBasename() . '
'; 193 | } 194 | 195 | ### FileReader 196 | 197 | The `FileReader` class extends the `File` class to read the content of a given file in different ways. 198 | 199 | Since PHP 5.1 the Standard PHP Library (SPL) contains a class `SplFileObject` that can access and change file content. 200 | The `FileReader` class nowadays use a instance of `SplFileObject` to perform read operations but do not inherit from it and is also not compatible. 201 | 202 | Create a instance of `FileReader` class with a absolute file path string. 203 | 204 | $filePath = __DIR__ . '/example_read.txt'; 205 | 206 | use Naucon\File\FileReader; 207 | $fileObject = new FileReader($filePath, 'r', true); 208 | 209 | 210 | #### Iterate lines 211 | 212 | The `FileReader` class implements the iterater interface. The instance can be iterated with the `foreach()` command to retrieve lines of file content. 213 | 214 | // iterate 215 | foreach($fileObject as $line) { 216 | echo $line . '
'; 217 | } 218 | 219 | To navigate between line the following methods are provided `isFirst()`, `firstLine()`, `isLast()`, `nextLine()`, `readLine($pointer)` 220 | 221 | // while 222 | echo $fileObject->firstLine(); 223 | echo '
'; 224 | while ( !$fileObject->isLast() ){ 225 | echo $fileObject->nextLine(); 226 | echo '
'; 227 | } 228 | 229 | echo $fileObject->firstLine(); 230 | echo '
'; 231 | echo $fileObject->nextLine(); 232 | echo '
'; 233 | echo $fileObject->nextLine(); 234 | echo '
'; 235 | echo $fileObject->firstLine(); 236 | echo '
'; 237 | 238 | echo $fileObject->readLine(3); 239 | echo '
'; 240 | 241 | echo $fileObject->readLine(7); 242 | echo '
'; 243 | 244 | When calling `read()` the file content is returned at once. 245 | 246 | // read all 247 | echo nl2br($fileObject->read()); 248 | echo '
'; 249 | 250 | When calling `readLines()` the lines of file content are returned in a array at once. 251 | 252 | $lines = $fileObject->readLines(); // return array 253 | foreach ($lines as $line) { 254 | echo $line . '
'; 255 | } 256 | 257 | 258 | ### FileWriter 259 | 260 | The `FileWriter` class extends the `FileReader` class to write content of a given file in different ways. 261 | 262 | Since PHP 5.1 the Standard PHP Library (SPL) contains a class `SplFileObject` that can access and change file content. 263 | The `FileWriter` class nowadays use a instance of `SplFileObject` to perform read operations but do not inherit from it and is also not compatible. 264 | 265 | Create a instance of `FileWriter` class with a absolute file path string. 266 | 267 | $filePath = __DIR__ . '/example_write.txt'; 268 | 269 | use Naucon\File\FileWriter; 270 | $fileObject = new FileWriter($filePath,'w+'); // file point at the beginning of the file, truncate existing content 271 | 272 | Afterwards call `write($sting)` or `writeLine($string)` to write a given string to the file. 273 | 274 | $string = 'Line01'.PHP_EOL; 275 | $string.= 'Line02'.PHP_EOL; 276 | $string.= 'Line03'.PHP_EOL; 277 | $string.= 'Line04'.PHP_EOL; 278 | 279 | $fileObject->write($string); 280 | 281 | // iterate file lines 282 | foreach($fileObject as $line) { 283 | echo $line . '
'; 284 | } 285 | echo '
'; 286 | 287 | //Output: 288 | //Line01 289 | //Line02 290 | //Line03 291 | //Line04 292 | 293 | The method `writeLine($string)` will add a line break a the given string. 294 | 295 | $filePath = __DIR__ . '/example_write.txt'; 296 | $fileObject = new FileWriter($filePath,'a+'); // file point at the end of the file 297 | $fileObject->writeLine("foo"); 298 | $fileObject->writeLine("bar"); 299 | 300 | foreach($fileObject as $line) { 301 | echo $line . '
'; 302 | } 303 | echo '
'; 304 | 305 | //Output: 306 | //Line01 307 | //Line02 308 | //Line03 309 | //Line04 310 | //foo 311 | //bar 312 | 313 | When calling `clear()` the file content will be removed 314 | 315 | $fileObject->clear(); // remove all content 316 | 317 | 318 | ### Lock 319 | 320 | The `Lock` classes is a mechanism to lock processes by writing a lock file at the beginning of the process and deleting it at the end of the process. 321 | When the process is executed and the lock file already exists it will aborted. Thereby multiple processes can not be executed at the same time. 322 | You may have seen it when opening word document on windows. 323 | 324 | First create a instance of `LockHandler` class with a absolute path where the lock file are written. 325 | Afterward create a instance of `LockManager` class with the LockHandler` instance. The `LockManager` implements the singleton patter and is there for accessible everywhere. 326 | 327 | $lockPath = __DIR__ . '/lock/'; 328 | 329 | use Naucon\File\LockHandler; 330 | use Naucon\File\LockManager; 331 | LockManager::init(new LockHandler($lockPath)); 332 | 333 | To perform locking create a instance of `Lock` with a unique identifier. Then call `lock()` to write the lock file. 334 | 335 | use Naucon\File\Lock; 336 | $lockObject = new Lock('foo'); 337 | 338 | $lockObject->lock(); // create lock file "~foo.lock" 339 | 340 | When calling `unlock()` the lock file will be removed; 341 | 342 | $lockObject->unlock(); // delete lock file "~foo.lock" 343 | 344 | To verify if a a lock file exist call `isLocked()`. 345 | 346 | if ($lockObject->isLocked()) { 347 | $lockObject->unlock(); // make sure that file is not locked (deadlock) 348 | } 349 | 350 | Example: 351 | 352 | $lockObject1 = new Lock('foo'); 353 | $lockObject1->lock(); // create lock file "~foo.lock" 354 | 355 | $lockObject2 = new Lock('foo'); 356 | try { 357 | $lockObject2->lock(); // throw exception - lock file lock file "~foo.lock" already there 358 | } catch (\Exception $e) { 359 | echo 'Already Locked
'; 360 | } 361 | 362 | $lockObject1->unlock(); // delete lock file "~foo.lock" 363 | 364 | $lockObject2->lock(); // create lock file "~foo.lock" again 365 | $lockObject2->unlock(); // delete lock file "~foo.lock" again 366 | 367 | 368 | ## Example 369 | 370 | Start the build-in webserver to see the examples in action: 371 | 372 | cd examples 373 | php -S 127.0.0.1:3000 374 | 375 | open url in browser 376 | 377 | http://127.0.0.1:3000/index.html 378 | 379 | 380 | ## License 381 | 382 | The MIT License (MIT) 383 | 384 | Copyright (c) 2015 Sven Sanzenbacher 385 | 386 | Permission is hereby granted, free of charge, to any person obtaining a copy 387 | of this software and associated documentation files (the "Software"), to deal 388 | in the Software without restriction, including without limitation the rights 389 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 390 | copies of the Software, and to permit persons to whom the Software is 391 | furnished to do so, subject to the following conditions: 392 | 393 | The above copyright notice and this permission notice shall be included in all 394 | copies or substantial portions of the Software. 395 | 396 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 397 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 398 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 399 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 400 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 401 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 402 | SOFTWARE. 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "naucon/file", 3 | "description": "This package contains php classes to access, change, copy, delete, move, rename files and directories.", 4 | "keywords": ["file", "dir", "filesystem", "io", "copy", "move", "delete", "rename"], 5 | "homepage": "https://github.com/naucon/File", 6 | "type": "library", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Sven Sanzenbacher" 11 | } 12 | ], 13 | "require": { 14 | "php": ">=5.3.0", 15 | "naucon/utility": "~1.0" 16 | }, 17 | "require-dev": { 18 | "phpunit/phpunit": "4.8.*" 19 | }, 20 | "autoload": { 21 | "psr-4": {"Naucon\\File\\": "src/"} 22 | }, 23 | "autoload-dev": { 24 | "psr-4": {"Naucon\\File\\Tests\\": "tests"} 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /examples/ExampleDir/ExampleChildDir/childFile.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/naucon/File/447d4e9d3e645192e996bde8f3d91669a6b4f996/examples/ExampleDir/ExampleChildDir/childFile.txt -------------------------------------------------------------------------------- /examples/ExampleDir/file1.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/naucon/File/447d4e9d3e645192e996bde8f3d91669a6b4f996/examples/ExampleDir/file1.txt -------------------------------------------------------------------------------- /examples/ExampleDir/file2.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/naucon/File/447d4e9d3e645192e996bde8f3d91669a6b4f996/examples/ExampleDir/file2.txt -------------------------------------------------------------------------------- /examples/FileExample.php: -------------------------------------------------------------------------------- 1 | getPathname() . ' do' . (($fileObject->exists()) ? '' : ' not') . ' exist.'; 10 | echo '
'; 11 | echo 'File is' . (($fileObject->isReadable()) ? '' : ' not') . ' readable.'; 12 | echo '
'; 13 | echo 'File is' . (($fileObject->isWritable()) ? '' : ' not') . ' writeable.'; 14 | echo '
'; 15 | echo '
'; 16 | 17 | echo 'Filesize: ' . $fileObject->getSize() . ' bytes'; 18 | echo '
'; 19 | echo 'Access Time: ' . $fileObject->lastAccessed()->format('d.m.Y H:s'); 20 | echo '
'; 21 | echo 'Change Time: ' . $fileObject->lastChanged()->format('d.m.Y H:s'); 22 | echo '
'; 23 | echo 'Modification Time: ' . $fileObject->lastModified()->format('d.m.Y H:s'); 24 | echo '
'; 25 | echo '
'; 26 | 27 | echo 'File Owner: ' . $fileObject->getOwnerName() . ' (' . $fileObject->getOwner() . ')'; 28 | echo '
'; 29 | echo 'File Group: ' . $fileObject->getGroupName() . ' (' . $fileObject->getGroup() . ')'; 30 | echo '
'; 31 | echo 'File permission: ' . $fileObject->getPermission(); 32 | echo '
'; 33 | echo '
'; 34 | 35 | 36 | echo 'Create new directory'; 37 | echo '
'; 38 | 39 | $newDirectoryPath = __DIR__ . '/tmp/target/move/'; 40 | $newDirectoryFileObject = new File($newDirectoryPath); 41 | if (!$newDirectoryFileObject->isDir()) { 42 | $newDirectoryFileObject->mkdirs(); 43 | } 44 | 45 | echo 'Copy file to target directory'; 46 | echo '
'; 47 | 48 | $examplePath = __DIR__ . '/example.txt'; 49 | $exampleCopyPath = __DIR__ . '/tmp/target/'; 50 | $fileObject = new File($examplePath); 51 | $fileObject->copy($exampleCopyPath); 52 | $fileObject->rename('example_copy.txt'); 53 | 54 | 55 | echo 'Move copied file to a new directory'; 56 | echo '
'; 57 | 58 | $exampleMovePath = __DIR__ . '/tmp/target/move/'; 59 | $fileObject->move($exampleMovePath); 60 | $fileObject->rename('example_move.txt'); 61 | 62 | echo 'Moved File ' . $fileObject->getPathname() . ' do' . (($fileObject->exists()) ? '' : ' not') . ' exist.'; 63 | echo '
'; 64 | echo 'File is' . (($fileObject->isReadable()) ? '' : ' not') . ' readable.'; 65 | echo '
'; 66 | echo 'File is' . (($fileObject->isWritable()) ? '' : ' not') . ' writeable.'; 67 | echo '
'; 68 | echo 'File permission: ' . $fileObject->getPermission(); 69 | echo '
'; 70 | echo '
'; 71 | 72 | 73 | echo 'Remove file'; 74 | echo '
'; 75 | $deletePath = __DIR__ . '/tmp/example.txt'; 76 | $fileObject = new File($deletePath); 77 | $fileObject->delete(); 78 | 79 | echo 'Remove directories'; 80 | echo '
'; 81 | $deletePath = __DIR__ . '/tmp/target/'; 82 | $fileObject = new File($deletePath); 83 | $fileObject->deleteAll(); 84 | 85 | $examplePath = __DIR__ . '/ExampleDir'; 86 | $fileObject = new File($examplePath); 87 | 88 | echo '
'; 89 | 90 | $iteratorObject = $fileObject->listAll(); 91 | echo 'Iterate * ' . $fileObject->getBasename() . ''; 92 | echo '
'; 93 | 94 | echo ''; 110 | 111 | $iteratorObject = $fileObject->listFiles(); 112 | echo 'Iterate files ' . $fileObject->getBasename() . ''; 113 | echo '
'; 114 | 115 | echo ''; 124 | -------------------------------------------------------------------------------- /examples/FileReaderExample.php: -------------------------------------------------------------------------------- 1 | getPathname(); 10 | echo '
'; 11 | echo '
'; 12 | 13 | echo 'Iterate lines:
'; 14 | // iterate 15 | foreach($fileObject as $line) { 16 | echo $line . '
'; 17 | } 18 | 19 | echo '
'; 20 | echo '
'; 21 | 22 | // while 23 | echo 'While:
'; 24 | echo $fileObject->firstLine(); 25 | echo '
'; 26 | while ( !$fileObject->isLast() ){ 27 | echo $fileObject->nextLine(); 28 | echo '
'; 29 | } 30 | 31 | echo '
'; 32 | echo '
'; 33 | 34 | echo 'Navigate:
'; 35 | echo $fileObject->firstLine(); 36 | echo '
'; 37 | echo $fileObject->nextLine(); 38 | echo '
'; 39 | echo $fileObject->nextLine(); 40 | echo '
'; 41 | echo $fileObject->firstLine(); 42 | echo '
'; 43 | echo $fileObject->readLine(3); 44 | echo '
'; 45 | echo $fileObject->readLine(7); 46 | echo '
'; 47 | 48 | echo '
'; 49 | echo '
'; 50 | 51 | // read all 52 | echo 'File Content:
'; 53 | echo nl2br($fileObject->read()); 54 | echo '
'; 55 | 56 | echo '
'; 57 | echo '
'; 58 | 59 | echo 'Array of lines:
'; 60 | $lines = $fileObject->readLines(); // return array 61 | foreach ($lines as $line) { 62 | echo $line . '
'; 63 | } -------------------------------------------------------------------------------- /examples/FileWriterExample.php: -------------------------------------------------------------------------------- 1 | getPathname(); 10 | echo '
'; 11 | echo '
'; 12 | 13 | $string = 'Line01'.PHP_EOL; 14 | $string.= 'Line02'.PHP_EOL; 15 | $string.= 'Line03'.PHP_EOL; 16 | $string.= 'Line04'.PHP_EOL; 17 | 18 | $fileObject->write($string); 19 | 20 | foreach($fileObject as $line) { 21 | echo $line . '
'; 22 | } 23 | echo '
'; 24 | 25 | //Line01 26 | //Line02 27 | //Line03 28 | //Line04 29 | 30 | 31 | $filePath = __DIR__ . '/example_write.txt'; 32 | $fileObject = new FileWriter($filePath,'a+'); // file point at the end of the file 33 | $fileObject->writeLine("foo"); 34 | $fileObject->writeLine("bar"); 35 | 36 | foreach($fileObject as $line) { 37 | echo $line . '
'; 38 | } 39 | echo '
'; 40 | 41 | //Line01 42 | //Line02 43 | //Line03 44 | //Line04 45 | //foo 46 | //bar 47 | 48 | $fileObject->clear(); // remove all content 49 | -------------------------------------------------------------------------------- /examples/LockManagerExample.php: -------------------------------------------------------------------------------- 1 | isLocked()) { 14 | $lockObject->unlock(); // make sure that file is not locked 15 | } 16 | 17 | if ($lockObject->lock()) { 18 | echo "Lock
"; 19 | } else { 20 | echo "Lock not possible
"; 21 | } 22 | 23 | $lockObject->unlock(); 24 | echo "Unlock
"; 25 | 26 | 27 | $lockObject1 = new Lock('foo'); 28 | $lockObject1->lock(); // create lock file "~foo.lock" 29 | 30 | $lockObject2 = new Lock('foo'); 31 | try { 32 | $lockObject2->lock(); // throw exception - lock file lock file "~foo.lock" already there 33 | } catch (\Exception $e) { 34 | echo 'Already Locked
'; 35 | } 36 | 37 | $lockObject1->unlock(); // delete lock file "~foo.lock" 38 | 39 | $lockObject2->lock(); // create lock file "~foo.lock" again 40 | $lockObject2->unlock(); // delete lock file "~foo.lock" again -------------------------------------------------------------------------------- /examples/content.txt: -------------------------------------------------------------------------------- 1 | Line01 2 | Line02 3 | Line03 4 | Line04 5 | Line05 6 | Line06 7 | Line07 8 | Line08 9 | Line09 -------------------------------------------------------------------------------- /examples/example.txt: -------------------------------------------------------------------------------- 1 | 1234567890 -------------------------------------------------------------------------------- /examples/example_read.txt: -------------------------------------------------------------------------------- 1 | Line01 2 | Line02 3 | Line03 4 | Line04 5 | Line05 6 | Line06 7 | Line07 8 | Line08 9 | Line09 10 | -------------------------------------------------------------------------------- /examples/example_write.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/naucon/File/447d4e9d3e645192e996bde8f3d91669a6b4f996/examples/example_write.txt -------------------------------------------------------------------------------- /examples/index.html: -------------------------------------------------------------------------------- 1 |

Examples

2 | 3 | -------------------------------------------------------------------------------- /examples/lock/.gitignore: -------------------------------------------------------------------------------- 1 | * -------------------------------------------------------------------------------- /examples/tmp/.gitignore: -------------------------------------------------------------------------------- 1 | * -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | ./tests/ 15 | 16 | 17 | 18 | 19 | 20 | ./ 21 | 22 | ./examples 23 | ./tests 24 | ./vendor 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/Exception/FileException.php: -------------------------------------------------------------------------------- 1 | getPathname()); 52 | } else { 53 | throw new FileException('No pathname was ' . __CLASS__ . ' given.', E_ERROR); 54 | } 55 | 56 | $this->setInfoClass('Naucon\File\File'); 57 | } 58 | 59 | 60 | /** 61 | * return a basename without extension 62 | * 63 | * @return string 64 | */ 65 | public function getName() 66 | { 67 | return $this->getBasename('.' . $this->getExtension()); 68 | } 69 | 70 | /** 71 | * return file extension. method was implemented as of version 5.3.6, 72 | * we will therefore check, which version we got and extract the extension 73 | * on our own otherwise 74 | * 75 | * @return string 76 | */ 77 | public function getExtension() 78 | { 79 | if (version_compare(PHP_VERSION, '5.3.6') >= 0) { 80 | return parent::getExtension(); 81 | } else { 82 | return ltrim(pathinfo($this->getAbsolutePath(), PATHINFO_EXTENSION), '.'); 83 | } 84 | } 85 | 86 | /** 87 | * return if file path is absolute 88 | * 89 | * @return bool 90 | */ 91 | public function isAbsolute() 92 | { 93 | $filepath = $this->getPathname(); 94 | if (isset($filepath[0]) && $filepath[0] == self::PATH_SEPARATOR) { 95 | return true; 96 | } 97 | return false; 98 | } 99 | 100 | /** 101 | * return absolute file path 102 | * 103 | * @return string 104 | */ 105 | public function getAbsolutePath() 106 | { 107 | if (!$this->isAbsolute()) { 108 | $filepath = stream_resolve_include_path($this->getPathname()); 109 | } else { 110 | $filepath = realpath($this->getPathname()); 111 | } 112 | return $filepath; 113 | } 114 | 115 | /** 116 | * return a instance of FileInterface of the parent directory or null 117 | * 118 | * @return FileInterface|FileInfoInterface|\SplFileInfo 119 | */ 120 | public function getParent() 121 | { 122 | return $this->getPathInfo(); 123 | } 124 | 125 | /** 126 | * return if fil e path is a directory 127 | * 128 | * @return bool 129 | * @see FileInfoInterface::isDir() 130 | */ 131 | public function isDirectory() 132 | { 133 | return $this->isDir(); 134 | } 135 | 136 | /** 137 | * returns if the file or directory of the current file path exists 138 | * 139 | * @return bool 140 | */ 141 | public function exists() 142 | { 143 | $return = false; 144 | if ($this->isFile() 145 | || $this->isDir() 146 | ) { 147 | $return = true; 148 | } 149 | return $return; 150 | } 151 | 152 | /** 153 | * return if file or directory is hidden, for example .htaccess, .svn 154 | * 155 | * @return bool 156 | */ 157 | public function isHidden() 158 | { 159 | $basename = $this->getBasename(); 160 | 161 | if (isset($basename[0]) && $basename[0] == '.') { 162 | return true; 163 | } 164 | return false; 165 | } 166 | 167 | /** 168 | * return a time of when last modified 169 | * 170 | * @return \DateTime date time object 171 | */ 172 | public function lastModified() 173 | { 174 | $dateTime = new \DateTime(); 175 | $dateTime->setTimestamp($this->getMTime()); 176 | return $dateTime; 177 | } 178 | 179 | /** 180 | * return a time of when last accessed 181 | * 182 | * @return \DateTime date time object 183 | */ 184 | public function lastAccessed() 185 | { 186 | $dateTime = new \DateTime(); 187 | $dateTime->setTimestamp($this->getATime()); 188 | return $dateTime; 189 | } 190 | 191 | /** 192 | * return a time of when last accessed 193 | * 194 | * @return \DateTime date time object 195 | */ 196 | public function lastChanged() 197 | { 198 | $dateTime = new \DateTime(); 199 | $dateTime->setTimestamp($this->getCTime()); 200 | return $dateTime; 201 | } 202 | 203 | /** 204 | * touch set access and modification time to file 205 | * 206 | * @param int $modificationTime optional unix timestamp of modification time, default is current time 207 | * @param int $accessTime optional unix timestamp of access time. default is the modification time 208 | * @return bool 209 | * @throws FileException 210 | */ 211 | public function touch($modificationTime = null, $accessTime = null) 212 | { 213 | if ($this->isFile()) { 214 | if (is_null($modificationTime)) { 215 | $modificationTime = time(); 216 | } 217 | 218 | if (is_null($accessTime)) { 219 | $accessTime = $modificationTime; 220 | } 221 | 222 | return touch($this->getPathname(), (int)$modificationTime, (int)$accessTime); 223 | } else { 224 | throw new FileException('File can not be touched, because it is no file or do not exist.', E_NOTICE); 225 | } 226 | } 227 | 228 | /** 229 | * create a empty file, named by the pathname 230 | * 231 | * @param int $mode optional file permission 232 | * @return bool returns true if the file was successfully created 233 | * @throws FileException 234 | */ 235 | public function createNewFile($mode = null) 236 | { 237 | if ($this->isAbsolute()) { 238 | if (is_null($mode)) { 239 | $mode = $this->defaul_permission; 240 | } 241 | 242 | if ($resource = fopen($this->getPathname(), 'x')) { 243 | fclose($resource); 244 | $this->chmod($mode); 245 | return true; 246 | } 247 | return false; 248 | } else { 249 | throw new FileException('Given file path is not a absolute path.'); 250 | } 251 | } 252 | 253 | /** 254 | * create a directory (but not the parent directories) 255 | * 256 | * @param int $mode optional file permission 257 | * @return bool 258 | * @throws FileException 259 | */ 260 | public function mkdir($mode = null) 261 | { 262 | if ($this->isAbsolute()) { 263 | if (is_null($mode)) { 264 | $mode = $this->defaul_permission; 265 | } 266 | 267 | if (mkdir($this->getPathname())) { 268 | $this->chmod($mode); 269 | return true; 270 | } 271 | return false; 272 | } else { 273 | throw new FileException('Given file path is not a absolute path.'); 274 | } 275 | } 276 | 277 | /** 278 | * create a directory recursive (with parent directories) 279 | * 280 | * @param int $mode optional file permission 281 | * @return bool 282 | * @throws FileException 283 | */ 284 | public function mkdirs($mode = null) 285 | { 286 | if ($this->isAbsolute()) { 287 | if (is_null($mode)) { 288 | $mode = $this->defaul_permission; 289 | } 290 | 291 | if (mkdir($this->getPathname(), 0777, true)) { 292 | $this->chmod($mode); 293 | return true; 294 | } 295 | return false; 296 | } else { 297 | throw new FileException('Given file path is not a absolute path.'); 298 | } 299 | } 300 | 301 | /** 302 | * delete the file or empty directory of the current file path 303 | * 304 | * @return bool true if file or directory was deleted, false if file or directory was not found 305 | * @throws FileException 306 | */ 307 | public function delete() 308 | { 309 | if ($this->isAbsolute()) { 310 | if ($this->isFile() || $this->isLink()) { 311 | // delete file and symlink 312 | return unlink($this->getPathname()); 313 | } elseif ($this->isDir()) { 314 | // delete directory 315 | return rmdir($this->getPathname()); 316 | } 317 | return false; 318 | } else { 319 | throw new FileException('Given file path is not a absolute path.'); 320 | } 321 | } 322 | 323 | /** 324 | * delete the file or directory with its content of the current file path 325 | * 326 | * @return bool true if file or directory was deleted, false if file or directory was not found 327 | * @throws FileException 328 | */ 329 | public function deleteAll() 330 | { 331 | if ($this->isAbsolute()) { 332 | if ($this->isFile() || $this->isLink()) { 333 | // delete file and symlink 334 | return unlink($this->getPathname()); 335 | } elseif ($this->isDir()) { 336 | // delete directories and its content 337 | return $this->deleteAction($this->getPathname(), true); 338 | } 339 | return false; 340 | } else { 341 | throw new FileException('Given file path is not a absolute path.'); 342 | } 343 | } 344 | 345 | /** 346 | * @access protected 347 | * @param string $pathname pathname 348 | * @param bool $recursive delete files recursive 349 | * @return bool 350 | */ 351 | protected function deleteAction($pathname, $recursive = false) 352 | { 353 | if ($recursive) { 354 | $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($pathname, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST); 355 | } else { 356 | $files = new \FilesystemIterator($pathname, \FilesystemIterator::SKIP_DOTS); 357 | } 358 | 359 | foreach ($files as $file) { 360 | if ($file->isDir()) { 361 | if (!rmdir($file->getRealPath())) { 362 | return false; 363 | } 364 | } else { 365 | if (!unlink($file->getRealPath())) { 366 | return false; 367 | } 368 | } 369 | } 370 | if (!rmdir($pathname)) { 371 | return false; 372 | } 373 | return true; 374 | } 375 | 376 | /** 377 | * delete files of the current directory 378 | * 379 | * @return bool true if files were deleted 380 | * @throws FileException 381 | * @throws Exception\FileFilterException 382 | */ 383 | public function deleteFiles() 384 | { 385 | if ($this->isAbsolute()) { 386 | if ($this->isDir()) { 387 | // delete files in the current directory 388 | return $this->deleteFilesAction($this->getPathname(), false); 389 | } else { 390 | throw new FileException('Given path is a file and not a directory.'); 391 | } 392 | } else { 393 | throw new FileException('Given file path is not a absolute path.'); 394 | } 395 | } 396 | 397 | /** 398 | * delete files of the current directory recursive 399 | * 400 | * @return bool true if files were deleted 401 | * @throws FileException 402 | * @throws Exception\FileFilterException 403 | */ 404 | public function deleteAllFiles() 405 | { 406 | if ($this->isAbsolute()) { 407 | if ($this->isDir()) { 408 | // delete files in the current directory and it's subdirectories 409 | return $this->deleteFilesAction($this->getPathname(), true); 410 | } else { 411 | throw new FileException('Given path is a file and not a directory.'); 412 | } 413 | } else { 414 | throw new FileException('Given file path is not a absolute path.'); 415 | } 416 | } 417 | 418 | /** 419 | * @access protected 420 | * @param string $pathname pathname 421 | * @param bool $recursive delete files recursive 422 | * @return bool 423 | * @throws Exception\FileFilterException 424 | */ 425 | public function deleteFilesAction($pathname, $recursive = false) 426 | { 427 | if ($recursive) { 428 | $files = new FileFilterType(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($pathname, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST),FileFilterType::TYPE_FILE); 429 | } else { 430 | $files = new FileFilterType(new \FilesystemIterator($pathname, \FilesystemIterator::SKIP_DOTS),FileFilterType::TYPE_FILE); 431 | } 432 | 433 | foreach ($files as $file) { 434 | if (!unlink($file->getRealPath())) { 435 | return false; 436 | } 437 | } 438 | return true; 439 | } 440 | 441 | /** 442 | * rename file 443 | * 444 | * @param string $pathname directory or file name with extension 445 | * @return bool 446 | * @throws FileException 447 | */ 448 | public function rename($pathname) 449 | { 450 | if (!empty($pathname)) { 451 | if ($this->exists()) { 452 | $targetPathname = $this->getPath() . self::PATH_SEPARATOR . (string)$pathname; 453 | if (rename($this->getPathname(), $targetPathname)) { 454 | parent::__construct($targetPathname); 455 | return true; 456 | } 457 | } else { 458 | throw new FileException('Rename failed, file or directory do not exist.'); 459 | } 460 | } else { 461 | throw new FileException('Rename failed, because given filename is empty.'); 462 | } 463 | return false; 464 | } 465 | 466 | /** 467 | * move file to a given directory 468 | * 469 | * @param \SplFileInfo|string $pathname 470 | * @return bool 471 | * @throws FileException 472 | */ 473 | public function move($pathname) 474 | { 475 | if (!empty($pathname)) { 476 | if ($pathname instanceof \SplFileInfo) { 477 | $targetFile = $pathname; 478 | } else { 479 | $targetFile = new File($pathname); 480 | } 481 | 482 | if ($this->exists()) { 483 | if ($targetFile->isDir()) { 484 | $targetPathname = $targetFile->getPathname() . self::PATH_SEPARATOR . $this->getBasename(); 485 | if (rename($this->getPathname(), $targetPathname)) { 486 | parent::__construct($targetPathname); 487 | return true; 488 | } 489 | } else { 490 | throw new FileException('Move failed, target directory do not exist.'); 491 | } 492 | } else { 493 | throw new FileException('Move failed, source file or directory do not exist.'); 494 | } 495 | } else { 496 | throw new FileException('Move failed, because given filepath is empty.'); 497 | } 498 | return false; 499 | } 500 | 501 | /** 502 | * copy file to a given directory 503 | * 504 | * @param \SplFileInfo|string $pathname 505 | * @return bool 506 | * @throws FileException 507 | */ 508 | public function copy($pathname) 509 | { 510 | if (!empty($pathname)) { 511 | if ($pathname instanceof \SplFileInfo) { 512 | $targetFile = $pathname; 513 | } else { 514 | $targetFile = new File($pathname); 515 | } 516 | 517 | if ($targetFile->isDir()) { 518 | $targetPathname = $targetFile->getPathname() . self::PATH_SEPARATOR . $this->getBasename(); 519 | if ($this->isFile()) { 520 | if (copy($this->getPathname(), $targetPathname)) { 521 | parent::__construct($targetPathname); 522 | return true; 523 | } 524 | } elseif ($this->isDir()) { 525 | if ($this->copyAction($this->getPathname(), $targetPathname, true)) { 526 | parent::__construct($targetPathname); 527 | return true; 528 | } 529 | } else { 530 | throw new FileException('Copy failed, source file or directory do not exist.'); 531 | } 532 | } else { 533 | throw new FileException('Copy failed, target directory do not exist.'); 534 | } 535 | } else { 536 | throw new FileException('Copy failed, because given filepath is empty.'); 537 | } 538 | return false; 539 | } 540 | 541 | /** 542 | * @access protected 543 | * @param string $sourcePathname source file path 544 | * @param string $targetPathname target file path 545 | * @param bool $recursive copy files recursive 546 | * @return bool 547 | */ 548 | protected function copyAction($sourcePathname, $targetPathname, $recursive = false) 549 | { 550 | if (!empty($sourcePathname) && !empty($targetPathname)) { 551 | if (is_file($sourcePathname)) { 552 | if (!copy($sourcePathname, $targetPathname)) { 553 | return false; 554 | } 555 | } elseif (is_dir($sourcePathname)) { 556 | if (!is_dir($targetPathname)) { 557 | if (!mkdir($targetPathname, 0777, true)) { 558 | return false; 559 | } 560 | } 561 | 562 | $sourceSubPaths = new \FilesystemIterator($sourcePathname, \FilesystemIterator::SKIP_DOTS); 563 | foreach ($sourceSubPaths as $sourceSubBasename) { 564 | $sourceSubFilePath = $sourcePathname . self::PATH_SEPARATOR . $sourceSubBasename; 565 | $targetSubFilePath = $targetPathname . self::PATH_SEPARATOR . $sourceSubBasename; 566 | if (!$this->copyAction($sourceSubFilePath, $targetSubFilePath, $recursive)) { 567 | return false; 568 | } 569 | } 570 | } 571 | return true; 572 | } 573 | return false; 574 | } 575 | 576 | /** 577 | * return user name of the file or directory 578 | * 579 | * @return string user name of file owner 580 | */ 581 | public function getOwnerName() 582 | { 583 | $userId = $this->getOwner(); 584 | if ($userId) { 585 | $userData = posix_getpwuid($userId); 586 | return ((isset($userData['name'])) ? $userData['name'] : null); 587 | } 588 | return false; 589 | } 590 | 591 | /** 592 | * return user group name of the file or directory 593 | * 594 | * @return string|bool|null user group name of file group 595 | */ 596 | public function getGroupName() 597 | { 598 | $userId = $this->getGroup(); 599 | if ($userId) { 600 | $userData = posix_getgrgid($userId); 601 | return ((isset($userData['name'])) ? $userData['name'] : null); 602 | } 603 | return false; 604 | } 605 | 606 | /** 607 | * return permission of file or directory 608 | * 609 | * @return string file permission 610 | */ 611 | public function getPermission() 612 | { 613 | return substr(decoct($this->getPerms()), 2); 614 | } 615 | 616 | /** 617 | * @param string|int $userGroup user group name or id 618 | * @return bool 619 | * @throws FileException 620 | */ 621 | public function chgrp($userGroup) 622 | { 623 | if ($this->exists() 624 | && (is_string($userGroup) || is_int($userGroup)) 625 | ) { 626 | if (!empty($userGroup)) { 627 | return chgrp($this->getPathname(), $userGroup); 628 | } else { 629 | throw new FileException('Chgrp failed, because given usergroup is empty.'); 630 | } 631 | } 632 | return false; 633 | } 634 | 635 | /** 636 | * change owner 637 | * 638 | * @param string|int $user user name or id 639 | * @return bool 640 | * @throws FileException 641 | */ 642 | public function chown($user) 643 | { 644 | if ($this->exists() 645 | && (is_string($user) || is_int($user)) 646 | ) { 647 | if (!empty($user)) { 648 | return chown($this->getPathname(), $user); 649 | } else { 650 | throw new FileException('Chown failed, because given user is empty.'); 651 | } 652 | } 653 | return false; 654 | } 655 | 656 | /** 657 | * change permission 658 | * 659 | * @param int|string $fileMode file permission, default permission is 0777 660 | * @return bool 661 | * @throws FileException 662 | */ 663 | public function chmod($fileMode) 664 | { 665 | if ($this->exists()) { 666 | // file mode must be from type octal. through converting octal to decimal and the other way around 667 | // we going sure that the given value is a octal. Any non octal number will be detected. 668 | if (decoct(octdec($fileMode)) != $fileMode) { 669 | throw new FileException('Chmod failed, because given permission is not from type octal.'); 670 | } 671 | 672 | // convert a given octal string to a octal integer 673 | if (is_string($fileMode)) { 674 | $fileMode = intval($fileMode, 8); 675 | } 676 | 677 | switch ($fileMode) { 678 | case 0600: // file owner read and write; 679 | case 0640: // file owner read and write; owner group read 680 | case 0660: // file owner read and write; owner group read and write 681 | case 0604: // file owner read and write; everbody read 682 | case 0606: // file owner read and write; everbody read and write 683 | case 0664: // file owner read and write; owner group read and write; everbody read 684 | case 0666: // file owner read and write; owner group read and write; everbody read and write 685 | case 0700: // file owner read, execute and write; 686 | case 0740: // file owner read, execute and write; owner group read 687 | case 0760: // file owner read, execute and write; owner group read and write 688 | case 0770: // file owner read, execute and write; owner group read, execute and write 689 | case 0704: // file owner read, execute and write; everbody read 690 | case 0706: // file owner read, execute and write; everbody read and write 691 | case 0707: // file owner read, execute and write; everbody read, execute and write 692 | case 0744: // file owner read, execute and write; owner group read; everbody read 693 | case 0746: // file owner read, execute and write; owner group read; everbody read and write 694 | case 0747: // file owner read, execute and write; owner group read; everbody read, execute and write 695 | case 0754: // file owner read, execute and write; owner group read and execute; everbody read 696 | case 0755: // file owner read, execute and write; owner group read and execute; everbody read and execute 697 | case 0756: // file owner read, execute and write; owner group read and execute; everbody read and write 698 | case 0757: // file owner read, execute and write; owner group read and execute; everbody read, execute and write 699 | case 0764: // file owner read, execute and write; owner group read and write; everbody read 700 | case 0766: // file owner read, execute and write; owner group read and write; everbody read and write 701 | case 0767: // file owner read, execute and write; owner group read and write; everbody read, execute and write 702 | case 0774: // file owner read, execute and write; owner group read, execute and write; everbody read 703 | case 0775: // file owner read, execute and write; owner group read, execute and write; everbody read, execute and write 704 | case 0776: // file owner read, execute and write; owner group read, execute and write; everbody read and write 705 | case 0777: // file owner read, execute and write; owner group read, execute and write; everbody read, execute and write 706 | break; 707 | default: 708 | $fileMode = 0777; 709 | } 710 | 711 | return chmod($this->getPathname(), $fileMode); 712 | } 713 | return false; 714 | } 715 | 716 | /** 717 | * clear file status cache 718 | */ 719 | public function flush() 720 | { 721 | clearstatcache(); 722 | } 723 | 724 | /** 725 | * return file size in bytes 726 | * 727 | * @return int filesize in bytes or -1 if it fails 728 | */ 729 | public function getSize() 730 | { 731 | $filePath = $this->getPathname(); 732 | $size = -1; 733 | $isWin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'); 734 | $execWorks = (function_exists('exec') && !ini_get('safe_mode') && exec('echo EXEC') == 'EXEC'); 735 | if ($isWin) { 736 | if ($execWorks) { 737 | $cmd = "for %F in (\"$filePath\") do @echo %~zF"; 738 | exec($cmd, $output); 739 | if (is_array($output)) { 740 | $result = trim(implode("\n", $output)); 741 | if (ctype_digit($result)) { 742 | $size = $result; 743 | } 744 | } 745 | } 746 | // try the Windows COM interface if its fails 747 | if (class_exists('COM') && $size > -1) { 748 | $fsobj = new \COM('Scripting.FileSystemObject'); 749 | $file = $fsobj->GetFile($filePath); 750 | $result = $file->Size; 751 | if (ctype_digit($result)) { 752 | $size = $result; 753 | } 754 | } 755 | } else { 756 | $result = trim("stat -c%s $filePath"); 757 | if (ctype_digit($result)) { 758 | $size = $result; 759 | } 760 | } 761 | if ($size < 0) { 762 | $size = filesize($filePath); 763 | } 764 | return $size; 765 | } 766 | 767 | /** 768 | * returns files and directories 769 | * 770 | * @return \FilesystemIterator 771 | */ 772 | public function listAll() 773 | { 774 | $iterator = new \FilesystemIterator($this->getAbsolutePath()); 775 | $iterator->setInfoClass(get_class($this)); 776 | return $iterator; 777 | } 778 | 779 | /** 780 | * returns files 781 | * 782 | * @return \FilesystemIterator|FileFilterType 783 | * @throws Exception\FileFilterException 784 | */ 785 | public function listFiles() 786 | { 787 | $innerIterator = new \FilesystemIterator($this->getAbsolutePath()); 788 | $innerIterator->setInfoClass(get_class($this)); 789 | $iterator = new FileFilterType($innerIterator, 'file'); 790 | return $iterator; 791 | } 792 | } -------------------------------------------------------------------------------- /src/FileFilterInterface.php: -------------------------------------------------------------------------------- 1 | type = (string)$type; 52 | break; 53 | default: 54 | throw new FileFilterException('File type filter failed because given file type is unkown.'); 55 | } 56 | parent::__construct($iterator); 57 | } 58 | 59 | /** 60 | * @return string file type 61 | */ 62 | public function getType() 63 | { 64 | return $this->type; 65 | } 66 | 67 | /** 68 | * filter iterator element 69 | * 70 | * @return bool 71 | */ 72 | public function accept() 73 | { 74 | /** 75 | * @var \SplFileInfo $file 76 | */ 77 | $file = $this->current(); 78 | if ($file->getType()==$this->getType()) { 79 | return true; 80 | } 81 | return false; 82 | } 83 | } -------------------------------------------------------------------------------- /src/FileInfoInterface.php: -------------------------------------------------------------------------------- 1 | openFileObject($mode, $skipEmptyLines); 63 | } 64 | } -------------------------------------------------------------------------------- /src/FileReaderAbstract.php: -------------------------------------------------------------------------------- 1 | fileObject); 42 | } 43 | 44 | 45 | 46 | /** 47 | * @access protected 48 | * @return \SplFileObject file object 49 | */ 50 | protected function getFileObject() 51 | { 52 | return $this->fileObject; 53 | } 54 | 55 | /** 56 | * file mode 57 | * r = read only, beginning of file 58 | * r+ = read and write, beginning of file 59 | * w = write only, beginning of file, empty file, create file if necessary 60 | * w+ = read and write, beginning of file, empty file, create file if nesessary 61 | * a = write only, end of file, create file if necessary 62 | * a+ = read and write, end of file, create file if necessary 63 | * x = write only, beginning of file, only create file 64 | * x+ = read and write, beginning of file, only create file 65 | * 66 | * +----+----+-----+-----+---+------+ 67 | * |mode|read|write|start|end|create| 68 | * +----+----+-----+-----+---+------+ 69 | * | r | x | | x | | | 70 | * +----+----+-----+-----+---+------+ 71 | * | r+ | x | x | x | | | 72 | * +----+----+-----+-----+---+------+ 73 | * | w | | x | x | | opt | 74 | * +----+----+-----+-----+---+------+ 75 | * | w+ | x | x | x | | opt | 76 | * +----+----+-----+-----+---+------+ 77 | * | a | | x | | x | opt | 78 | * +----+----+-----+-----+---+------+ 79 | * | a+ | x | x | | x | opt | 80 | * +----+----+-----+-----+---+------+ 81 | * | x | | x | x | | only | 82 | * +----+----+-----+-----+---+------+ 83 | * | x+ | x | x | x | | only | 84 | * +----+----+-----+-----+---+------+ 85 | * 86 | * @param string $mode file mode 87 | * @param bool $skipEmptyLines true = skip empty lines, false = contains empty lines 88 | * @throws Exception\FileException 89 | */ 90 | protected function openFileObject($mode, $skipEmptyLines) 91 | { 92 | if (!$this->getParent()->isDir()) { 93 | $this->getParent()->mkdirs(); 94 | } 95 | 96 | $newFile = false; 97 | if (!$this->isFile()) { 98 | switch ($mode) 99 | { 100 | case 'w': 101 | case 'w+': 102 | case 'a': 103 | case 'a+': 104 | case 'x': 105 | case 'x+': 106 | $newFile = true; 107 | break; 108 | } 109 | } 110 | 111 | $this->fileObject = $this->openFile($mode); 112 | 113 | if ($skipEmptyLines) { 114 | $this->skipEmptyLines = true; 115 | $this->fileObject->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::READ_AHEAD| \SplFileObject::SKIP_EMPTY); 116 | } else { 117 | $this->fileObject->setFlags(\SplFileObject::DROP_NEW_LINE); 118 | } 119 | 120 | if ($newFile) { 121 | $this->chmod($this->defaul_permission); 122 | } 123 | } 124 | 125 | /** 126 | * @return string 127 | */ 128 | public function read() 129 | { 130 | if ($this->getFileObject()->isFile()) { 131 | return file_get_contents($this->getFileObject()->getPathname()); 132 | } 133 | return null; 134 | } 135 | 136 | /** 137 | * return a specified line of the file 138 | * 139 | * @param int $line line number 140 | * @return string|array file line, or false is line do not exist 141 | */ 142 | public function readLine($line = 0) 143 | { 144 | // // seek line 145 | $this->getFileObject()->seek($line); 146 | return $this->current(); 147 | } 148 | 149 | /** 150 | * return an array with all lines of a file 151 | * 152 | * @return array file lines 153 | */ 154 | public function readLines() 155 | { 156 | if ($this->getFileObject()->isFile()) { 157 | if ($this->skipEmptyLines) { 158 | // skip empty lines 159 | $lines = file($this->getFileObject()->getPathname(), FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES); 160 | } else { 161 | $lines = file($this->getFileObject()->getPathname(), FILE_IGNORE_NEW_LINES); 162 | } 163 | 164 | if (is_array($lines)) { 165 | return $lines; 166 | } 167 | } 168 | return array(); 169 | } 170 | 171 | /** 172 | * return first line 173 | * 174 | * @return string|array 175 | */ 176 | public function firstLine() 177 | { 178 | $this->first(); 179 | return $this->getFileObject()->current(); 180 | } 181 | 182 | /** 183 | * return next line of file 184 | * 185 | * @return string|array file line 186 | */ 187 | public function nextLine() 188 | { 189 | $this->next(); 190 | return $this->current(); 191 | } 192 | 193 | /** 194 | * @return bool current line is first 195 | */ 196 | public function isFirst() 197 | { 198 | if ($this->key()==0) { 199 | return true; 200 | } 201 | return false; 202 | } 203 | 204 | /** 205 | * @return bool current line is last 206 | */ 207 | public function isLast() 208 | { 209 | return $this->getFileObject()->eof(); 210 | } 211 | 212 | /** 213 | * return true if there is a next line 214 | * 215 | * @return bool has next line 216 | */ 217 | public function hasNext() 218 | { 219 | if (!$this->isLast()) { 220 | return true; 221 | } 222 | return false; 223 | } 224 | 225 | /** 226 | * set first line as current line 227 | * 228 | * @see FileReader::rewind() 229 | */ 230 | public function first() 231 | { 232 | $this->getFileObject()->rewind(); 233 | } 234 | 235 | /** 236 | * return current line 237 | * 238 | * @return string|array current item 239 | */ 240 | public function current() 241 | { 242 | return $this->getFileObject()->current(); 243 | } 244 | 245 | /** 246 | * set point to next line 247 | */ 248 | public function next() 249 | { 250 | $this->getFileObject()->next(); 251 | } 252 | 253 | /** 254 | * return index of the current line 255 | * 256 | * @return int index of current line 257 | */ 258 | public function key() 259 | { 260 | return $this->getFileObject()->key(); 261 | } 262 | 263 | /** 264 | * return true if current line is valid 265 | * 266 | * @return bool current line is valid 267 | */ 268 | public function valid() 269 | { 270 | return $this->getFileObject()->valid(); 271 | } 272 | 273 | /** 274 | * rewind to the first line 275 | */ 276 | public function rewind() 277 | { 278 | $this->getFileObject()->rewind(); 279 | } 280 | } -------------------------------------------------------------------------------- /src/FileReaderInterface.php: -------------------------------------------------------------------------------- 1 | openFileObject($mode, $skipEmptyLines); 65 | } 66 | 67 | /** 68 | * write string to file 69 | * 70 | * @param string $string file content 71 | * @return FileWriterInterface 72 | * @throws FileWriterException 73 | */ 74 | public function write($string) 75 | { 76 | if ($this->getFileObject()->fwrite($string) === false) { 77 | // return written bytes or null on error 78 | throw new FileWriterException('write to file failed.'); 79 | } 80 | return $this; 81 | } 82 | 83 | /** 84 | * add string to file 85 | * 86 | * @param string $string file content 87 | * @return FileWriterInterface 88 | * @throws FileWriterException 89 | */ 90 | public function writeLine($string) 91 | { 92 | $string = rtrim($string, "\n\r") . PHP_EOL; 93 | if ($this->getFileObject()->fwrite($string) === false) { 94 | // return written bytes or null on error 95 | throw new FileWriterException('write line to file failed.'); 96 | } 97 | return $this; 98 | } 99 | 100 | /** 101 | * clear file 102 | */ 103 | public function clear() 104 | { 105 | $this->getFileObject()->ftruncate(0); 106 | } 107 | 108 | /** 109 | * truncates file to a given length (in bytes) 110 | * 111 | * @param int $bytes length in bytes 112 | */ 113 | public function truncates($bytes) 114 | { 115 | $this->getFileObject()->ftruncate($bytes); 116 | } 117 | } -------------------------------------------------------------------------------- /src/FileWriterInterface.php: -------------------------------------------------------------------------------- 1 | setLockId($lockId); 40 | } 41 | 42 | 43 | /** 44 | * @return string lock id 45 | */ 46 | public function getLockId() 47 | { 48 | return $this->lockId; 49 | } 50 | 51 | /** 52 | * @access protected 53 | * @param string $lockId lock id 54 | * @throws LockException 55 | */ 56 | protected function setLockId($lockId) 57 | { 58 | if (strlen(trim((string)$lockId)) > 0) { 59 | $this->lockId = (string)$lockId; 60 | } else { 61 | throw new LockException('Lock ID is not valid.'); 62 | } 63 | } 64 | 65 | /** 66 | * @return bool 67 | * @throws Exception\LockManagerException 68 | */ 69 | public function isLocked() 70 | { 71 | return LockManager::isLocked($this); 72 | } 73 | 74 | /** 75 | * @return bool 76 | * @throws Exception\LockManagerException 77 | */ 78 | public function lock() 79 | { 80 | return LockManager::lock($this); 81 | } 82 | 83 | /** 84 | * @return bool 85 | * @throws Exception\LockManagerException 86 | */ 87 | public function unlock() 88 | { 89 | return LockManager::unlock($this); 90 | } 91 | } -------------------------------------------------------------------------------- /src/LockHandler.php: -------------------------------------------------------------------------------- 1 | setFileInfo($pathname); 39 | } 40 | 41 | /** 42 | * Destructor 43 | */ 44 | public function __destruct() 45 | { 46 | unset($this->fileInfo); 47 | } 48 | 49 | 50 | 51 | /** 52 | * @access protected 53 | * @return \SplFileInfo 54 | */ 55 | protected function getFileInfo() 56 | { 57 | return $this->fileInfo; 58 | } 59 | 60 | /** 61 | * @access protected 62 | * @param \SplFileInfo|string $pathname pathname 63 | */ 64 | protected function setFileInfo($pathname) 65 | { 66 | if ($pathname instanceof \SplFileInfo) { 67 | $this->fileInfo = $pathname; 68 | } else { 69 | $this->fileInfo = new \SplFileInfo($pathname); 70 | } 71 | } 72 | 73 | /** 74 | * @access protected 75 | * @param LockInterface $lockObject lock object 76 | * @return File 77 | * @throws Exception\FileException 78 | */ 79 | protected function getLockFile(LockInterface $lockObject) 80 | { 81 | $pathname = $this->getFileInfo()->getPathname() . '/~' . $lockObject->getLockId() . '.lock'; 82 | return new File($pathname); 83 | } 84 | 85 | /** 86 | * @param LockInterface $lockObject lock object 87 | * @return bool 88 | * @throws Exception\FileException 89 | */ 90 | public function isLocked(LockInterface $lockObject) 91 | { 92 | $fileObject = $this->getLockFile($lockObject); 93 | if ($fileObject->isFile()) { 94 | return true; 95 | } 96 | return false; 97 | } 98 | 99 | /** 100 | * @param LockInterface $lockObject lock object 101 | * @return bool 102 | * @throws LockHandlerException 103 | * @throws Exception\FileException 104 | */ 105 | public function lock(LockInterface $lockObject) 106 | { 107 | $fileObject = $this->getLockFile($lockObject); 108 | if (!$fileObject->isFile()) { 109 | $fileObject->createNewFile(); 110 | return true; 111 | } else { 112 | // alarm 113 | throw new LockHandlerException('Lock ID ' . $lockObject->getLockId() . ' already locked.'); 114 | } 115 | } 116 | 117 | /** 118 | * @param LockInterface $lockObject lock object 119 | * @return bool 120 | * @throws Exception\FileException 121 | */ 122 | public function unlock(LockInterface $lockObject) 123 | { 124 | $fileObject = $this->getLockFile($lockObject); 125 | if ($fileObject->delete()) { 126 | return true; 127 | } 128 | return false; 129 | } 130 | } -------------------------------------------------------------------------------- /src/LockHandlerInterface.php: -------------------------------------------------------------------------------- 1 | lockHandler = $lockHandler; 48 | } 49 | 50 | /** 51 | * Destructor 52 | */ 53 | public function __destruct() 54 | { 55 | unset($this->lockHandler); 56 | } 57 | 58 | /** 59 | * Clone 60 | * 61 | * @access private 62 | */ 63 | private function __clone() 64 | { 65 | } 66 | 67 | 68 | 69 | /** 70 | * @return LockHandlerInterface lock handler object 71 | */ 72 | public function getLockHandler() 73 | { 74 | return $this->lockHandler; 75 | } 76 | 77 | /** 78 | * singleton 79 | * 80 | * @static 81 | * @param LockHandlerInterface $lockHandler 82 | * @return LockManager 83 | */ 84 | static public function init(LockHandlerInterface $lockHandler) 85 | { 86 | if (is_null(self::$singletonObject)) { 87 | self::$singletonObject = new self($lockHandler); 88 | } 89 | return self::$singletonObject; 90 | } 91 | 92 | /** 93 | * @static 94 | * @param LockInterface $lockObject 95 | * @return bool 96 | * @throws LockManagerException 97 | */ 98 | static public function isLocked(LockInterface $lockObject) 99 | { 100 | if (!is_null(self::$singletonObject)) { 101 | return self::$singletonObject->getLockHandler()->isLocked($lockObject); 102 | } else { 103 | throw new LockManagerException('IsLocked failed. LockManager is not initialized.'); 104 | } 105 | } 106 | 107 | /** 108 | * @static 109 | * @param LockInterface $lockObject 110 | * @return bool 111 | * @throws LockManagerException 112 | */ 113 | static public function lock(LockInterface $lockObject) 114 | { 115 | if (!is_null(self::$singletonObject)) { 116 | return self::$singletonObject->getLockHandler()->lock($lockObject); 117 | } else { 118 | throw new LockManagerException('Lock failed. LockManager is not initialized.'); 119 | } 120 | } 121 | 122 | /** 123 | * @static 124 | * @param LockInterface $lockObject 125 | * @return bool 126 | * @throws LockManagerException 127 | */ 128 | static public function unlock(LockInterface $lockObject) 129 | { 130 | if (!is_null(self::$singletonObject)) { 131 | return self::$singletonObject->getLockHandler()->unlock($lockObject); 132 | } else { 133 | throw new LockManagerException('Unlock failed. LockManager is not initialized.'); 134 | } 135 | } 136 | } -------------------------------------------------------------------------------- /tests/ExampleDir/ExampleChildDir/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/naucon/File/447d4e9d3e645192e996bde8f3d91669a6b4f996/tests/ExampleDir/ExampleChildDir/.gitkeep -------------------------------------------------------------------------------- /tests/ExampleDir/testFile1.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/naucon/File/447d4e9d3e645192e996bde8f3d91669a6b4f996/tests/ExampleDir/testFile1.txt -------------------------------------------------------------------------------- /tests/ExampleDir/testFile2.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/naucon/File/447d4e9d3e645192e996bde8f3d91669a6b4f996/tests/ExampleDir/testFile2.txt -------------------------------------------------------------------------------- /tests/FileReaderTest.php: -------------------------------------------------------------------------------- 1 | assertEquals($filePath, $fileObject->getPathname()); 22 | $this->assertTrue($fileObject->isReadable()); 23 | 24 | $fileObject = new \SplFileObject($filePath, 'r'); 25 | $this->assertEquals($filePath, $fileObject->getPathname()); 26 | $this->assertTrue($fileObject->isReadable()); 27 | } 28 | 29 | /** 30 | * @depends testInit 31 | */ 32 | public function testIterate() 33 | { 34 | $filePath = __DIR__ . '/example_read.txt'; 35 | 36 | $lines = array( 37 | 'Line01', 'Line02', 'Line03', 'Line04', 'Line05', 'Line06', 'Line07', 'Line08', 'Line09', 38 | ); 39 | 40 | 41 | $fileObject = new FileReader($filePath, 'r', true); 42 | $i = 0; 43 | foreach($fileObject as $line) { 44 | $this->assertContains($line, $lines); 45 | $i++; 46 | } 47 | $this->assertEquals(9, $i); 48 | 49 | 50 | $fileObject = new \SplFileObject($filePath, 'r'); 51 | $fileObject->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::READ_AHEAD| \SplFileObject::SKIP_EMPTY); 52 | $i = 0; 53 | foreach($fileObject as $line) { 54 | $this->assertContains($line, $lines); 55 | $i++; 56 | } 57 | $this->assertEquals(9, $i); 58 | } 59 | 60 | /** 61 | * @depends testInit 62 | */ 63 | public function testNextLine() 64 | { 65 | $filePath = __DIR__ . '/example_read.txt'; 66 | 67 | $fileObject = new FileReader($filePath, 'r', true); 68 | $this->assertEquals('Line01', $fileObject->firstLine()); 69 | $this->assertEquals('Line02', $fileObject->nextLine()); 70 | $this->assertEquals('Line03', $fileObject->nextLine()); 71 | $this->assertEquals('Line01', $fileObject->firstLine()); 72 | 73 | $fileObject = new \SplFileObject($filePath, 'r'); 74 | $fileObject->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::READ_AHEAD| \SplFileObject::SKIP_EMPTY); 75 | $this->assertEquals('Line01', $fileObject->current()); 76 | $fileObject->next(); 77 | $this->assertEquals('Line02', $fileObject->current()); 78 | $fileObject->next(); 79 | $this->assertEquals('Line03', $fileObject->current()); 80 | $fileObject->rewind(); 81 | $this->assertEquals('Line01', $fileObject->current()); 82 | } 83 | 84 | /** 85 | * @depends testInit 86 | */ 87 | public function testIsFirst() 88 | { 89 | $filePath = __DIR__ . '/example_read.txt'; 90 | 91 | $fileObject = new FileReader($filePath, 'r', true); 92 | $this->assertTrue($fileObject->isFirst()); 93 | $this->assertEquals('Line01', $fileObject->firstLine()); 94 | $this->assertEquals('Line02', $fileObject->nextLine()); 95 | $this->assertEquals('Line03', $fileObject->nextLine()); 96 | $this->assertFalse($fileObject->isFirst()); 97 | } 98 | 99 | /** 100 | * @depends testInit 101 | */ 102 | public function testReadLine() 103 | { 104 | $filePath = __DIR__ . '/example_read.txt'; 105 | 106 | $fileObject = new FileReader($filePath, 'r', true); 107 | $this->assertEquals('Line04', $fileObject->readLine(3)); 108 | 109 | $fileObject = new \SplFileObject($filePath, 'r'); 110 | $fileObject->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::READ_AHEAD| \SplFileObject::SKIP_EMPTY); 111 | $fileObject->seek(3); // seek line 112 | $this->assertEquals('Line04', $fileObject->current()); 113 | } 114 | 115 | /** 116 | * @depends testInit 117 | */ 118 | public function testReadInvalidLine() 119 | { 120 | $filePath = __DIR__ . '/example_read.txt'; 121 | 122 | $fileObject = new FileReader($filePath, 'r', true); 123 | $this->assertFalse($fileObject->readLine(20)); 124 | 125 | $fileObject = new \SplFileObject($filePath, 'r'); 126 | $fileObject->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::READ_AHEAD| \SplFileObject::SKIP_EMPTY); 127 | $fileObject->seek(20); // seek line 128 | $this->assertFalse($fileObject->current()); 129 | } 130 | 131 | /** 132 | * @depends testInit 133 | */ 134 | public function testIsLast() 135 | { 136 | $filePath = __DIR__ . '/example_read.txt'; 137 | 138 | $fileObject = new FileReader($filePath, 'r', true); 139 | $this->assertEquals('Line08', $fileObject->readLine(7)); 140 | $this->assertFalse($fileObject->isLast()); 141 | $this->assertEquals('Line09', $fileObject->nextLine()); 142 | $this->assertEquals('', $fileObject->nextLine()); 143 | $this->assertTrue($fileObject->isLast()); 144 | 145 | $fileObject = new \SplFileObject($filePath, 'r'); 146 | $fileObject->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::READ_AHEAD| \SplFileObject::SKIP_EMPTY); 147 | $fileObject->seek(7); 148 | $this->assertEquals('Line08', $fileObject->current()); 149 | $this->assertFalse($fileObject->eof()); 150 | $fileObject->next(); 151 | $this->assertEquals('Line09', $fileObject->current()); 152 | $fileObject->next(); 153 | $this->assertEquals('', $fileObject->current()); 154 | $this->assertTrue($fileObject->eof()); 155 | } 156 | 157 | /** 158 | * @depends testInit 159 | */ 160 | public function testRead() 161 | { 162 | $filePath = __DIR__ . '/example_read.txt'; 163 | $fileObject = new FileReader($filePath, 'r', true); 164 | $expectedString = 'Line01' . PHP_EOL 165 | . 'Line02' . PHP_EOL 166 | . 'Line03' . PHP_EOL 167 | . 'Line04' . PHP_EOL 168 | . 'Line05' . PHP_EOL 169 | . 'Line06' . PHP_EOL 170 | . 'Line07' . PHP_EOL 171 | . 'Line08' . PHP_EOL 172 | . 'Line09' . PHP_EOL; 173 | $this->assertEquals($expectedString, $fileObject->read()); 174 | } 175 | 176 | /** 177 | * @depends testInit 178 | */ 179 | public function testReadLines() 180 | { 181 | $filePath = __DIR__ . '/example_read.txt'; 182 | $fileObject = new FileReader($filePath, 'r', true); 183 | $expectedArray = array('Line01', 'Line02', 'Line03', 'Line04', 'Line05', 'Line06', 'Line07', 'Line08', 'Line09'); 184 | $this->assertEquals($expectedArray, $fileObject->readLines()); 185 | } 186 | } -------------------------------------------------------------------------------- /tests/FileTest.php: -------------------------------------------------------------------------------- 1 | deleteAll(); 22 | 23 | // remove directories 24 | $filePath = __DIR__ . '/tmp/bar'; 25 | $fileObject = new File($filePath); 26 | $fileObject->deleteAll(); 27 | 28 | // remove directories 29 | $filePath = __DIR__ . '/tmp/foo2'; 30 | $fileObject = new File($filePath); 31 | $fileObject->deleteAll(); 32 | } 33 | 34 | /** 35 | * @expectedException \Naucon\File\Exception\FileException 36 | */ 37 | public function testEmptyInit() 38 | { 39 | $fileObject = new File(''); 40 | } 41 | 42 | public function testInit() 43 | { 44 | $fileObject = new File('example.txt'); 45 | $this->assertEquals('example.txt', $fileObject->getPathname()); 46 | $this->assertEquals('example.txt', (string)$fileObject); 47 | $this->assertEquals('example.txt', $fileObject->getBasename()); 48 | $this->assertEquals('', $fileObject->getPath()); 49 | 50 | $fileObject = new File('./example.txt'); 51 | $this->assertEquals('./example.txt', $fileObject->getPathname()); 52 | $this->assertEquals('./example.txt', (string)$fileObject); 53 | $this->assertEquals('example.txt', $fileObject->getBasename()); 54 | $this->assertEquals('.', $fileObject->getPath()); 55 | 56 | $fileObject = new File('../example.txt'); 57 | $this->assertEquals('../example.txt', $fileObject->getPathname()); 58 | $this->assertEquals('../example.txt', (string)$fileObject); 59 | $this->assertEquals('example.txt', $fileObject->getBasename()); 60 | $this->assertEquals('..', $fileObject->getPath()); 61 | 62 | $fileObject = new File('tmp/example.txt'); 63 | $this->assertEquals('tmp/example.txt', $fileObject->getPathname()); 64 | $this->assertEquals('tmp/example.txt', (string)$fileObject); 65 | $this->assertEquals('example.txt', $fileObject->getBasename()); 66 | $this->assertEquals('tmp', $fileObject->getPath()); 67 | 68 | $fileObject = new File('/example.txt'); 69 | $this->assertEquals('/example.txt', $fileObject->getPathname()); 70 | $this->assertEquals('/example.txt', (string)$fileObject); 71 | $this->assertEquals('example.txt', $fileObject->getBasename()); 72 | $this->assertEquals('', $fileObject->getPath()); 73 | 74 | $fileObject = new File('tmp/'); 75 | $this->assertEquals('tmp', $fileObject->getPathname()); 76 | $this->assertEquals('tmp', (string)$fileObject); 77 | $this->assertEquals('tmp', $fileObject->getBasename()); 78 | $this->assertEquals('', $fileObject->getPath()); 79 | 80 | $fileObject = new File('tmp'); 81 | $this->assertEquals('tmp', $fileObject->getPathname()); 82 | $this->assertEquals('tmp', (string)$fileObject); 83 | $this->assertEquals('tmp', $fileObject->getBasename()); 84 | $this->assertEquals('', $fileObject->getPath()); 85 | 86 | $fileObject = new File('/foo/bar/'); 87 | $this->assertEquals('/foo/bar', $fileObject->getPathname()); 88 | $this->assertEquals('/foo/bar', (string)$fileObject); 89 | $this->assertEquals('bar', $fileObject->getBasename()); 90 | $this->assertEquals('/foo', $fileObject->getPath()); 91 | 92 | $fileObject = new File('/foo\bar/'); 93 | $this->assertEquals('/foo/bar', $fileObject->getPathname()); // deviation to SplFileInfo '/foo\bar' 94 | $this->assertEquals('/foo/bar', (string)$fileObject); // deviation to SplFileInfo '/foo\bar' 95 | $this->assertEquals('bar', $fileObject->getBasename()); // deviation to SplFileInfo 'foo\bar' 96 | $this->assertEquals('/foo', $fileObject->getPath()); // deviation to SplFileInfo '' 97 | 98 | 99 | $fileObject = new \SplFileInfo('example.txt'); 100 | $this->assertEquals('example.txt', $fileObject->getPathname()); 101 | $this->assertEquals('example.txt', (string)$fileObject); 102 | $this->assertEquals('example.txt', $fileObject->getBasename()); 103 | $this->assertEquals('', $fileObject->getPath()); 104 | 105 | $fileObject = new \SplFileInfo('./example.txt'); 106 | $this->assertEquals('./example.txt', $fileObject->getPathname()); 107 | $this->assertEquals('./example.txt', (string)$fileObject); 108 | $this->assertEquals('example.txt', $fileObject->getBasename()); 109 | $this->assertEquals('.', $fileObject->getPath()); 110 | 111 | $fileObject = new \SplFileInfo('../example.txt'); 112 | $this->assertEquals('../example.txt', $fileObject->getPathname()); 113 | $this->assertEquals('../example.txt', (string)$fileObject); 114 | $this->assertEquals('example.txt', $fileObject->getBasename()); 115 | $this->assertEquals('..', $fileObject->getPath()); 116 | 117 | $fileObject = new \SplFileInfo('tmp/example.txt'); 118 | $this->assertEquals('tmp/example.txt', $fileObject->getPathname()); 119 | $this->assertEquals('tmp/example.txt', (string)$fileObject); 120 | $this->assertEquals('example.txt', $fileObject->getBasename()); 121 | $this->assertEquals('tmp', $fileObject->getPath()); 122 | 123 | $fileObject = new \SplFileInfo('/example.txt'); 124 | $this->assertEquals('/example.txt', $fileObject->getPathname()); 125 | $this->assertEquals('/example.txt', (string)$fileObject); 126 | $this->assertEquals('example.txt', $fileObject->getBasename()); 127 | $this->assertEquals('', $fileObject->getPath()); // deviation to File '/' 128 | 129 | $fileObject = new \SplFileInfo('tmp/'); 130 | $this->assertEquals('tmp', $fileObject->getPathname()); 131 | $this->assertEquals('tmp', (string)$fileObject); 132 | $this->assertEquals('tmp', $fileObject->getBasename()); 133 | $this->assertEquals('', $fileObject->getPath()); 134 | 135 | $fileObject = new \SplFileInfo('tmp'); 136 | $this->assertEquals('tmp', $fileObject->getPathname()); 137 | $this->assertEquals('tmp', (string)$fileObject); 138 | $this->assertEquals('tmp', $fileObject->getBasename()); 139 | $this->assertEquals('', $fileObject->getPath()); 140 | 141 | $fileObject = new \SplFileInfo('/foo/bar/'); 142 | $this->assertEquals('/foo/bar', $fileObject->getPathname()); 143 | $this->assertEquals('/foo/bar', (string)$fileObject); 144 | $this->assertEquals('bar', $fileObject->getBasename()); 145 | $this->assertEquals('/foo', $fileObject->getPath()); 146 | 147 | $fileObject = new \SplFileInfo('/foo\bar/'); 148 | $this->assertEquals('/foo\bar', $fileObject->getPathname()); // deviation to File '/foo/bar' 149 | $this->assertEquals('/foo\bar', (string)$fileObject); // deviation to File '/foo/bar' 150 | $this->assertEquals('foo\bar', $fileObject->getBasename()); // deviation to File 'bar' 151 | $this->assertEquals('', $fileObject->getPath()); // deviation to File 'foo' 152 | } 153 | 154 | public function testGetExtension() 155 | { 156 | $fileObject = new File('example.txt'); 157 | $this->assertEquals('txt', $fileObject->getExtension()); 158 | 159 | $fileObject = new File('image.jpeg'); 160 | $this->assertEquals('jpeg', $fileObject->getExtension()); 161 | 162 | $fileObject = new File('.htaccess'); 163 | $this->assertEquals('htaccess', $fileObject->getExtension()); 164 | 165 | $fileObject = new File('/tmp'); 166 | $this->assertEquals('', $fileObject->getExtension()); 167 | 168 | $fileObject = new File('/tmp/example.txt.txt'); 169 | $this->assertEquals('txt', $fileObject->getExtension()); 170 | 171 | 172 | $fileObject = new \SplFileInfo('example.txt'); 173 | $this->assertEquals('txt', $fileObject->getExtension()); 174 | 175 | $fileObject = new \SplFileInfo('image.jpeg'); 176 | $this->assertEquals('jpeg', $fileObject->getExtension()); 177 | 178 | $fileObject = new \SplFileInfo('.htaccess'); 179 | $this->assertEquals('htaccess', $fileObject->getExtension()); 180 | 181 | $fileObject = new \SplFileInfo('/tmp'); 182 | $this->assertEquals('', $fileObject->getExtension()); 183 | 184 | $fileObject = new \SplFileInfo('/tmp/example.txt.txt'); 185 | $this->assertEquals('txt', $fileObject->getExtension()); 186 | } 187 | 188 | public function testGetFilename() 189 | { 190 | $fileObject = new File('example.txt'); 191 | $this->assertEquals('example.txt', $fileObject->getFilename()); 192 | 193 | $fileObject = new File('image.jpeg'); 194 | $this->assertEquals('image.jpeg', $fileObject->getFilename()); 195 | 196 | $fileObject = new File('.htaccess'); 197 | $this->assertEquals('.htaccess', $fileObject->getFilename()); 198 | 199 | $fileObject = new File('/tmp'); 200 | $this->assertEquals('/tmp', $fileObject->getFilename()); 201 | 202 | 203 | $fileObject = new \SplFileInfo('example.txt'); 204 | $this->assertEquals('example.txt', $fileObject->getFilename()); 205 | 206 | $fileObject = new \SplFileInfo('image.jpeg'); 207 | $this->assertEquals('image.jpeg', $fileObject->getFilename()); 208 | 209 | $fileObject = new \SplFileInfo('.htaccess'); 210 | $this->assertEquals('.htaccess', $fileObject->getFilename()); 211 | 212 | $fileObject = new \SplFileInfo('/tmp'); 213 | $this->assertEquals('/tmp', $fileObject->getFilename()); 214 | } 215 | 216 | public function testGetName() 217 | { 218 | $fileObject = new File('example.txt'); 219 | $this->assertEquals('example', $fileObject->getName()); 220 | 221 | $fileObject = new File('image.jpeg'); 222 | $this->assertEquals('image', $fileObject->getName()); 223 | 224 | $fileObject = new File('.htaccess'); 225 | $this->assertEquals('.htaccess', $fileObject->getName()); 226 | 227 | $fileObject = new File('/tmp'); 228 | $this->assertEquals('tmp', $fileObject->getName()); 229 | 230 | $fileObject = new File('/tmp/example.txt.txt'); 231 | $this->assertEquals('example.txt', $fileObject->getName()); 232 | 233 | 234 | 235 | $fileObject = new \SplFileInfo('example.txt'); 236 | $this->assertEquals('example', $fileObject->getBasename('.' . $fileObject->getExtension())); 237 | 238 | $fileObject = new \SplFileInfo('image.jpeg'); 239 | $this->assertEquals('image', $fileObject->getBasename('.' . $fileObject->getExtension())); 240 | 241 | $fileObject = new \SplFileInfo('.htaccess'); 242 | $this->assertEquals('.htaccess', $fileObject->getBasename('.' . $fileObject->getExtension())); 243 | 244 | $fileObject = new \SplFileInfo('/tmp'); 245 | $this->assertEquals('tmp', $fileObject->getBasename('.' . $fileObject->getExtension())); 246 | 247 | $fileObject = new \SplFileInfo('/tmp/example.txt.txt'); 248 | $this->assertEquals('example.txt', $fileObject->getBasename('.' . $fileObject->getExtension())); 249 | } 250 | 251 | public function testAbsolute() 252 | { 253 | // require to that phpunit is executed in vendor/naucon directory 254 | 255 | $fileObject = new File('tests/example.txt'); 256 | $this->assertFalse($fileObject->isAbsolute()); 257 | $this->assertEquals(strtolower(__DIR__) . '/example.txt', strtolower($fileObject->getAbsolutePath())); 258 | 259 | $fileObject = new File(__DIR__ . '/example.txt'); 260 | $this->assertTrue($fileObject->isAbsolute()); 261 | $this->assertEquals(strtolower(__DIR__) . '/example.txt', strtolower($fileObject->getAbsolutePath())); 262 | 263 | $fileObject = new File('tests/'); 264 | $this->assertFalse($fileObject->isAbsolute()); 265 | $this->assertEquals(strtolower(__DIR__), strtolower($fileObject->getAbsolutePath())); 266 | 267 | 268 | 269 | $fileObject = new \SplFileInfo('tests/example.txt'); 270 | $this->assertEquals(strtolower(__DIR__) . '/example.txt', strtolower($fileObject->getRealPath())); 271 | 272 | $fileObject = new \SplFileInfo(__DIR__ . '/example.txt'); 273 | $this->assertEquals(strtolower(__DIR__) . '/example.txt', strtolower($fileObject->getRealPath())); 274 | 275 | $fileObject = new \SplFileInfo('tests/'); 276 | $this->assertEquals(strtolower(__DIR__), strtolower($fileObject->getRealPath())); 277 | } 278 | 279 | public function testGetRealPath() 280 | { 281 | // require to that phpunit is executed in vendor/naucon directory 282 | 283 | $fileObject = new File('tests/example.txt'); 284 | $this->assertEquals(strtolower(__DIR__) . '/example.txt', strtolower($fileObject->getRealPath())); 285 | 286 | $fileObject = new File(__DIR__ . '/example.txt'); 287 | $this->assertEquals(strtolower(__DIR__) . '/example.txt', strtolower($fileObject->getRealPath())); 288 | 289 | $fileObject = new File('tests/'); 290 | $this->assertEquals(strtolower(__DIR__), strtolower($fileObject->getRealPath())); 291 | 292 | 293 | 294 | $fileObject = new \SplFileInfo('tests/example.txt'); 295 | $this->assertEquals(strtolower(__DIR__) . '/example.txt', strtolower($fileObject->getRealPath())); 296 | 297 | $fileObject = new \SplFileInfo(__DIR__ . '/example.txt'); 298 | $this->assertEquals(strtolower(__DIR__) . '/example.txt', strtolower($fileObject->getRealPath())); 299 | 300 | $fileObject = new \SplFileInfo('tests/'); 301 | $this->assertEquals(strtolower(__DIR__), strtolower($fileObject->getRealPath())); 302 | } 303 | 304 | public function testParent() 305 | { 306 | $fileObject = new File('tests/example.txt'); 307 | $parentFileObject = $fileObject->getParent(); 308 | $this->assertInstanceOf('Naucon\File\FileInterface', $parentFileObject); 309 | $this->assertEquals(strtolower(__DIR__), strtolower($parentFileObject->getAbsolutePath())); 310 | 311 | $fileObject = new File(__DIR__ . '/example.txt'); 312 | $parentFileObject = $fileObject->getParent(); 313 | $this->assertInstanceOf('Naucon\File\FileInterface', $parentFileObject); 314 | $this->assertEquals(strtolower(__DIR__), strtolower($parentFileObject->getAbsolutePath())); 315 | 316 | $fileObject = new File('example.txt'); 317 | $parentFileObject = $fileObject->getParent(); 318 | $this->assertEquals('.', $parentFileObject->getPathname()); 319 | 320 | $fileObject = new File('tests/'); 321 | $parentFileObject = $fileObject->getParent(); 322 | $this->assertInstanceOf('Naucon\File\FileInterface', $parentFileObject); 323 | $this->assertEquals(strtolower(realpath(__DIR__.'/../')), strtolower($parentFileObject->getAbsolutePath())); 324 | } 325 | 326 | public function testExist() 327 | { 328 | $path = __DIR__; 329 | $filePath = __DIR__ . '/example.txt'; 330 | $fileObject = new File($filePath); 331 | $this->assertEquals($filePath, $fileObject->getPathname()); 332 | $this->assertTrue($fileObject->exists()); 333 | 334 | $fileObject = new File($path); 335 | $this->assertEquals($path, $fileObject->getPathname()); 336 | $this->assertTrue($fileObject->exists()); 337 | 338 | 339 | $fileObject = new \SplFileInfo($filePath); 340 | $this->assertEquals($filePath, $fileObject->getPathname()); 341 | $this->assertTrue($fileObject->isFile()); // SplFileInfo has no exist method 342 | 343 | $fileObject = new \SplFileInfo($path); 344 | $this->assertEquals($path, $fileObject->getPathname()); 345 | $this->assertTrue($fileObject->isDir()); // SplFileInfo has no exist method 346 | } 347 | 348 | /** 349 | * @depends testExist 350 | */ 351 | public function testIsExecutable() 352 | { 353 | $filePath = __DIR__ . '/example.txt'; 354 | $fileObject = new File($filePath); 355 | $result = $fileObject->isExecutable(); 356 | 357 | 358 | $fileObject = new \SplFileInfo($filePath); 359 | $this->assertEquals($result, $fileObject->isExecutable()); 360 | } 361 | 362 | /** 363 | * @depends testExist 364 | */ 365 | public function testIsReadable() 366 | { 367 | $filePath = __DIR__ . '/example.txt'; 368 | $fileObject = new File($filePath); 369 | $this->assertTrue($fileObject->isReadable()); 370 | 371 | 372 | $fileObject = new \SplFileInfo($filePath); 373 | $this->assertTrue($fileObject->isReadable()); 374 | } 375 | 376 | /** 377 | * @depends testExist 378 | */ 379 | public function testIsWritable() 380 | { 381 | $filePath = __DIR__ . '/example.txt'; 382 | $fileObject = new File($filePath); 383 | $this->assertTrue($fileObject->isWritable()); 384 | 385 | 386 | $fileObject = new \SplFileInfo($filePath); 387 | $this->assertTrue($fileObject->isWritable()); 388 | } 389 | 390 | /** 391 | * @depends testExist 392 | */ 393 | public function testIsFile() 394 | { 395 | $path = __DIR__; 396 | $filePath = $path . '/example.txt'; 397 | 398 | $fileObject = new File($filePath); 399 | $this->assertTrue($fileObject->isFile()); 400 | 401 | $fileObject = new File($path); 402 | $this->assertFalse($fileObject->isFile()); 403 | 404 | 405 | $fileObject = new \SplFileInfo($filePath); 406 | $this->assertTrue($fileObject->isFile()); 407 | 408 | $fileObject = new \SplFileInfo($path); 409 | $this->assertFalse($fileObject->isFile()); 410 | } 411 | 412 | /** 413 | * @depends testExist 414 | */ 415 | public function testIsDir() 416 | { 417 | $path = __DIR__; 418 | $filePath = $path . '/example.txt'; 419 | 420 | $fileObject = new File($filePath); 421 | $this->assertFalse($fileObject->isDirectory()); 422 | $this->assertFalse($fileObject->isDir()); 423 | 424 | $fileObject = new File($path); 425 | $this->assertTrue($fileObject->isDirectory()); 426 | $this->assertTrue($fileObject->isDir()); 427 | 428 | 429 | $fileObject = new \SplFileInfo($filePath); 430 | $this->assertFalse($fileObject->isDir()); 431 | 432 | $fileObject = new \SplFileInfo($path); 433 | $this->assertTrue($fileObject->isDir()); 434 | } 435 | 436 | /** 437 | * @depends testExist 438 | */ 439 | public function testIsLink() 440 | { 441 | $path = __DIR__; 442 | $filePath = $path . '/example.txt'; 443 | 444 | $fileObject = new File($filePath); 445 | $this->assertFalse($fileObject->isLink()); 446 | 447 | $fileObject = new File($path); 448 | $this->assertFalse($fileObject->isLink()); 449 | 450 | 451 | $fileObject = new \SplFileInfo($filePath); 452 | $this->assertFalse($fileObject->isLink()); 453 | 454 | $fileObject = new \SplFileInfo($path); 455 | $this->assertFalse($fileObject->isLink()); 456 | } 457 | 458 | public function testIsHidden() 459 | { 460 | $fileObject = new File('example.txt'); 461 | $this->assertEquals('txt', $fileObject->getExtension()); 462 | $this->assertFalse($fileObject->isHidden()); 463 | 464 | $fileObject = new File('.htaccess'); 465 | $this->assertEquals('htaccess', $fileObject->getExtension()); 466 | $this->assertTrue($fileObject->isHidden()); 467 | } 468 | 469 | /** 470 | * @depends testExist 471 | */ 472 | public function testLastModified() 473 | { 474 | $filePath = __DIR__ . '/example.txt'; 475 | $fileObject = new File($filePath); 476 | $this->assertGreaterThanOrEqual(0, $fileObject->getMTime()); 477 | $this->assertInstanceOf('DateTime', $fileObject->lastModified()); 478 | 479 | 480 | $fileObject = new \SplFileInfo($filePath); 481 | $this->assertGreaterThanOrEqual(0, $fileObject->getMTime()); 482 | } 483 | 484 | /** 485 | * @depends testExist 486 | */ 487 | public function testLastAccessed() 488 | { 489 | $filePath = __DIR__ . '/example.txt'; 490 | $fileObject = new File($filePath); 491 | $this->assertGreaterThanOrEqual(0, $fileObject->getATime()); 492 | $this->assertInstanceOf('DateTime', $fileObject->lastAccessed()); 493 | 494 | 495 | $fileObject = new \SplFileInfo($filePath); 496 | $this->assertGreaterThanOrEqual(0, $fileObject->getATime()); 497 | } 498 | 499 | /** 500 | * @depends testExist 501 | */ 502 | public function testLastChanged() 503 | { 504 | $filePath = __DIR__ . '/example.txt'; 505 | $fileObject = new File($filePath); 506 | $this->assertGreaterThanOrEqual(0, $fileObject->getCTime()); 507 | $this->assertInstanceOf('DateTime', $fileObject->lastChanged()); 508 | 509 | 510 | $fileObject = new \SplFileInfo($filePath); 511 | $this->assertGreaterThanOrEqual(0, $fileObject->getCTime()); 512 | } 513 | 514 | /** 515 | * @depends testExist 516 | */ 517 | public function testTouch() 518 | { 519 | // I did not compare access and modification time to defined values 520 | // because it looks like that this file attributes are cached in php 521 | // because they did not change until you call the script again. 522 | // beyond that i feared problem with windows. 523 | // TODO try clearstatcache() 524 | 525 | // create file 526 | $createFiles = array( 527 | __DIR__ . '/tmp/example_touch1.txt', 528 | __DIR__ . '/tmp/example_touch2.txt', 529 | __DIR__ . '/tmp/example_touch3.txt', 530 | __DIR__ . '/tmp/example_touch4.txt' 531 | ); 532 | foreach ($createFiles as $createFile) { 533 | if (!is_file($createFile)) { 534 | fclose(fopen($createFile, 'x')); 535 | } 536 | } 537 | 538 | $filePath = __DIR__ . '/tmp/example_touch1.txt'; 539 | $fileObject = new File($filePath); 540 | $this->assertTrue($fileObject->touch()); 541 | $this->assertGreaterThanOrEqual(0, $fileObject->getMTime()); 542 | $this->assertGreaterThanOrEqual(0, $fileObject->getATime()); 543 | 544 | $filePath = __DIR__ . '/tmp/example_touch2.txt'; 545 | $modificationTime = time()-3600; // current time -1 hour 546 | $fileObject = new File($filePath); 547 | $this->assertTrue($fileObject->touch($modificationTime)); 548 | $this->assertGreaterThanOrEqual(0, $fileObject->getMTime()); 549 | $this->assertGreaterThanOrEqual(0, $fileObject->getATime()); 550 | 551 | $filePath = __DIR__ . '/tmp/example_touch3.txt'; 552 | $accessTime = time()-7200; // current time -2 hour 553 | $fileObject = new File($filePath); 554 | $this->assertTrue($fileObject->touch(null,$accessTime)); 555 | $this->assertGreaterThanOrEqual(0, $fileObject->getMTime()); 556 | $this->assertGreaterThanOrEqual(0, $fileObject->getATime()); 557 | 558 | $filePath = __DIR__ . '/tmp/example_touch4.txt'; 559 | $modificationTime = time()-42200; // current time -12 hour 560 | $accessTime = time()-36000; // current time -10 hour 561 | $fileObject = new File($filePath); 562 | $this->assertTrue($fileObject->touch($modificationTime,$accessTime)); 563 | $this->assertGreaterThanOrEqual(0, $fileObject->getMTime()); 564 | $this->assertGreaterThanOrEqual(0, $fileObject->getATime()); 565 | } 566 | 567 | /** 568 | * @depends testExist 569 | */ 570 | public function testCreateNewFile() 571 | { 572 | $pathname = __DIR__ . '/tmp/new_file.txt'; 573 | 574 | if (is_file($pathname)) { 575 | unlink($pathname); 576 | } 577 | $fileObject = new File($pathname); 578 | $this->assertTrue($fileObject->createNewFile()); 579 | $this->assertTrue($fileObject->isFile()); 580 | } 581 | 582 | /** 583 | * @depends testExist 584 | */ 585 | public function testMkdir() 586 | { 587 | $filePath = __DIR__ . '/tmp/foo'; 588 | 589 | if (is_dir($filePath)) { 590 | $this->markTestSkipped(); 591 | } else { 592 | $fileObject = new File($filePath); 593 | $this->assertTrue($fileObject->mkdir()); 594 | $this->assertTrue($fileObject->isReadable()); 595 | $this->assertTrue($fileObject->isWritable()); 596 | 597 | $mode = 0777; 598 | $filePath = __DIR__ . '/tmp/foo/lv1/'; 599 | $fileObject = new File($filePath); 600 | $this->assertTrue($fileObject->mkdir($mode)); 601 | $this->assertTrue($fileObject->isReadable()); 602 | $this->assertTrue($fileObject->isWritable()); 603 | } 604 | } 605 | 606 | /** 607 | * @depends testExist 608 | */ 609 | public function testMkdirs() 610 | { 611 | $filePath = __DIR__ . '/tmp/bar/lv1/'; 612 | if (is_dir($filePath)) { 613 | $this->markTestSkipped(); 614 | } else { 615 | $fileObject = new File($filePath); 616 | $this->assertTrue($fileObject->mkdirs()); 617 | $this->assertTrue($fileObject->isReadable()); 618 | $this->assertTrue($fileObject->isWritable()); 619 | 620 | $mode = 0777; 621 | $filePath = __DIR__ . '/tmp/bar/lv1/lv2/lv3/'; 622 | $fileObject = new File($filePath); 623 | $this->assertTrue($fileObject->mkdirs($mode)); 624 | $this->assertTrue($fileObject->isReadable()); 625 | $this->assertTrue($fileObject->isWritable()); 626 | } 627 | } 628 | 629 | /** 630 | * @depends testExist 631 | */ 632 | public function testDelete() 633 | { 634 | $filePath = __DIR__ . '/tmp/example_delete.txt'; 635 | 636 | if (!is_file($filePath)) { 637 | // create file 638 | fclose(fopen($filePath, 'x')); 639 | } 640 | 641 | $fileObject = new File($filePath); 642 | $this->assertTrue($fileObject->isFile()); 643 | $this->assertTrue($fileObject->isWritable()); 644 | $this->assertTrue($fileObject->delete()); 645 | 646 | 647 | $filePath = __DIR__ . '/tmp/example_delete/'; 648 | 649 | if (!is_dir($filePath)) { 650 | // create directory 651 | mkdir($filePath,0777,true); 652 | } 653 | 654 | $fileObject = new File($filePath); 655 | $this->assertTrue($fileObject->isDir()); 656 | $this->assertTrue($fileObject->isWritable()); 657 | $this->assertTrue($fileObject->delete()); 658 | } 659 | 660 | /** 661 | * @depends testExist 662 | * @depends testMkdir 663 | * @depends testMkdirs 664 | */ 665 | public function testDeleteAll() 666 | { 667 | $filePath = __DIR__ . '/tmp/example_delete.txt'; 668 | 669 | if (!is_file($filePath)) { 670 | // create file 671 | fclose(fopen($filePath, 'x')); 672 | } 673 | 674 | $fileObject = new File($filePath); 675 | $this->assertTrue($fileObject->isFile()); 676 | $this->assertTrue($fileObject->isWritable()); 677 | $this->assertTrue($fileObject->deleteAll()); 678 | 679 | $filePath = __DIR__ . '/tmp/foo/'; 680 | 681 | if (!is_dir($filePath)) { 682 | // create directories 683 | mkdir($filePath,0777,true); 684 | } 685 | 686 | $fileObject = new File($filePath); 687 | $this->assertTrue($fileObject->isDir()); 688 | $this->assertTrue($fileObject->isWritable()); 689 | $this->assertTrue($fileObject->deleteAll()); 690 | 691 | $filePath = __DIR__ . '/tmp/bar/'; 692 | 693 | if (!is_dir($filePath)) { 694 | // create directories 695 | mkdir($filePath,0777,true); 696 | } 697 | 698 | $fileObject = new File($filePath); 699 | $this->assertTrue($fileObject->isDir()); 700 | $this->assertTrue($fileObject->isWritable()); 701 | $this->assertTrue($fileObject->deleteAll()); 702 | } 703 | 704 | /** 705 | * @depends testDeleteAll 706 | */ 707 | public function testDeleteFiles() 708 | { 709 | $filePath = __DIR__ . '/tmp/foo2'; 710 | 711 | // create directories 712 | $createDirs = array( 713 | $filePath, 714 | $filePath . '/bar2', 715 | $filePath . '/bar3', 716 | ); 717 | foreach ($createDirs as $createDir) { 718 | if (!is_dir($createDir)) { 719 | mkdir($createDir,0777,true); 720 | } 721 | } 722 | 723 | // create file 724 | $createFiles = array( 725 | $filePath . '/example_file_delete1.txt', 726 | $filePath . '/example_file_delete2.txt', 727 | $filePath . '/bar2/example_file_delete3.txt', 728 | $filePath . '/bar2/example_file_delete4.txt', 729 | $filePath . '/bar3/example_file_delete5.txt', 730 | $filePath . '/bar3/example_file_delete6.txt' 731 | ); 732 | foreach ($createFiles as $createFile) { 733 | if (!is_file($createFile)) { 734 | fclose(fopen($createFile, 'x')); 735 | } 736 | } 737 | 738 | $fileObject = new File($filePath); 739 | $this->assertTrue($fileObject->isDir()); 740 | $this->assertTrue($fileObject->isWritable()); 741 | $this->assertTrue($fileObject->deleteFiles()); 742 | 743 | $subPaths = array_diff(scandir($filePath), array('..', '.')); 744 | $this->assertEquals(2, count($subPaths)); 745 | } 746 | 747 | /** 748 | * @depends testDeleteFiles 749 | */ 750 | public function testDeleteAllFiles() 751 | { 752 | $filePath = __DIR__ . '/tmp/foo2'; 753 | 754 | $fileObject = new File($filePath); 755 | $this->assertTrue($fileObject->isDir()); 756 | $this->assertTrue($fileObject->isWritable()); 757 | $this->assertTrue($fileObject->deleteAllFiles()); 758 | 759 | $subPaths = array_diff(scandir($filePath.'/bar2'), array('..', '.')); 760 | $this->assertEquals(0, count($subPaths)); 761 | $subPaths = array_diff(scandir($filePath.'/bar3'), array('..', '.')); 762 | $this->assertEquals(0, count($subPaths)); 763 | } 764 | 765 | /** 766 | * @depends testExist 767 | */ 768 | public function testRename() 769 | { 770 | // create file 771 | $filePath = __DIR__ . '/tmp/example_rename_old.txt'; 772 | if (!is_file($filePath)) { 773 | fclose(fopen($filePath, 'x')); 774 | } 775 | // remove renamed file 776 | $filePathNew = __DIR__ . '/tmp/example_rename_new.txt'; 777 | if (is_file($filePathNew)) { 778 | unlink($filePathNew); 779 | } 780 | 781 | $fileObject = new File($filePath); 782 | $this->assertTrue($fileObject->isFile()); 783 | $this->assertTrue($fileObject->isWritable()); 784 | $this->assertTrue($fileObject->rename('example_rename_new.txt')); 785 | $this->assertEquals($filePathNew, $fileObject->getPathname()); 786 | $this->assertTrue($fileObject->isFile()); 787 | 788 | 789 | // create dir 790 | $filePath = __DIR__ . '/tmp/ExampleRenameOld'; 791 | if (!is_dir($filePath)) { 792 | mkdir($filePath,0777); 793 | } 794 | // remove renamed file 795 | $filePathNew = __DIR__ . '/tmp/ExampleRenameNew'; 796 | if (is_dir($filePathNew)) { 797 | rmdir($filePathNew); 798 | } 799 | 800 | $fileObject = new File($filePath); 801 | $this->assertTrue($fileObject->isDir()); 802 | $this->assertTrue($fileObject->isWritable()); 803 | $this->assertTrue($fileObject->rename('ExampleRenameNew')); 804 | $this->assertEquals($filePathNew, $fileObject->getPathname()); 805 | $this->assertTrue($fileObject->isDir()); 806 | } 807 | 808 | /** 809 | * @depends testExist 810 | */ 811 | public function testMove() 812 | { 813 | // create file 814 | $filePath = __DIR__ . '/tmp/example_move.txt'; 815 | if (!is_file($filePath)) { 816 | fclose(fopen($filePath, 'x')); 817 | } 818 | // create target dir 819 | $newPath = __DIR__ . '/tmp/target'; 820 | if (!is_dir($newPath)) { 821 | mkdir($newPath,0777); 822 | } 823 | // remove moved file 824 | $filePathNew = $newPath . '/example_move.txt'; 825 | if (is_file($filePathNew)) { 826 | unlink($filePathNew); 827 | } 828 | 829 | $fileObject = new File($filePath); 830 | $this->assertTrue($fileObject->isFile()); 831 | $this->assertTrue($fileObject->isWritable()); 832 | $this->assertTrue($fileObject->move($newPath)); 833 | $this->assertEquals($filePathNew, $fileObject->getPathname()); 834 | $this->assertTrue($fileObject->isFile()); 835 | 836 | 837 | // create dir 838 | $filePath = __DIR__ . '/tmp/ExampleMove'; 839 | if (!is_dir($filePath)) { 840 | mkdir($filePath,0777); 841 | } 842 | // remove moved file 843 | $filePathNew = $newPath . '/ExampleMove'; 844 | if (is_dir($filePathNew)) { 845 | rmdir($filePathNew); 846 | } 847 | 848 | $fileObject = new File($filePath); 849 | $this->assertTrue($fileObject->isDir()); 850 | $this->assertTrue($fileObject->isWritable()); 851 | $this->assertTrue($fileObject->move($newPath)); 852 | $this->assertEquals($filePathNew, $fileObject->getPathname()); 853 | $this->assertTrue($fileObject->isDir()); 854 | } 855 | 856 | /** 857 | * @depends testExist 858 | */ 859 | public function testCopy() 860 | { 861 | // create file 862 | $filePath = __DIR__ . '/tmp/example_copy.txt'; 863 | if (!is_file($filePath)) { 864 | fclose(fopen($filePath, 'x')); 865 | } 866 | // create target dir 867 | $newPath = __DIR__ . '/tmp/target'; 868 | if (!is_dir($newPath)) { 869 | mkdir($newPath,0777); 870 | } 871 | // remove copied file 872 | $filePathNew = $newPath . '/example_copy.txt'; 873 | if (is_file($filePathNew)) { 874 | unlink($filePathNew); 875 | } 876 | 877 | $fileObject = new File($filePath); 878 | $this->assertTrue($fileObject->isFile()); 879 | $this->assertTrue($fileObject->isWritable()); 880 | $this->assertTrue($fileObject->copy($newPath)); 881 | $this->assertEquals($filePathNew, $fileObject->getPathname()); 882 | $this->assertTrue($fileObject->isFile()); 883 | 884 | 885 | // create dir 886 | $filePath = __DIR__ . '/tmp/ExampleCopy'; 887 | if (!is_dir($filePath)) { 888 | mkdir($filePath,0777); 889 | } 890 | // remove copied file 891 | $filePathNew = $newPath . '/ExampleCopy'; 892 | if (is_dir($filePathNew)) { 893 | rmdir($filePathNew); 894 | } 895 | 896 | $fileObject = new File($filePath); 897 | $this->assertTrue($fileObject->isDir()); 898 | $this->assertTrue($fileObject->isWritable()); 899 | $this->assertTrue($fileObject->copy($newPath)); 900 | $this->assertEquals($filePathNew, $fileObject->getPathname()); 901 | $this->assertTrue($fileObject->isDir()); 902 | } 903 | 904 | /** 905 | * @depends testExist 906 | */ 907 | public function testOwner() 908 | { 909 | $filePath = __DIR__ . '/example.txt'; 910 | $fileObject = new File($filePath); 911 | $this->assertGreaterThanOrEqual(0, $fileObject->getOwner()); 912 | $this->assertGreaterThan(0, strlen($fileObject->getOwnerName())); 913 | 914 | 915 | $fileObject = new \SplFileInfo($filePath); 916 | $this->assertGreaterThanOrEqual(0, $fileObject->getOwner()); 917 | } 918 | 919 | /** 920 | * @depends testExist 921 | */ 922 | public function testGroup() 923 | { 924 | $filePath = __DIR__ . '/example.txt'; 925 | $fileObject = new File($filePath); 926 | $this->assertGreaterThanOrEqual(0, $fileObject->getGroup()); 927 | $this->assertGreaterThanOrEqual(0, strlen($fileObject->getGroupName())); 928 | 929 | $fileObject = new \SplFileInfo($filePath); 930 | $this->assertGreaterThanOrEqual(0, $fileObject->getGroup()); 931 | } 932 | 933 | /** 934 | * @depends testExist 935 | */ 936 | public function testPermission() 937 | { 938 | $filePath = __DIR__ . '/example.txt'; 939 | $fileObject = new File($filePath); 940 | $this->assertGreaterThan(0, $fileObject->getPermission()); 941 | $this->assertEquals(4, strlen($fileObject->getPermission())); 942 | 943 | 944 | $fileObject = new \SplFileInfo($filePath); 945 | $this->assertGreaterThan(0, $fileObject->getPerms()); 946 | $this->assertEquals(4, strlen( substr(decoct($fileObject->getPerms()),2) ) ); // deviation 947 | } 948 | 949 | /** 950 | * @depends testExist 951 | */ 952 | public function testChgrp() 953 | { 954 | $filePath = __DIR__ . '/example.txt'; 955 | $fileObject = new File($filePath); 956 | $this->assertGreaterThanOrEqual(0, $userGroupId = $fileObject->getGroup()); 957 | $this->assertGreaterThanOrEqual(0, strlen($userGroup = $fileObject->getGroupName())); 958 | 959 | if ($userGroupId > 0) { 960 | $this->assertTrue($fileObject->chgrp($userGroupId)); 961 | $this->assertTrue($fileObject->chgrp($userGroup)); 962 | 963 | $this->assertEquals($userGroupId, $fileObject->getGroup()); 964 | $this->assertEquals($userGroup, $fileObject->getGroupName()); 965 | } 966 | } 967 | 968 | /** 969 | * @depends testExist 970 | */ 971 | public function testChown() 972 | { 973 | $filePath = __DIR__ . '/example.txt'; 974 | $fileObject = new File($filePath); 975 | $this->assertGreaterThanOrEqual(0, $userId = $fileObject->getOwner()); 976 | $this->assertGreaterThanOrEqual(0, strlen($user = $fileObject->getOwnerName())); 977 | 978 | $this->assertTrue($fileObject->chown($userId)); 979 | $this->assertTrue($fileObject->chown($user)); 980 | 981 | $this->assertEquals($userId, $fileObject->getOwner()); 982 | $this->assertEquals($user, $fileObject->getOwnerName()); 983 | } 984 | 985 | /** 986 | * @depends testExist 987 | */ 988 | public function testChmod() 989 | { 990 | $filePath = __DIR__ . '/example.txt'; 991 | $fileObject = new File($filePath); 992 | $this->assertGreaterThan(0, $mode = $fileObject->getPermission()); 993 | $this->assertEquals(4, strlen($mode)); 994 | $this->assertTrue($fileObject->chmod($mode)); 995 | $fileObject->flush(); // clear file status cache 996 | $this->assertEquals($mode, $fileObject->getPermission()); 997 | } 998 | 999 | /** 1000 | * @depends testExist 1001 | */ 1002 | public function testChmodWithAllModes() 1003 | { 1004 | $modes = array( 1005 | '0600', // file owner read and write; 1006 | '0640', // file owner read and write; owner group read 1007 | '0660', // file owner read and write; owner group read and write 1008 | '0604', // file owner read and write; everbody read 1009 | '0606', // file owner read and write; everbody read and write 1010 | '0664', // file owner read and write; owner group read and write; everbody read 1011 | '0666', // file owner read and write; owner group read and write; everbody read and write 1012 | '0700', // file owner read, execute and write; 1013 | '0740', // file owner read, execute and write; owner group read 1014 | '0760', // file owner read, execute and write; owner group read and write 1015 | '0770', // file owner read, execute and write; owner group read, execute and write 1016 | '0704', // file owner read, execute and write; everbody read 1017 | '0706', // file owner read, execute and write; everbody read and write 1018 | '0707', // file owner read, execute and write; everbody read, execute and write 1019 | '0744', // file owner read, execute and write; owner group read; everbody read 1020 | '0746', // file owner read, execute and write; owner group read; everbody read and write 1021 | '0747', // file owner read, execute and write; owner group read; everbody read, execute and write 1022 | '0754', // file owner read, execute and write; owner group read and execute; everbody read 1023 | '0755', // file owner read, execute and write; owner group read and execute; everbody read and execute 1024 | '0756', // file owner read, execute and write; owner group read and execute; everbody read and write 1025 | '0757', // file owner read, execute and write; owner group read and execute; everbody read, execute and write 1026 | '0764', // file owner read, execute and write; owner group read and write; everbody read 1027 | '0766', // file owner read, execute and write; owner group read and write; everbody read and write 1028 | '0767', // file owner read, execute and write; owner group read and write; everbody read, execute and write 1029 | '0774', // file owner read, execute and write; owner group read, execute and write; everbody read 1030 | '0775', // file owner read, execute and write; owner group read, execute and write; everbody read, execute and write 1031 | '0776', // file owner read, execute and write; owner group read, execute and write; everbody read and write 1032 | '0777', // file owner read, execute and write; owner group read, execute and write; everbody read, execute and write 1033 | ); 1034 | 1035 | $filePath = __DIR__ . '/example.txt'; 1036 | $fileObject = new File($filePath); 1037 | foreach ($modes as $mode) { 1038 | $this->assertTrue($fileObject->chmod($mode)); 1039 | clearstatcache(); 1040 | $this->assertEquals($mode, $fileObject->getPermission()); 1041 | } 1042 | } 1043 | 1044 | /** 1045 | * @depends testExist 1046 | */ 1047 | public function testSize() 1048 | { 1049 | $filePath = __DIR__ . '/example.txt'; 1050 | $fileObject = new File($filePath); 1051 | $this->assertGreaterThan(0, $size = $fileObject->getSize()); 1052 | 1053 | 1054 | $fileObject = new \SplFileInfo($filePath); 1055 | $this->assertGreaterThan(0, $fileObject->getSize()); 1056 | $this->assertEquals($size, $fileObject->getSize()); 1057 | } 1058 | 1059 | /** 1060 | * @depends testExist 1061 | */ 1062 | public function testListAll() 1063 | { 1064 | $examplePath = __DIR__ . '/ExampleDir'; 1065 | 1066 | $filesObject = new File($examplePath); 1067 | 1068 | $expectedFiles = array( 1069 | $examplePath . '/ExampleChildDir', 1070 | $examplePath . '/testFile1.txt', 1071 | $examplePath . '/testFile2.txt' 1072 | ); 1073 | 1074 | $i = 0; 1075 | foreach ($filesObject->listAll() as $fileObject) { 1076 | $this->assertInstanceOf('Naucon\File\File', $fileObject); 1077 | $this->assertContains($fileObject->getPathname(), $expectedFiles); 1078 | $i++; 1079 | } 1080 | 1081 | $this->assertEquals(3, $i); 1082 | } 1083 | 1084 | /** 1085 | * @depends testExist 1086 | */ 1087 | public function testListFiles() 1088 | { 1089 | $examplePath = __DIR__ . '/ExampleDir'; 1090 | 1091 | $filesObject = new File($examplePath); 1092 | 1093 | $expectedFiles = array( 1094 | $examplePath . '/testFile1.txt', 1095 | $examplePath . '/testFile2.txt' 1096 | ); 1097 | 1098 | $i = 0; 1099 | foreach ($filesObject->listFiles() as $fileObject) { 1100 | $this->assertInstanceOf('Naucon\File\File', $fileObject); 1101 | $this->assertContains($fileObject->getPathname(), $expectedFiles); 1102 | $i++; 1103 | } 1104 | 1105 | $this->assertEquals(2, $i); 1106 | } 1107 | } -------------------------------------------------------------------------------- /tests/FileWriterTest.php: -------------------------------------------------------------------------------- 1 | assertEquals($filePath, $fileObject->getPathname()); 21 | $this->assertTrue($fileObject->isWritable()); 22 | 23 | $filePath = __DIR__ . '/example_write2.txt'; 24 | $fileObject = new \SplFileObject($filePath, 'r+'); 25 | $this->assertEquals($filePath, $fileObject->getPathname()); 26 | $this->assertTrue($fileObject->isWritable()); 27 | } 28 | 29 | /** 30 | * @return FileWriter 31 | * @throws \Naucon\File\Exception\FileWriterException 32 | */ 33 | public function testWrite() 34 | { 35 | $string = 'Line01'.PHP_EOL; 36 | $string.= 'Line02'.PHP_EOL; 37 | $string.= 'Line03'.PHP_EOL; 38 | $string.= 'Line04'.PHP_EOL; 39 | 40 | 41 | $filePath = __DIR__ . '/example_write1.txt'; 42 | $fileObject1 = new FileWriter($filePath,'w+'); 43 | $fileObject1->write($string); 44 | 45 | $filePath = __DIR__ . '/example_write2.txt'; 46 | $fileObject2 = new \SplFileObject($filePath, 'w+'); 47 | $fileObject2->fwrite($string); 48 | 49 | return $fileObject1; 50 | } 51 | 52 | /** 53 | * @depends testWrite 54 | * @param FileWriter $fileObject1 55 | */ 56 | public function testIterate($fileObject1) 57 | { 58 | $lines = array( 59 | 'Line01', 'Line02', 'Line03', 'Line04', '' 60 | ); 61 | 62 | 63 | $i = 0; 64 | foreach($fileObject1 as $line) { 65 | $this->assertContains($line, $lines); 66 | $i++; 67 | } 68 | $this->assertEquals(5, $i); 69 | } 70 | 71 | public function testWriteLine() 72 | { 73 | $filePath = __DIR__ . '/example_write1.txt'; 74 | $fileObject = new FileWriter($filePath,'a+'); 75 | $fileObject->writeLine("foo"); 76 | $fileObject->writeLine("bar"); 77 | 78 | $fileObject->writeLine(" some string " . PHP_EOL . "with line breaks \n\r".PHP_EOL); 79 | 80 | $filePath = __DIR__ . '/example_write2.txt'; 81 | $fileObject = new \SplFileObject($filePath, 'a+'); 82 | $fileObject->fwrite("foo" . PHP_EOL); 83 | $fileObject->fwrite("bar" . PHP_EOL); 84 | } 85 | 86 | public function testTruncates() 87 | { 88 | $filePath = __DIR__ . '/example_write1.txt'; 89 | $fileObject = new FileWriter($filePath,'a+'); 90 | $fileObject->truncates(13); 91 | 92 | $filePath = __DIR__ . '/example_write2.txt'; 93 | $fileObject = new \SplFileObject($filePath, 'a+'); 94 | $fileObject->ftruncate(13); 95 | } 96 | 97 | public function testClear() 98 | { 99 | $filePath = __DIR__ . '/example_write1.txt'; 100 | $fileObject = new FileWriter($filePath,'a+'); 101 | $fileObject->clear(); 102 | 103 | $filePath = __DIR__ . '/example_write2.txt'; 104 | $fileObject = new \SplFileObject($filePath, 'a+'); 105 | $fileObject->ftruncate(0); 106 | } 107 | } -------------------------------------------------------------------------------- /tests/LockTest.php: -------------------------------------------------------------------------------- 1 | assertFalse($lockObject->isLocked()); 57 | 58 | // create lock file "~foo.lock" 59 | $this->assertTrue($lockObject->lock()); 60 | 61 | $this->assertTrue($lockObject->isLocked()); 62 | return $lockObject; 63 | } 64 | 65 | /** 66 | * @depends testLock 67 | * @param LockInterface $lockObject 68 | */ 69 | public function testUnlock(LockInterface $lockObject) 70 | { 71 | // delete lock file "~foo.lock" 72 | $this->assertTrue($lockObject->unlock()); 73 | } 74 | 75 | /** 76 | * @depends testUnlock 77 | * @expectedException \Naucon\File\Exception\LockHandlerException 78 | */ 79 | public function testAlreadyLocked() 80 | { 81 | $firstLock = new Lock('foo'); 82 | // create lock file "~foo.lock" 83 | $this->assertTrue($firstLock->lock()); 84 | 85 | $secondLock = new Lock('foo'); 86 | // try to lock file "~foo.lock" again 87 | $this->assertFalse($secondLock->lock()); 88 | 89 | $this->assertTrue($firstLock->unlock()); // delete lock file "~foo.lock" 90 | 91 | $this->assertTrue($secondLock->lock()); // create lock file "~foo.lock" again 92 | $this->assertTrue($secondLock->unlock()); // delete lock file "~foo.lock" again 93 | } 94 | } -------------------------------------------------------------------------------- /tests/example.txt: -------------------------------------------------------------------------------- 1 | 1234567890 -------------------------------------------------------------------------------- /tests/example_read.txt: -------------------------------------------------------------------------------- 1 | Line01 2 | Line02 3 | Line03 4 | Line04 5 | Line05 6 | Line06 7 | Line07 8 | Line08 9 | Line09 10 | -------------------------------------------------------------------------------- /tests/example_write1.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/naucon/File/447d4e9d3e645192e996bde8f3d91669a6b4f996/tests/example_write1.txt -------------------------------------------------------------------------------- /tests/example_write2.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/naucon/File/447d4e9d3e645192e996bde8f3d91669a6b4f996/tests/example_write2.txt -------------------------------------------------------------------------------- /tests/lock/.gitignore: -------------------------------------------------------------------------------- 1 | * -------------------------------------------------------------------------------- /tests/tmp/.gitignore: -------------------------------------------------------------------------------- 1 | * --------------------------------------------------------------------------------