"SfR Fresh" - the SfR Freeware/Shareware Archive 
Member "combine-0.4.2/generator/Generator.php" of archive combine-0.4.2.tar.gz:
As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting with prefixed line numbers.
Alternatively you can here view or download the uninterpreted source code file.
That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
1 <?
2 /**
3 *
4 * Copyright (c) 2000-2001 David Giffin
5 *
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * Install Combine and Build Projects
9 *
10 * $Id: Generator.php,v 1.5 2003/10/01 00:02:42 david Exp $
11 *
12 * @version V2.0 Copyright (c) 2000-2001 David Giffin
13 * @package Generator
14 * @author David Giffin <david@giffin.org>
15 * @since PHP 4.0
16 * @copyright Copyright (c) 2000-2003 David Giffin : LGPL - See LICENCE
17 *
18 */
19
20 /**
21 *
22 * Define Pear Console Options
23 */
24 require_once('Console/Getopt.php');
25
26
27 require_once("Combine/Combine.php");
28
29 /** XML Parsers / Class Generators / XML Generators */
30 require_once(dirname(__FILE__) . "/SchemaParser.php");
31 require_once(dirname(__FILE__) . "/ConfigWriter.php");
32 require_once(dirname(__FILE__) . "/TemplateHandler.php");
33
34 /**
35 * Process the schema file and generate source code
36 *
37 * This object will handle reading the schema, command line options and more
38 *
39 * @version V2.0 Copyright (c) 2000-2001 David Giffin
40 * @package Generator
41 * @author David Giffin <david@giffin.org>
42 * @since PHP 4.0
43 * @copyright Copyright (c) 2000-2003 David Giffin : LGPL - See LICENCE
44 *
45 */
46 class Generator {
47
48 /** Path Seperator */
49 var $pathSeperator = "/";
50
51 /** Smarty Template Handler */
52 var $templateHandler;
53
54 /** Schema Files */
55 var $schemaFiles = array();
56
57 /** Project Directory */
58 var $projectDir = "";
59
60 /** Pear Console Object */
61 var $console;
62
63 /** Pear Console Aguments */
64 var $args;
65
66 var $fileTypePaths = array(
67 "properties" => "/combine",
68 "stub" => "/combine",
69 "object" => "/combine/om",
70 "action" => "/actions",
71 "template" => "/templates" );
72
73
74 /** List of files which require generation */
75 var $generateFiles = array();
76
77 /** Types wich require generation */
78 var $generateTypes = array();
79
80 /** Types wich require generation */
81 var $generateAllTypes = array();
82
83 /** force over write */
84 var $forceOverWrite = false;
85
86 var $queue = array();
87
88 /**
89 * Constructor
90 *
91 */
92 function Generator() {
93
94 $this->combine =& Combine::getInstance();
95 $this->projectDir = $this->combine->request->getEnv("PWD");
96 $this->extraDir = $this->combine->getCombineDir() . "/extra";
97 $this->iconsDir = $this->combine->getCombineDir() . "/icons";
98 $this->schemaDir = $this->projectDir . "/schema";
99
100 }
101
102
103 /**
104 * Process the schema files
105 */
106 function doGenerator() {
107
108 $this->getSchemaFiles();
109 $this->templateHandler = new TemplateHandler();
110 $this->templateHandler->projectDir = $this->projectDir;
111
112 foreach ($this->schemaFiles as $schemaFile) {
113
114 print "Processing Schema File: $schemaFile\n";
115
116 // Read the schema and pass it to the template handler
117 $schemaParser = new SchemaParser($schemaFile);
118 $schema =& $schemaParser->getSchema();
119 $this->templateHandler->setSchema($schema);
120
121 foreach ($schema->tables as $tableName => $tableData) {
122
123 // reset the queue for this table
124 $this->queue = array();
125
126 // add new files into the queue
127 $this->doFileQueue($tableName);
128
129 // process the files from the queue
130 foreach ($this->queue as $file => $template) {
131 // print "Parsing Template: $template\n";
132 $this->templateHandler->parseAndWrite($tableName, $template, $file);
133 print "Wrote File: $file\n";
134 }
135 }
136
137 $this->doConfigWriter($schema);
138 }
139
140 $this->doCopyExtra();
141 $this->doMakeDirs();
142
143 }
144
145
146 /**
147 * Write a Combine MVC Config file
148 *
149 */
150 function doConfigWriter(&$schema) {
151
152 $configFileName = $schema->name . "-config.xml";
153 $configFilePath = $this->projectDir . "/combine/$configFileName";
154
155 if (!file_exists($configFilePath)) {
156
157 require_once(dirname(__FILE__) . "/ConfigWriter.php");
158 $configWriter = new ConfigWriter();
159
160 $configWriter->setSchema($schema);
161 $configWriter->doProcess();
162 $configWriter->writeFile($configFilePath);
163 }
164 }
165
166
167 /**
168 * Copy additional files required for runtime
169 *
170 */
171 function doCopyExtra() {
172 $this->copyNoOverWrite($this->extraDir . "/doAction.php",$this->projectDir . "/doAction.php");
173
174 $this->copyNoOverWrite($this->extraDir . "/dot.htaccess",$this->projectDir . "/combine/.htaccess");
175 $this->copyNoOverWrite($this->extraDir . "/dot.htaccess",$this->projectDir . "/actions/.htaccess");
176 $this->copyNoOverWrite($this->extraDir . "/dot.htaccess",$this->projectDir . "/templates/.htaccess");
177
178 $this->copyNoOverWrite($this->extraDir . "/header.tpl",$this->projectDir . "/templates/header.tpl");
179 $this->copyNoOverWrite($this->extraDir . "/footer.tpl",$this->projectDir . "/templates/footer.tpl");
180
181 $mediaDir = $this->projectDir . "/media";
182 $projectIcons = $this->projectDir . "/media/icons";
183
184 if (!file_exists($mediaDir)) {
185 mkdir($mediaDir);
186 }
187
188 if (!file_exists($projectIcons)) {
189 mkdir($projectIcons);
190 }
191 $this->copyRecursive($this->iconsDir, $projectIcons);
192 }
193
194
195 /**
196 * Copy a file but don't overwrite it if it already exists
197 *
198 * @param string $src The source file
199 * @param string $dest The destination file
200 *
201 * @return int the result of the copy command
202 */
203 function copyNoOverWrite($src, $dest) {
204 if (!file_exists($dest)) {
205 return copy($src,$dest);
206 }
207 }
208
209 /**
210 * Make Additional Directories
211 *
212 */
213 function doMakeDirs() {
214 $cacheDir = $this->projectDir . "/cache";
215 if (!file_exists($cacheDir)) {
216 mkdir($cacheDir);
217 }
218 }
219
220
221 /**
222 * Get the file name for a generated component
223 *
224 * @param string $table The name of the table for which the component references
225 * @param string $type The type of component we are generating
226 *
227 */
228 function doFileQueue($table) {
229 $phpName = $this->db2phpName($table);
230
231 foreach ($this->fileTypePaths as $type => $path) {
232 $absolutePath = $this->projectDir . $path;
233 switch($type) {
234 case "properties":
235 $this->addToQueue($type,"{$absolutePath}/combine.properties","CombineProperties.tpl");
236 break;
237 case "stub":
238 $this->addToQueue($type,"{$absolutePath}/{$phpName}.php","StubObject.tpl");
239 $this->addToQueue($type,"{$absolutePath}/{$phpName}Peer.php","StubPeer.tpl");
240 break;
241
242 case "object":
243 $this->addToQueue($type,"{$absolutePath}/Base{$phpName}.php","BaseObject.tpl");
244 $this->addToQueue($type,"{$absolutePath}/Base{$phpName}Peer.php","BasePeer.tpl");
245 break;
246
247 case "action":
248 $this->addToQueue($type,"{$absolutePath}/save{$phpName}.php","ActionSave.tpl");
249 $this->addToQueue($type,"{$absolutePath}/edit{$phpName}.php","ActionEdit.tpl");
250 $this->addToQueue($type,"{$absolutePath}/list{$phpName}.php","ActionList.tpl");
251 $this->addToQueue($type,"{$absolutePath}/delete{$phpName}.php","ActionDelete.tpl");
252 $this->addToQueue($type,"{$absolutePath}/detail{$phpName}.php","ActionDetail.tpl");
253 break;
254
255 case "template":
256 $this->addToQueue($type,"{$absolutePath}/edit{$phpName}.tpl","SmartyEdit.tpl");
257 $this->addToQueue($type,"{$absolutePath}/list{$phpName}.tpl","SmartyList.tpl");
258 $this->addToQueue($type,"{$absolutePath}/detail{$phpName}.tpl","SmartyDetail.tpl");
259 break;
260
261 default:
262 break;
263 }
264 }
265 }
266
267
268 /**
269 * Check if a file needs to be generated and add the file to the queue
270 *
271 * We will handle all of the logic to check if the file really needs
272 * to be generated here...
273 *
274 * @param string $type The type of component we are generating
275 * @param string $path The type of component we are generating
276 * @param string $template The type of component we are generating
277 */
278 function addToQueue($type,$path,$template) {
279
280 $addToQueue = false;
281 if ($type == "stub" || $type == "properties") {
282 if (!file_exists($path)) {
283 $addToQueue = true;
284 }
285 } else {
286 $addToQueue = $this->requiresGeneration($path,$type);
287 }
288 if ($addToQueue == true) {
289 $this->queue[$path] = $template;
290 }
291 }
292
293
294
295 /**
296 * Set the schema directory
297 *
298 * @param string $directory
299 */
300 function setSchemaDirectory($directory) {
301 if ($directory{0} == "/") {
302 $this->schemaDir = $directory;
303 }
304 $this->schemaDir = $this->projectDir . "/" . $directory;
305 }
306
307
308 /**
309 * Set Force Over Writing
310 *
311 * @param bool $value
312 */
313 function setForceOverWrite($value) {
314 $this->forceOverWrite = $value;
315 }
316
317
318 /**
319 * Add a generation type
320 *
321 * @param string $type
322 */
323 function addGenerateType($type) {
324 $this->generateTypes[$type] = true;
325 }
326
327
328 /**
329 * Add an generation all type
330 *
331 * @param string $type
332 */
333 function addGenerateAllType($type) {
334 $this->generateAllTypes[$type] = true;
335 }
336
337
338
339 /**
340 * Check if a files requires generation
341 *
342 * @param string $filepath path to the file
343 * @param string $type Type of file to be generated
344 *
345 * @return bool True if the file needs to be generated
346 */
347 function requiresGeneration($filepath,$type) {
348 $filename = basename($filepath);
349
350 if (!file_exists($filepath)) {
351
352 return true;
353
354 } else if ($this->generateTypes[$type] === true
355 && is_array($this->generateFiles)
356 && in_array($filename, $this->generateFiles) ) {
357
358 return true;
359
360 } else if ($this->generateAllTypes[$type] === true) {
361
362 return true;
363
364 }
365 return false;
366
367 }
368
369
370 /**
371 * Find all of the Schema files in the schema dir
372 *
373 */
374 function getSchemaFiles() {
375
376
377 if (!file_exists($this->schemaDir)) {
378 die("schema directory is missing: {$this->schemaDir}\n");
379 }
380
381 print "Looking for schema files in: {$this->schemaDir}\n";
382
383 $dh = dir($this->schemaDir);
384 while ($entry = $dh->read()) {
385 // Find Files with schema.xml extesion
386 if (preg_match("/schema.xml$/",$entry)) {
387 $this->schemaFiles[] = $this->schemaDir . "/" . $entry;
388 }
389 }
390 $dh->close();
391 }
392
393
394 /**
395 * Convert a Database Name to a PHP Name
396 *
397 * @access private
398 *
399 * @param string $name The database column name
400 * @return string The php name for the database column
401 */
402 function db2phpName($name) {
403 $words = split("_",strtolower($name));
404 $ret = "";
405 foreach ($words as $word) {
406 $ret .= ucfirst($word);
407 }
408 return $ret;
409 }
410
411 /**
412 * Copy Files Recursively
413 *
414 * This function copies all files in a directory structure
415 *
416 * @param string $from The The path to copy from
417 * @param string $to The path to copy to
418 */
419 function copyRecursive($from,$to) {
420
421 /*
422 if (is_dir($to)) {
423 if (!file_exists($to)) {
424 mkdir($to);
425 }
426 }
427 */
428
429 $d = dir($from);
430 while (false !== ($entry = $d->read())) {
431
432 if ($entry == "." || $entry == ".." || $entry == "CVS") {
433 continue;
434 }
435
436 $fromPath = "$from" . $this->pathSeperator . "$entry";
437 $toPath = "$to" . $this->pathSeperator . "$entry";
438
439 if (is_dir($fromPath)) {
440 if (!file_exists($toPath)) {
441 mkdir($toPath);
442 }
443
444 $this->copyRecursive($fromPath, $toPath);
445 } else {
446
447 print "Installing: $toPath\n";
448 copy($fromPath, $toPath);
449 }
450 }
451 $d->close();
452
453 }
454
455
456 }
457
458 if (!function_exists("lcfirst")) {
459 function lcfirst($str) {
460 return substr_replace($str,strtolower(substr($str, 0, 1)), 0, 1);
461 }
462 }
463
464 ?>