Changeset 313 for trunk/classes
- Timestamp:
- 11/17/07 04:20:58 (1 year ago)
- Files:
-
- trunk/classes/phing/system/io/File.php (modified) (42 diffs)
- trunk/classes/phing/system/io/FileSystem.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/classes/phing/system/io/File.php
r312 r313 31 31 class File { 32 32 33 /** separator string, static, obtained from FileSystem */ 33 /** 34 * Separator string, static, obtained from FileSystem. 35 * @see FileSystem::getSeparator() 36 */ 34 37 public static $separator; 35 38 36 /** path separator string, static, obtained from FileSystem (; or :)*/ 39 /** 40 * Path separator string, static, obtained from FileSystem (; or :) 41 * @see FileSystem::getSeparator() 42 */ 37 43 public static $pathSeparator; 38 44 … … 41 47 * pathname string uses the default name-separator character and does not 42 48 * contain any duplicate or redundant separators. 43 */ 44 private $path = null; 45 46 /** The length of this abstract pathname's prefix, or zero if it has no prefix. */ 49 * @var string 50 */ 51 private $path; 52 53 /** 54 * The length of this abstract pathname's prefix, or zero if it has no prefix. 55 * @var int 56 */ 47 57 private $prefixLength = 0; 48 58 49 59 /** 50 60 * Create a new File object. 51 * Valid constructor signatures include: 52 * File(File parent, string filename) 53 * File(string filename) 54 * File(string parent, string filename) 55 */ 56 function __construct($arg1 = null, $arg2 = null) { 61 * 62 * This method supports sevarl valid signatures: 63 * new File(File parent, string filename) 64 * new File(string filename) 65 * new File(string parent, string filename) 66 */ 67 function __construct($arg1, $arg2 = null) { 57 68 58 69 if (self::$separator === null || self::$pathSeparator === null) { … … 62 73 } 63 74 64 /* simulate signature identified constructors*/75 /* simulate constructor overloading */ 65 76 if ($arg1 instanceof File && is_string($arg2)) { 66 $this->_ constructFileParentStringChild($arg1, $arg2);77 $this->__constructFileParentStringChild($arg1, $arg2); 67 78 } elseif (is_string($arg1) && ($arg2 === null)) { 68 $this->_ constructPathname($arg1);79 $this->__constructPathname($arg1); 69 80 } elseif(is_string($arg1) && is_string($arg2)) { 70 $this->_ constructStringParentStringChild($arg1, $arg2);81 $this->__constructStringParentStringChild($arg1, $arg2); 71 82 } else { 72 83 if ($arg1 === null) { … … 77 88 } 78 89 } 79 80 /** Returns the length of this abstract pathname's prefix. */81 function getPrefixLength() {82 return (int) $this->prefixLength;83 }84 90 85 /* -- constructors not called by signature match, so we need some helpers --*/ 86 87 function _constructPathname($pathname) { 91 /** 92 * Private overloaded constructor when passed a File parent path and string child path. 93 * @param File $parent The parent path 94 * @param string $child (optional) The child path 95 */ 96 private function __constructFileParentStringChild(File $parent, $child) { 97 // obtain ref to the filesystem layer 98 $fs = FileSystem::getFileSystem(); 99 100 if ($child === null) { 101 throw new NullPointerException("Argument to function must not be null"); 102 } 103 104 if ($parent !== null) { 105 if ($parent->getPath() === "") { 106 $this->path = $fs->resolve($fs->getDefaultParent(), $fs->normalize($child)); 107 } else { 108 $this->path = $fs->resolve($parent->getPath(), $fs->normalize($child)); 109 } 110 } else { 111 $this->path = $fs->normalize($child); 112 } 113 $this->prefixLength = $fs->prefixLength($this->path); 114 } 115 116 /** 117 * Private constructor when passed a single path string. 118 * 119 * @param string $pathname 120 */ 121 private function __constructPathname($pathname) { 88 122 // obtain ref to the filesystem layer 89 123 $fs = FileSystem::getFileSystem(); … … 96 130 $this->prefixLength = (int) $fs->prefixLength($this->path); 97 131 } 98 99 function _constructStringParentStringChild($parent, $child = null) { 132 133 /** 134 * Private overloaded constructor when passed a string parent path and child paths. 135 * @param string $parent The parent path 136 * @param string $child (optional) The child path 137 */ 138 private function __constructStringParentStringChild($parent, $child) { 100 139 // obtain ref to the filesystem layer 101 140 $fs = FileSystem::getFileSystem(); … … 115 154 $this->prefixLength = (int) $fs->prefixLength($this->path); 116 155 } 117 118 function _constructFileParentStringChild($parent, $child = null) { 119 // obtain ref to the filesystem layer 120 $fs = FileSystem::getFileSystem(); 121 122 if ($child === null) { 123 throw new NullPointerException("Argument to function must not be null"); 124 } 125 126 if ($parent !== null) { 127 if ($parent->path === "") { 128 $this->path = $fs->resolve($fs->getDefaultParent(), $fs->normalize($child)); 129 } else { 130 $this->path = $fs->resolve($parent->path, $fs->normalize($child)); 131 } 132 } else { 133 $this->path = $fs->normalize($child); 134 } 135 $this->prefixLength = $fs->prefixLength($this->path); 156 157 /** 158 * Returns the length of this abstract pathname's prefix. 159 * @return int 160 */ 161 function getPrefixLength() { 162 return (int) $this->prefixLength; 136 163 } 137 164 … … 144 171 * string is returned. 145 172 * 146 * @return The name of the file or directory denoted by this abstract 147 * pathname, or the empty string if this pathname's name sequence 148 * is empty 149 */ 150 function getName() { 173 * @return string The name of the file or directory. 174 */ 175 public function getName() { 151 176 // that's a lastIndexOf 152 177 $index = ((($res = strrpos($this->path, self::$separator)) === false) ? -1 : $res); … … 166 191 * directory. 167 192 * 168 * @return The pathname string of the parent directory named by this 169 * abstract pathname, or null if this pathname does not name a parent 170 */ 171 function getParent() { 193 * @return string The pathname string of the parent directory. 194 */ 195 public function getParent() { 172 196 // that's a lastIndexOf 173 197 $index = ((($res = strrpos($this->path, self::$separator)) === false) ? -1 : $res); … … 182 206 183 207 /** 184 * Returns the abstract pathname of this abstract pathname's parent, 185 * or null if this pathname does not name a parent directory. 186 * 187 * The parent of an abstract pathname consists of the pathname's prefix, 188 * if any, and each name in the pathname's name sequence except for the 189 * last. If the name sequence is empty then the pathname does not name 190 * a parent directory. 191 * 192 * @return The abstract pathname of the parent directory named by this 193 * abstract pathname, or null if this pathname 194 * does not name a parent 195 */ 196 function getParentFile() { 208 * Returns the parent directory as File object. 209 * 210 * @return File A File of the parent directory for this file. 211 */ 212 public function getParentFile() { 197 213 $p = $this->getParent(); 198 214 if ($p === null) { … … 207 223 * in the name sequence. 208 224 * 209 * @return The string form of this abstract pathname210 */ 211 function getPath() {225 * @return string The string form of this abstract pathname 226 */ 227 public function getPath() { 212 228 return (string) $this->path; 213 229 } … … 220 236 * is "\\". 221 237 * 222 * @return true if this abstract pathname is absolute, false otherwise223 */ 224 function isAbsolute() {238 * @return boolean true if this abstract pathname is absolute, false otherwise 239 */ 240 public function isAbsolute() { 225 241 return ($this->prefixLength !== 0); 226 242 } 227 228 243 229 244 /** … … 242 257 * directory. 243 258 * 244 * @return The absolute pathname string denoting the same file or 245 * directory as this abstract pathname 246 * @see #isAbsolute() 247 */ 248 function getAbsolutePath() { 259 * @return string The absolute pathname of this file/directory. 260 * @see isAbsolute() 261 */ 262 public function getAbsolutePath() { 249 263 $fs = FileSystem::getFileSystem(); 250 264 return $fs->resolveFile($this); … … 252 266 253 267 /** 254 * Returns the absolute form of this abstract pathname. Equivalent to 255 * getAbsolutePath. 256 * 257 * @return The absolute abstract pathname denoting the same file or 258 * directory as this abstract pathname 259 */ 260 function getAbsoluteFile() { 268 * Returns a File object containing abs path to this file/dir. 269 * 270 * @see getAbsolutePath() 271 * @return File A File object containing the absolute path to this file/dir. 272 */ 273 public function getAbsoluteFile() { 261 274 return new File((string) $this->getAbsolutePath()); 262 275 } 263 264 276 265 277 /** … … 284 296 * pathname after the file or directory is deleted. 285 297 * 286 * @return The canonical pathname string denoting the same file or 287 * directory as this abstract pathname 288 */ 289 function getCanonicalPath() { 298 * @return string The canonical path to this file/dir. 299 */ 300 public function getCanonicalPath() { 290 301 $fs = FileSystem::getFileSystem(); 291 302 return $fs->canonicalize($this->path); 292 303 } 293 304 294 295 /** 296 * Returns the canonical form of this abstract pathname. Equivalent to 297 * getCanonicalPath(. 298 * 299 * @return File The canonical pathname string denoting the same file or 300 * directory as this abstract pathname 301 */ 302 function getCanonicalFile() { 305 /** 306 * Returns the canonical form of this abstract pathname. 307 * 308 * @see getCanonicalPath() 309 * @return File The canonical File to tihs file/dir. 310 */ 311 public function getCanonicalFile() { 303 312 return new File($this->getCanonicalPath()); 304 313 } 305 306 /** 307 * Converts this abstract pathname into a file: URL. The 308 * exact form of the URL is system-dependent. If it can be determined that 309 * the file denoted by this abstract pathname is a directory, then the 310 * resulting URL will end with a slash. 311 * 312 * Usage note: This method does not automatically escape 313 * characters that are illegal in URLs. It is recommended that new code 314 * convert an abstract pathname into a URL by first converting it into a 315 * URI, via the toURI() method, and then converting the URI 316 * into a URL via the URI::toURL() 317 * 318 * @return A URL object representing the equivalent file URL 319 * 320 * 321 */ 322 function toURL() { 323 /* 324 // URL class not implemented yet 325 return new URL("file", "", $this->_slashify($this->getAbsolutePath(), $this->isDirectory())); 326 */ 327 } 328 329 /** 330 * Constructs a file: URI that represents this abstract pathname. 331 * Not implemented yet 332 */ 333 function toURI() { 334 /* 335 $f = $this->getAbsoluteFile(); 336 $sp = (string) $this->slashify($f->getPath(), $f->isDirectory()); 337 if (StringHelper::startsWith('//', $sp)) 338 $sp = '//' + sp; 339 return new URI('file', null, $sp, null); 340 */ 341 } 342 343 function _slashify($path, $isDirectory) { 314 315 /** 316 * Normalizes the directory separators in the path and adds a trailing '/' to directories. 317 * 318 * @param string $path 319 * @param boolean $isDirectory 320 * @return string 321 */ 322 private function _slashify($path, $isDirectory) { 344 323 $p = (string) $path; 345 324 … … 365 344 * abstract pathname. 366 345 * 367 * @return true if and only if the file specified by this 368 * abstract pathname exists and can be read by the 369 * application; false otherwise 370 */ 371 function canRead() { 346 * @return boolean Whether file/dir can be read by application. 347 */ 348 public function canRead() { 372 349 $fs = FileSystem::getFileSystem(); 373 350 … … 382 359 * abstract pathname. 383 360 * 384 * @return true if and only if the file system actually 385 * contains a file denoted by this abstract pathname and 386 * the application is allowed to write to the file; 387 * false otherwise. 388 * 389 */ 390 function canWrite() { 361 * @return boolean Whether file/dir can be written to. 362 * 363 */ 364 public function canWrite() { 391 365 $fs = FileSystem::getFileSystem(); 392 366 return $fs->checkAccess($this, true); … … 396 370 * Tests whether the file denoted by this abstract pathname exists. 397 371 * 398 * @return true if and only if the file denoted by this 399 * abstract pathname exists; false otherwise 400 * 401 */ 402 function exists() { 372 * @return boolean Whether file/dir exists. 373 */ 374 public function exists() { 403 375 clearstatcache(); 404 376 if ($this->isFile()) { … … 410 382 411 383 /** 412 * Tests whether the file denoted by this abstract pathname is a 413 * directory. 414 * 415 * @return true if and only if the file denoted by this 416 * abstract pathname exists and is a directory; 417 * false otherwise 418 * 419 */ 420 function isDirectory() { 384 * Tests whether the path represented by this object corresponds to a directory. 385 * 386 * @return boolean Whether path represented is a directory. 387 */ 388 public function isDirectory() { 421 389 clearstatcache(); 422 390 $fs = FileSystem::getFileSystem(); … … 428 396 429 397 /** 430 * Tests whether the file denoted by this abstract pathname is a normal 431 * file. A file is normal if it is not a directory and, in 432 * addition, satisfies other system-dependent criteria. Any non-directory 433 * file created by a Java application is guaranteed to be a normal file. 434 * 435 * @return true if and only if the file denoted by this 436 * abstract pathname exists and is a normal file; 437 * false otherwise 438 */ 439 function isFile() { 398 * Tests whether the path represented by this object corresponds to a normal file. 399 * 400 * @return boolean Whether path represents a file. 401 */ 402 public function isFile() { 440 403 clearstatcache(); 441 404 //$fs = FileSystem::getFileSystem(); … … 444 407 445 408 /** 446 * Tests whether the file named by this abstract pathname is a hidden 447 * file. The exact definition of hidden is system-dependent. On 448 * UNIX systems, a file is considered to be hidden if its name begins with 449 * a period character ('.'). On Win32 systems, a file is considered to be 450 * hidden if it has been marked as such in the filesystem. Currently there 451 * seems to be no way to dermine isHidden on Win file systems via PHP 452 * 453 * @return true if and only if the file denoted by this 454 * abstract pathname is hidden according to the conventions of the 455 * underlying platform 456 */ 457 function isHidden() { 409 * Tests whether the path represented by this object is a hidden file. 410 * 411 * @return boolean Whether file/dir is hidden. 412 */ 413 public function isHidden() { 458 414 $fs = FileSystem::getFileSystem(); 459 415 if ($fs->checkAccess($this) !== true) { … … 467 423 * last modified. 468 424 * 469 * @return A integer value representing the time the file was425 * @return int A integer value representing the time the file was 470 426 * last modified, measured in milliseconds since the epoch 471 427 * (00:00:00 GMT, January 1, 1970), or 0 if the 472 428 * file does not exist or if an I/O error occurs 473 429 */ 474 function lastModified() {430 public function lastModified() { 475 431 $fs = FileSystem::getFileSystem(); 476 432 if ($fs->checkAccess($this) !== true) { … … 484 440 * The return value is unspecified if this pathname denotes a directory. 485 441 * 486 * @return The length, in bytes, of the file denoted by this abstract 487 * pathname, or 0 if the file does not exist 488 */ 489 function length() { 442 * @return int The length, in bytes, of the file denoted by this abstract 443 * pathname, or 0 if the file does not exist 444 * @throws IOException - if file cannot be read 445 */ 446 public function length() { 490 447 $fs = FileSystem::getFileSystem(); 491 448 if ($fs->checkAccess($this) !== true) { … … 499 456 * This method uses file_get_contents() to read file in an optimized way. 500 457 * @return string 501 * @throws Exception - if file cannot be read502 */ 503 function contents() {458 * @throws IOException - if file cannot be read 459 */ 460 public function contents() { 504 461 if (!$this->canRead() || !$this->isFile()) { 505 462 throw new IOException("Cannot read file contents!"); … … 522 479 * @throws IOException if file can't be created 523 480 */ 524 function createNewFile($parents=true, $mode=0777) {481 public function createNewFile($parents=true, $mode=0777) { 525 482 $file = FileSystem::getFileSystem()->createNewFile($this->path); 526 483 return $file; … … 535 492 * successfully deleted; false otherwise 536 493 */ 537 function delete() {494 public function delete() { 538 495 $fs = FileSystem::getFileSystem(); 539 496 if ($fs->canDelete($this) !== true) { … … 553 510 * 554 511 */ 555 function deleteOnExit() {512 public function deleteOnExit() { 556 513 $fs = FileSystem::getFileSystem(); 557 514 $fs->deleteOnExit($this); … … 559 516 560 517 /** 561 * Returns an array of strings naming the files and directories in the 562 * directory denoted by this abstract pathname. 518 * Return an array of names for contents of directory represented by this object. 563 519 * 564 520 * If this abstract pathname does not denote a directory, then this 565 * method returns null Otherwise an array of strings is 566 * returned, one for each file or directory in the directory. Names 567 * denoting the directory itself and the directory's parent directory are 568 * not included in the result. Each string is a file name rather than a 569 * complete path. 570 * 571 * There is no guarantee that the name strings in the resulting array 572 * will appear in any specific order; they are not, in particular, 573 * guaranteed to appear in alphabetical order. 574 * 575 * @return An array of strings naming the files and directories in the 576 * directory denoted by this abstract pathname. The array will be 577 * empty if the directory is empty. Returns null if 578 * this abstract pathname does not denote a directory, or if an 579 * I/O error occurs. 580 * 581 */ 582 function listDir($filter = null) { 521 * method returns null. 522 * 523 * @return array string[] An array of file and directory names 524 */ 525 public function listDir($filter = null) { 583 526 $fs = FileSystem::getFileSystem(); 584 527 return $fs->lister($this, $filter); 585 528 } 586 587 function listFiles($filter = null) { 529 530 /** 531 * Return an array of File objects for contents of directory represented by this object. 532 * 533 * @param unknown_type $filter 534 * @return array File[] 535 */ 536 public function listFiles($filter = null) { 588 537 $ss = $this->listDir($filter); 589 538 if ($ss === null) { … … 609 558 * @throws IOException 610 559 */ 611 function mkdirs() {560 public function mkdirs() { 612 561 if ($this->exists()) { 613 562 return false; … … 628 577 * 629 578 * @return true if and only if the directory was created; false otherwise 630 * @throws IOException 631 */ 632 function mkdir() {579 * @throws IOException - If no write access 580 */ 581 public function mkdir() { 633 582 $fs = FileSystem::getFileSystem(); 634 583 … … 645 594 * @return true if and only if the renaming succeeded; false otherwise 646 595 */ 647 function renameTo(File $destFile) {596 public function renameTo(File $destFile) { 648 597 $fs = FileSystem::getFileSystem(); 649 598 if ($fs->checkAccess($this) !== true) { … … 660 609 * @return true if and only if the renaming succeeded; false otherwise 661 610 */ 662 function copyTo(File $destFile) {611 public function copyTo(File $destFile) { 663 612 $fs = FileSystem::getFileSystem(); 664 613 … … 684 633 * that was passed to this method. 685 634 * 686 * @param time The new last-modified time, measured in milliseconds since635 * @param int $time The new last-modified time, measured in milliseconds since 687 636 * the epoch (00:00:00 GMT, January 1, 1970) 688 * @return true if and only if the operation succeeded; false otherwise689 */ 690 function setLastModified($time) {637 * @return boolean Whether operation succeeded 638 */ 639 public function setLastModified($time) { 691 640 $time = (int) $time; 692 641 if ($time < 0) { … … 704 653 /** 705 654 * Marks the file or directory named by this abstract pathname so that 706 * only read operations are allowed. After invoking this method the file 707 * or directory is guaranteed not to change until it is either deleted or 708 * marked to allow write access. Whether or not a read-only file or 709 * directory may be deleted depends upon the underlying system. 710 * 711 * @return true if and only if the operation succeeded; false otherwise 712 */ 713 function setReadOnly() { 655 * only read operations are allowed. 656 * 657 * @return boolean Whether operation succeeded 658 */ 659 public function setReadOnly() { 714 660 $fs = FileSystem::getFileSystem(); 715 661 if ($fs->checkAccess($this, true) !== true) { … … 724 670 * @param int $mode Ocatal mode. 725 671 */ 726 function setMode($mode) {672 public function setMode($mode) { 727 673 $fs = FileSystem::getFileSystem(); 728 674 return $fs->chmod($this->getPath(), $mode); … … 733 679 * @return int 734 680 */ 735 function getMode() {681 public function getMode() { 736 682 return @fileperms($this->getPath()); 737 683 } … … 767 713 * objects containing UNC pathnames will not be returned by this method. 768 714 * 769 * @return An array of File objects denoting the available715 * @return array File[] An array of File objects denoting the available 770 716 * filesystem roots, or null if the set of roots 771 717 * could not be determined. The array will be empty if there are 772 718 * no filesystem roots. 773 719 */ 774 function listRoots() {720 public function listRoots() { 775 721 $fs = FileSystem::getFileSystem(); 776 722 return (array) $fs->listRoots(); … … 781 727 /** 782 728 * Returns the path to the temp directory. 783 */ 784 function getTempDir() { 729 * @return string 730 */ 731 public function getTempDir() { 785 732 return Phing::getProperty('php.tmpdir'); 786 733 } … … 792 739 * Then, the file is locked for exclusive reading/writing. 793 740 * 794 * @author manuel holtgrewe, grin@gmx.net 795 * @throws IOException 796 * @access public 797 */ 798 function createTempFile($prefix, $suffix, File $directory) { 741 * @throws IOException 742 */ 743 public static function createTempFile($prefix, $suffix, File $directory) { 799 744 800 745 // quick but efficient hack to create a unique filename ;-) … … 817 762 * @access public 818 763 */ 819 function removeTempFile() {764 public function removeTempFile() { 820 765 $fs = FileSystem::getFileSystem(); 821 766 // catch IO Exception … … 841 786 * greater than the argument 842 787 */ 843 function compareTo(File $file) {788 public function compareTo(File $file) { 844 789 $fs = FileSystem::getFileSystem(); 845 790 return $fs->compare($this, $file); … … 847 792 848 793 /** 849 * Tests this abstract pathname for equality with the given object. 850 * Returns <code>true</code> if and only if the argument is not 851 * <code>null</code> and is an abstract pathname that denotes the same file 852 * or directory as this abstract pathname. Whether or not two abstract 853 * pathnames are equal depends upon the underlying system. On UNIX 854 * systems, alphabetic case is significant in comparing pathnames; on Win32 855 * systems it is not. 794 * Tests to see whether two File objects are equal. 856 795 * @return boolean 857 796 */ 858 function equals($obj) {797 public function equals($obj) { 859 798 if (($obj !== null) && ($obj instanceof File)) { 860 799 return ($this->compareTo($obj) === 0); … … 863 802 } 864 803 865 /** Backwards compatibility -- use PHP5's native __tostring method. */ 866 function toString() { 804 /** 805 * Backwards compatibility -- use PHP5's native __tostring method. 806 * @deprecated 807 */ 808 public function toString() { 867 809 return $this->getPath(); 868 810 } 869 811 870 /** PHP5's native method. */ 871 function __toString() { 812 /** 813 * PHP5's semi-magic __toString() method. 814 * @return string 815 */ 816 public function __toString() { 872 817 return $this->getPath(); 873 818 } 874 819 } 875 ?>trunk/classes/phing/system/io/FileSystem.php
r309 r313 51 51 const BA_HIDDEN = 0x08; 52 52 53 /** Instance for getFileSystem() method. */ 53 /** 54 * Singleton instance for getFileSystem() method. 55 * @var FileSystem 56 */ 54 57 private static $fs; 55 58 … … 63 66 switch(Phing::getProperty('host.fstype')) { 64 67 case 'UNIX': 65 66 68 self::$fs = new UnixFileSystem(); 67 69 break; 68 70 case 'WIN32': 69 70 71 self::$fs = new Win32FileSystem(); 71 72 break; 72 73 case 'WINNT': 73 74 74 self::$fs = new WinNTFileSystem(); 75 75 break;
