[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /* 3 * $Id: FileWriter.php 3076 2006-12-18 08:52:12Z fabien $ 4 * 5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 * 17 * This software consists of voluntary contributions made by many individuals 18 * and is licensed under the LGPL. For more information please see 19 * <http://phing.info>. 20 */ 21 22 include_once 'phing/system/io/PhingFile.php'; 23 include_once 'phing/system/io/Writer.php'; 24 25 /** 26 * Convenience class for reading files. The constructor of this 27 * 28 * @package phing.system.io 29 */ 30 class FileWriter extends Writer { 31 32 protected $file; 33 protected $fd; 34 35 /** Whether to append contents to file. */ 36 protected $append; 37 38 /** Whether we should attempt to lock the file (currently disabled). */ 39 protected $exclusive; 40 41 /** 42 * Construct a new FileWriter. 43 * @param mixed $file PhingFile or string pathname. 44 * @param boolean $append Append to existing file? 45 * @param boolean $exclusive Lock file? (currently disabled due to windows incompatibility) 46 */ 47 function __construct($file, $append = false, $exclusive = false) { 48 if ($file instanceof PhingFile) { 49 $this->file = $file; 50 } elseif (is_string($file)) { 51 $this->file = new PhingFile($file); 52 } else { 53 throw new Exception("Invalid argument type for \$file."); 54 } 55 $this->append = $append; 56 $this->exclusive = $exclusive; 57 } 58 59 function close() { 60 if ($this->fd === null) { 61 return true; 62 } 63 64 if (false === @fclose($this->fd)) { 65 // FAILED. 66 $msg = "Cannot fclose " . $this->file->__toString() . " $php_errormsg"; 67 throw new IOException($msg); 68 } else { 69 $this->fd = null; 70 return true; 71 } 72 } 73 74 function open() { 75 if ($this->fd === null) { 76 if ($this->append) { $flags = "ab"; } else { $flags = "wb"; } 77 $this->fd = @fopen($this->file->getPath(), $flags); 78 } 79 80 if ($this->fd === false) { 81 // fopen FAILED. 82 // Add error from php to end of log message. $php_errormsg. 83 $msg = "Cannot fopen ".$this->file->getPath()." $php_errormsg"; 84 throw new IOException($msg); 85 } 86 87 if (false) { 88 // Locks don't seem to work on windows??? HELP!!!!!!!!! 89 // if (FALSE === @flock($fp, LOCK_EX)) { // FAILED. 90 $msg = "Cannot acquire flock on $file. $php_errormsg"; 91 throw new IOException($msg); 92 } 93 94 return true; 95 } 96 97 function reset() { 98 // FIXME -- what exactly should this do, if anything? 99 // reset to beginning of file (i.e. re-open)? 100 } 101 102 function writeBuffer($buffer) { 103 104 if (!$this->file->canWrite()) { 105 throw new IOException("No permission to write to file: " . $this->file->__toString()); 106 } 107 108 $this->open(); 109 $result = @fwrite($this->fd, $buffer); 110 $this->close(); 111 112 if ($result === false) { 113 throw new IOException("Error writing file: ". $this->file->toString()); 114 } else { 115 return true; 116 } 117 } 118 119 function write($buf, $off = null, $len = null) { 120 if ( $off === null && $len === null ) 121 $to_write = $buf; 122 else 123 $to_write = substr($buf, $off, $len); 124 125 $this->open(); 126 $result = @fwrite($this->fd, $to_write); 127 128 if ( $result === false ) { 129 throw new IOException("Error writing file."); 130 } else { 131 return true; 132 } 133 } 134 135 function getResource() { 136 return $this->file->toString(); 137 } 138 } 139 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |