3 * File handling functions in phpdoc.
6 * @author Ulf Wendel <ulf@redsys.de>
8 class PhpdocFileHandler extends PhpdocObject {
11 * Filepath. The path is automatically added in front of all filenames
19 * Reads a list of files or one file.
21 * @param mixed Filename or an array filenames, $k => $filename
25 function get($files) {
27 $this->err[] = new PhpdocError("No files specified.", __FILE__, __LINE__);
31 if (!is_array($files))
32 $files = array($files);
38 while (list($k, $filename) = each($files))
39 $contents[$filename] = $this->getFile($filename);
45 * Sets the filepath. The path is automatically added in front of all filenames
51 function setFilePath($path) {
53 } // end func setFilePath
58 * @param string $filename
59 * @return string $content
62 function getFile($filename) {
63 if ("" == $filename) {
64 $this->err[] = new PhpdocError("getFile(), no filename specified.", __FILE__, __LINE__);
67 if (!file_exists($filename)) {
68 $this->err[] = new PhpdocError("getFile(), unknown file '$filename'.", __FILE__, __LINE__);
71 if (!$fh = @fopen($filename, "r")) {
72 $this->err[] = new PhpdocError("getFile(), can't open file '$filename' for reading.", __FILE__, __LINE__);
76 $content = fread($fh, filesize($filename));
83 * Appends a string to a file.
85 * @param string Filename
86 * @param string Content to append
87 * @param string Directory prefix
90 * @todo ... add a function boldy.
92 function appendToFile($filename, $content, $directory = "") {
93 if ("" == $filename || "" == $content) {
94 $this->err[] = new PhpdocError("No filename and/or no content given.", __FILE__, __LINE__);
98 $fh = @fopen($filename, "a");
104 fwrite($fh, $content);
108 } // end func appendToFile
111 * Creates a new file.
113 * Create or overrides a file in a specified directory. If the
114 * directory does not exists, it attempts to create it.
119 * @throws PHPDocError
122 function createFile($filename, $content, $directory = "") {
123 if ("" == $filename || "" == $content) {
124 $this->err[] = new PhpdocError("No filename or no content given.", __FILE__, __LINE__);
128 $fh = @fopen($filename, "w");
130 $this->err[] = new PhpdocError("Can't create file '$filename'.", __FILE__, __LINE__);
134 fwrite($fh, $content);
138 } // end func createFile
141 * Returns a list of files in a specified directory
143 * @param string $directory
144 * @param mixed $suffix Suffix of the files returned
145 * @param boolean $flag_subdir include subdirectories?
146 * @param array $files New entries are added to this variable if provided.
147 * Used only for the subdir feature.
148 * @return array $files
149 * @throws PhpdocError
151 function getFilesInDirectory($directory, $suffix = "", $flag_subdir = true, $files = "") {
152 if ("" == $directory) {
153 $this->err[] = new PhpdocError("No directory specified", __FILE__, __LINE__);
157 if ("/" != substr($directory, -1))
167 if (!is_array($suffix))
168 $suffix = array($suffix);
171 while (list($k, $v) = each($suffix))
172 $allowed[".$v"] = true;
176 if (!is_array($files))
179 $dh = @opendir($directory);
181 $this->err[] = new PhpdocError("Can't open '$directory' for reading.", __FILE__, __LINE__);
185 while ($file = readdir($dh)) {
186 if ("." == $file || ".." == $file)
189 if ($flag_subdir && is_dir($directory.$file))
190 $files = $this->getFilesInDirectory($directory.$file, $suffix, true, $files);
192 if (!is_file($directory.$file))
198 if (isset($allowed[substr($file, strrpos($file, "."))]))
199 $files[] = $directory.$file;
206 } // end fun getFilesInDirectory
208 } // end class PhpdocFileHandler