Source for file RSDEngineFile.php

Documentation is available at RSDEngineFile.php


1 <?php
2 // RSDEngine: The Rapid and Secure Development Engine
3 // Copyright (C) 2003 Lukas Feiler
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 /**Contains just the class RSDEngineFile.
20 *
21 * @package RSDEngine
22 * @author Lukas Feiler <lukas.feiler@chello.at>
23 * @copyright Lukas Feiler 2003
24 * @filesource
25 */
26
27 /**Helper class for creating source files.
28 *
29 * There are two ways of using this class.
30 * The first one is to extend it and to overwrite the methods getFilename,
31 * and getCode. This is recommendable if the file you want to create
32 * is rather complex. You would call the constructor with only one argument:
33 * <code>
34 *new RSDEngineFileChildClass($config);
35 * </code>
36 * The second way to use this class makes only sense if $config is not needed
37 * for creating your file. Call the constructor like this:
38 * <code>
39 *new RSDEngineClass(false, "myFilename.php", $fileCode);
40 * </code>
41 *
42 * @author Lukas Feiler <lukas.feiler@chello.at>
43 * @version 0.1.9
44 * @copyright Lukas Feiler 2003
45 * @package RSDEngine
46 */
47 class RSDEngineFile {
48
49 /**An associative array containing all configuration options. Gets set inside the constructor method.
50 * @var Array
51 */
52 var $config = array();
53
54 /**The name of the file to create. Gets set inside the constructor method.
55 * @var String
56 */
57 var $filename = '';
58
59 /**The code of the file to create. Gets set inside the constructor method.
60 * @var String
61 */
62 var $code = '';
63
64 /**The directory to which to write the file.
65 * @see writeToDirectory
66 * @var String
67 */
68 var $dir = '';
69
70 /**An associative array containing related files. A class that extends
71 * RSDEngineFile and overwrites the method getCode might need this to
72 * communicate with other files that are created. Gets set inside the
73 * method setRelatedFiles.
74 * @see setRelatedFiles
75 * @var Array
76 */
77 var $relatedFiles = array();
78
79 /**An associative array containing related files. A class that extends
80 * RSDEngineFile and overwrites the method getCode might need this to
81 * communicate with other classes that are created. Gets set inside the
82 * method setRelatedClasses.
83 * @see setRelatedClasses
84 * @var Array
85 */
86 var $relatedClasses = array();
87
88 /**An associative array containing related files. A class that extends
89 * RSDEngineFile and overwrites the method getCode might need this to
90 * communicate with other functions that are created. Gets set inside the
91 * method setRelatedFunctions.
92 * @see setRelatedFunctions
93 * @var Array
94 */
95 var $relatedFunctions = array();
96
97 /**Whether to overwrite an existing file.
98 * @var boolean
99 */
100 var $overwriteExisting = true;
101
102 /**Whether to prevent writing this file.
103 * @var boolean
104 */
105 var $preventWriting = false;
106
107 /**Constructor that does the initialization.
108 *
109 * @see config
110 * @see filename
111 * @see code
112 * @see dir
113 *
114 * @param Array $config An associative array containing all configuration options.
115 * @param String $filename The name of the file to create.
116 * Passing this argument makes only sense if the method getFilename
117 * does not get overwritten. This argument is optional. The default is false.
118 * @param String $code The code of the file to create.
119 * Passing this argument makes only sense if the method getCode
120 * does not get overwritten. This argument is optional. The default is false.
121 */
122 function RSDEngineFile(&$config, $filename = false, $code = false)
123 {
124 $this->config =& $config;
125 $this->filename = $filename;
126 $this->code = $code;
127 $this->dir = '';
128 }
129
130 /**Writes the file to a directory.
131 *
132 * If this method is called without arguments or $dir === false the file will be written to $this->dir.
133 * Otherwise the file will be written to the directory passed as argument.
134 * @see setOutputDirectory
135 *
136 * @param String $dir The directory to which to write the file. If this argument is false the file will
137 * be written to $this->dir. This argument is optional. The default is false.
138 */
139 function writeToDirectory($dir=false)
140 {
141 if ($dir === false) {
142 $dir = $this->dir;
143 }
144
145 $code = $this->getCode();
146
147 if ($this->preventWriting) {
148 $this->config['rsdEngine']->logInfo("Writing Prevented: $dir/" . $this->getFilename());
149 return;
150 }
151
152 if (!$this->overwriteExisting) {
153 if (file_exists("$dir/" . $this->getFilename())) {
154 $this->config['rsdEngine']->logInfo("Already exists: $dir/" . $this->getFilename());
155 return;
156 }
157 }
158 $error = File::write("$dir/" . $this->getFilename(), $code, FILE_MODE_WRITE);
159 if (PEAR::isError($error)) {
160 $this->config['rsdEngine']->logWarn("Could not write $dir/" . $this->getFilename() . ": " . $error->getMessage());
161 } else {
162 $this->config['rsdEngine']->logInfo("Successfully written: $dir/" . $this->getFilename());
163 }
164 }
165
166 /**Sets the output direcotry.
167 *
168 * $this->dir is set to the value passed as argument.
169 * @see writeToDirectory
170 * @see dir
171 *
172 * @param String $dir The output directory.
173 */
174 function setOutputDirectory($dir)
175 {
176 $this->dir = $dir;
177 }
178
179 /**Returns the filename.
180 * @see filename
181 *
182 * @return String $this->filename is returned.
183 */
184 function getFilename()
185 {
186 return $this->filename;
187 }
188
189 /**Returns the code for this file.
190 * @see code
191 * @return String $this->code is returned.
192 */
193 function getCode()
194 {
195 return $this->code;
196 }
197
198 /**Sets the related files.
199 *
200 * $this->relatedFiles is set to the array passed as argument.
201 * @see relatedFiles
202 * @param Array $files An associative array containing related files.
203 */
204 function setRelatedFiles($files)
205 {
206 $this->relatedFiles = $files;
207 }
208
209 /**Sets the related classes.
210 *
211 * $this->relatedClasses is set to the array passed as argument.
212 * @see relatedClasses
213 * @param Array $classes An associative array containing related classes.
214 */
215 function setRelatedClasses($classes)
216 {
217 $this->relatedClasses = $classes;
218 }
219
220 /**Sets the related functions.
221 *
222 * $this->relatedFunctions is set to the array passed as argument.
223 * @see relatedFunctions
224 * @param Array $functions An associative array containing related functions.
225 */
226 function setRelatedFunctions($functions)
227 {
228 $this->relatedFunctions = $functions;
229 }
230
231 /**Sets the properties preventWriting and overwriteExisting based on the string passed as argument
232 * @param String $write 'true', 'false' or 'overwrite.
233 */
234 function setWrite($write)
235 {
236 if ($write == 'false') {
237 $this->preventWriting = true;
238 $this->overwriteExisting = false;
239 } elseif ($write == 'overwrite') {
240 $this->overwriteExisting = true;
241 $this->preventWriting = false;
242 } elseif ($write == 'true') {
243 $this->preventWriting = false;
244 $this->overwriteExisting = false;
245 }
246 }
247 }
248 ?>

Documentation generated on Mon, 8 Dec 2003 13:12:46 +0100 by phpDocumentor 1.2.3