1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
<?php
/**
* File handling functions in phpdoc.
*
* @version 0.1alpha
* @author Ulf Wendel <ulf@redsys.de>
*/
class PhpdocFileHandler extends PhpdocObject {
/**
* Filepath. The path is automatically added in front of all filenames
* @var string $path
* @see setFilePath()
* @access private
*/
var $path = "";
/**
* Reads a list of files or one file.
* @param string $files filename
* @param array $files list of filenames, $k => $filename
* @return array $contents file contents indexed by filename
* @throws PhpdocError
* @access public
*/
function Get($files) {
if (""==$files) {
$this->err[] = new PhpdocError("No files specified.", __FILE__, __LINE__);
return array("", "");
}
if (!is_array($files))
$files = array($files);
$contents = array();
$together = "";
reset($files);
while (list($k, $filename)=each($files))
$contents[$filename] = $this->getFile($filename);
return $contents;
} // end func Get
/**
* Sets the filepath. The path is automatically added in front of all filenames
* @param string $path
* @return bool $ok
* @access public
*/
function setFilePath($path) {
$this->path = $path;
} // end func setFilePath
/**
* Reads a file.
* @param string $filename
* @return string $content
* @throws PhpdocError
* @access private
*/
function getFile($filename) {
if (""==$filename) {
$this->err[] = new PhpdocError("getFile(), no filename specified.", __FILE__, __LINE__);
return "";
}
if (!file_exists($filename)) {
$this->err[] = new PhpdocError("getFile(), unknown file '$filename'.", __FILE__, __LINE__);
return "";
}
if (!$fh = @fopen($filename, "r")) {
$this->err[] = new PhpdocError("getFile(), can't open file '$filename' for reading.", __FILE__, __LINE__);
return "";
}
$content = fread($fh, filesize($filename));
fclose($fh);
return $content;
} // end func getFile
function appendToFile($filename, $content, $directory="") {
if (""==$filename || ""==$content) {
$this->err[] = new PhpdocError("No filename and/or no content given.", __FILE__, __LINE__);
return false;
}
$fh = @fopen($filename, "a");
if (!$fh) {
print $filename;
### PANIK
return false;
}
fwrite($fh, $content);
fclose($fh);
return true;
} // end func appendToFile
/**
* Creates a new file.
*
* Create or overrides a file in a specified directory. If the
* directory does not exists, it attempts to create it.
*/
function createFile($filename, $content, $directory="") {
if (""==$filename || ""==$content) {
$this->err[] = new PhpdocError("No filename or no content given.", __FILE__, __LINE__);
return false;
}
$fh = @fopen($filename, "w");
if (!$fh) {
$this->err[] = new PhpdocError("Can't create file '$filename'.", __FILE__, __LINE__);
return false;
}
fwrite($fh, $content);
fclose($fh);
return true;
} // end func createFile
/**
* Returns a list of files in a specified directory
* @param string $directory
* @param mixed $suffix Suffix of the files returned
* @param boolean $flag_subdir include subdirectories?
* @param array $files New entries are added to this variable if provided.
* Used only for the subdir feature.
* @return array $files
* @throws PhpdocError
*/
function getFilesInDirectory($directory, $suffix="", $flag_subdir = true, $files = "") {
if (""==$directory) {
$this->err[] = new PhpdocError("No directory specified", __FILE__, __LINE__);
return array();
}
if ("/"!=substr($directory, -1))
$directory.= "/";
if (""==$suffix)
$flag_all = true;
else {
$flag_all = false;
$allowed = array();
if (!is_array($suffix))
$suffix = array($suffix);
reset($suffix);
while (list($k, $v)=each($suffix))
$allowed[".".$v] = true;
}
if (!is_array($files))
$files = array();
$dh = @opendir($directory);
if (!$dh) {
$this->err[] = new PhpdocError("Can't open '$directory' for reading.", __FILE__, __LINE__);
return array();
}
while ($file = readdir($dh)) {
if ("." == $file || ".." == $file)
continue;
if ($flag_subdir && is_dir($directory.$file))
$files = $this->getFilesInDirectory($directory.$file, $suffix, true, $files);
if (!is_file($directory.$file))
continue;
if ($flag_all) {
$files[] = $file;
} else {
if (isset($allowed[substr($file, strrpos($file, "."))]))
$files[] = $directory.$file;
}
}
closedir($dh);
return $files;
} // end fun getFilesInDirectory
} // end class PhpdocFileHandler
?>
|