Generator.php
[Pman.Core] / Generator.php
1 <?php
2  
3  
4 /**
5  * 
6  * Generate DataObjects...
7  * 
8  * This does not generate ini files any more - as that is done on the fly by the framework.
9  * 
10  * note - we write to a temporary directory first...
11  * 
12  * 
13  */
14  
15 require_once 'DB/DataObject/Generator.php';
16
17
18 /** basic thing now works... 
19
20 * it needs a bit more intelligence to work out what to do...
21
22
23 * Basically we need to build up all the formats for each db column
24 * then 
25 *   - overlay any mapping stuff.
26 *   
27 *   - overlay user defined settings
28 *   = write it out to file..
29 *   
30 *  Strucutres:
31 $this->def['order'][$table][] = $t->name;
32 $this->def['readers'][$table][$t->name] = $reader;
33 $this->def['colmodels'][$table][$t->name] = $colmodel;
34 $this->def['forms'][$table][$t->name] = $form;
35
36 *   Readers
37 *       readersDef[table.col]
38
39
40
41 * **/
42
43
44
45 class Pman_Core_Generator extends DB_DataObject_Generator
46 {
47     
48
49    
50     // inherrited..
51     // $tablekeys
52     // $tables
53     // $_definitions
54     /**
55      * def[order]  
56      *      [tablename] => array( list of columns ones with '-' indicate lookup
57      *    [readers]
58      *       [tablename][colname] -> reader foramt
59      *    [forms]
60      *        [tablename][colname] => xtype / name etc...
61      *    [readerArgs]
62      *        [tablename] => data for reader args (eg. id / total prop etc.)
63      *  readers =>
64      *         [tablename] => array of cols with types
65      *  forms =>
66      *        [tablename] -> array of cols
67      * 
68      */ 
69     var $def;
70     
71       
72     var $page = false; // page container when run from cli.
73     
74     // dont do usual stuff!!!
75     var $rootDir = '';
76     var $tablekeys = array();
77     
78     var $overwrite = array(); // default dont overwrite any of the files..
79     //  array('master', 'corejs', 'corephp', 'index', 'Roo')
80     // and ('js-?????' where ??? is the table name) <- for all the generated js classes.
81     // we always overwrite the definition!!!
82     // set to array('all') to overwrite everything!!!
83     
84     function start($cli=false, $mods='', $overwrite='')
85     {
86         
87         $ff = HTML_Flexyframework::get();
88         $this->scanModules();
89         //echo '<PRE>'; print_r($this->modtables); exit;
90         
91         $options = &PEAR::getStaticProperty('DB_DataObject','options');
92         
93         
94         $proj = 'pman'; //ucfirst(basename($options['database']));
95         // we are going to generate all of the code into a temporay foldler..
96         $user = posix_getpwuid(posix_getuid());
97         
98         $options['rootDir'] = ini_get('session.save_path').'/temp_'. $proj.'_'. $user['name'];
99         $options['cli'] = $cli;
100         $options['mods'] = empty($mods) ? array() : explode('/',$mods);
101        
102         if (!file_exists($options['rootDir'])) {
103             mkdir($options['rootDir'], 0775, true);
104         }
105         
106         $this->rootDir = $options['rootDir'];
107         $options['schema_location'] =  $this->rootDir .'/'.$proj.'/DataObjects';
108         $options['class_location'] = $this->rootDir .'/'.$proj.'/DataObjects';
109         $options['require_prefix'] =    $proj . '/DataObjects/';
110         $options['class_prefix'] =    $proj . '_DataObjects_';
111        //  print_r($this);exit;
112        
113        
114         $this->importSQL();
115        
116         $standard_database = $options['database'];
117        
118        
119         var_dump($mods);exit;
120        
121        
122        
123        
124        
125        
126         parent::start();
127         
128         $this->scanModules();
129         require_once 'System.php';
130         $diff = System::which('diff');
131         // now for each of the directories copy/show diffs..
132         echo $cli ? '' : '<PRE>';
133         $flist = explode(',', $overwrite);
134         foreach($this->modtables as $m=>$ar) {
135             if ($options['database'] !=  $standard_database) {
136                 $options['database'] =  $standard_database ;
137                 
138                 parent::start();
139             }
140             
141             $options['database'] =  $standard_database ;
142             if (isset($options['database_'. $m])) {
143                 $options['database'] =  $options['database_'. $m];
144                 //var_dump($url);exit;
145                 
146                 // start again?
147                 parent::start();
148             }
149             
150             
151             if (!empty($options['mods'] ) && !in_array($m,  $options['mods'] )) {
152                 continue;
153             }
154             // this happens when we have no database tables from a module,
155             // but module code has been defined.
156             if (!file_exists($options['rootDir'].'/'.$m)) {
157                 continue;
158             }
159             foreach(scandir($options['rootDir'].'/'.$m) as $f) {
160                 if (!strlen($f) || $f[0] == '.') {
161                     continue;
162                 }
163                 // does it exist!!!
164                 $src = $options['rootDir']."/$m/$f";
165                 $tg = $ff->page->rootDir."/Pman/$m/DataObjects/$f";
166                 if (preg_match('/\.js$/', $f)) {
167                     $tg = $ff->page->rootDir."/Pman/$m/$f";
168                 }
169                 
170                 if (!file_exists($tg) || !filesize($tg) ) {
171                   
172                     if ($cli && !filesize($tg) || in_array($f, $flist)) {
173                         echo "COPY $src $tg" . ($cli ? "\n" : "<BR>");
174                         copy($src, $tg);
175                         continue;
176                     }
177                     echo "!!!!MISSING!!! $tg" . ($cli ? "\n" : "<BR>");
178                     
179                     continue;
180                 }
181                 // always copy readers and ini file.=  nope - not on live..
182                 if ($cli && in_array($f, $flist) || in_array('_all_', $flist )) {
183                     
184                    //|| $f=='pman.ini' || preg_match('/\.js$/', $f))) {
185                     echo "COPY $src $tg". ($cli ? "\n" : "<BR>");
186                     copy($src, $tg);
187                     continue;
188                 }
189                 
190                 // diff the two..
191                 $cmd = "$diff -u -w ". escapeshellarg($tg) . ' ' . escapeshellarg($src);
192                  
193                 $out = array(); $ret = 0;
194                 exec($cmd, $out, $ret);
195                 if ($ret ==0) { // files match..
196                     continue;
197                 }
198                 // var_dump($ret);
199                 echo "\n" .implode( "\n" , $out) . "\n";
200                
201                 
202             }
203             
204             
205         }
206         
207         
208         
209         
210     }
211     /**
212      * imports SQL files from all DataObjects directories....
213      * 
214      * except any matching /migrate/
215      */
216     function importSQL()
217     {
218         $options = &PEAR::getStaticProperty('DB_DataObject','options');
219         
220         $ff = HTML_Flexyframework::get();
221         
222         $url = parse_url($options['database']);
223         // hide stuff for web..
224         $cli = $options['cli'];
225         if (!$cli) {
226             $url['pass'] = '*****';
227             $url['user'] = '*****';
228             $url['host'] = '*****';
229         }
230         
231         require_once 'System.php';
232         $cat = System::which('cat');
233         $mysql = System::which('mysql');
234         print_r($options['mods'] );
235         foreach($this->modsql as $m => $fl)
236         {
237             if ($cli && isset($options['database_'. $m])) {
238                 $url = parse_url($options['database_'.$m]);
239             }
240             
241             $mysql_cmd = $mysql .
242                 ' -h ' . $url['host'] .
243                 ' -u' . escapeshellarg($url['user']) .
244                 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass'])  :  '') .
245                 ' ' . basename($url['path']);
246            
247             echo $mysql_cmd . "\n" ;
248             
249             if (!empty($options['mods'] ) && !in_array($m,  $options['mods'] )) {
250                 continue;
251             }
252             
253             foreach($fl as $f) {
254                 $fn = $ff->page->rootDir. "/Pman/$m/DataObjects/$f";
255                 if (preg_match('/migrate/i', $f)) { // skip migration scripts at present..
256                     continue;
257                 }
258                 
259                 $cmd = $cat . ' ' . escapeshellarg($fn) . " | $mysql_cmd -f ";
260                 echo $cmd. ($cli ? "\n" : "<BR>\n");
261                 if ($cli) {
262                     passthru($cmd);
263                 }
264                 
265             }
266         }
267         
268         
269         
270     }
271     /**
272      * Scan the folders for DataObjects
273      * - Use the list of php files in DataObjects folders 
274      *   to determine which module owns which database table.
275      * 
276      */
277     
278     
279     function scanModules()
280     {
281         
282         $options = &PEAR::getStaticProperty('DB_DataObject','options');
283         if (isset($options['modtables'])) {
284             $this->modtables = $options['modtables'];
285             $this->modmap = $options['modmap'];
286             $this->modsql = $options['modsql'];
287             return;
288         }
289         
290         $ff = HTML_Flexyframework::get();
291         
292         $top = $ff->page->rootDir .'/Pman';
293         $this->modtables = array();
294         $this->modmap = array();
295         $this->modmapsql = array();
296         
297         foreach(scandir($top) as $m) {
298             
299             if (!strlen($m) || 
300                     $m[0] == '.' || 
301                     !is_dir($top .'/'.$m) || 
302                     !file_exists($top .'/'.$m.'/DataObjects')
303                 ) {
304                 continue;
305             }
306             $this->modtables[$m] = array();
307             $this->modsql[$m] = array();
308             foreach(scandir($top .'/'.$m.'/DataObjects') as $f) {
309                 if (!strlen($f) ||   $m[0] == '.') {
310                     continue;
311                 }
312                 if (preg_match('/\.sql$/', $f))  {
313                     $this->modsql[$m][] = $f;
314                 }
315                                 
316                 if (preg_match('/\.php$/', $f))  {
317                     $tn = strtolower(preg_replace('/\.php$/', '', $f));
318                     $this->modtables[$m][] = $tn;
319                     $this->modmap[$tn] = $m;
320                     continue;
321                 }
322             }
323         }
324         $options['modtables'] = $this->modtables;
325         $options['modmap'] = $this->modmap;
326         $options['modsql'] = $this->modsql;
327        // print_r($options);
328         
329     }
330     /**
331      * 
332      * this is run first, so picks up any missing dataobject files..
333      */
334     
335     function generateDefinitions()
336     {
337         if (!$this->tables) {
338             $this->debug("-- NO TABLES -- \n");
339             return;
340         }
341         if (!isset($this->modmap)) {
342             $this->scanModules();
343         }
344          $options = &PEAR::getStaticProperty('DB_DataObject','options');
345         $builder_options = PEAR::getStaticProperty('Pman_Builder','options');
346         $ignore = empty($builder_options['skip_tables']) ? array() : $builder_options['skip_tables'];
347         
348          $mods = $options['mods'];
349         $inis = array();
350         $this->_newConfig = '';
351         foreach($this->tables as $this->table) {
352             
353             $tn  = strtolower($this->table);
354             //print_r($this->modmap);//[$tn]);//
355             
356             
357             
358             if (!isset($this->modmap[$tn])) {
359                 
360                 if (in_array($this->table, $ignore)) {
361                     continue;
362                 }
363                 if (empty($mods)) {
364                 
365                 
366                    die("No existing DataObject file found for table {$this->table} 
367             
368 - either add it to Pman_Builder[skip_tables] or\n
369 - run generator and specify that module..
370 - create an empty file in the related Module/DataObjects directory
371 eg. 
372 touch Pman/????/DataObjects/".ucfirst($this->table).".php
373    
374    ");
375                 }
376                 // use mods to determine where it should output to..
377                 //var_dump($mods);exit;
378                 $this->modmap[$tn] = $mods[0];
379                 
380                 
381             }
382             $mod = $this->modmap[$tn];
383             $inis[$mod] = isset($inis[$mod]) ? $inis[$mod] : '';
384             
385             
386             $this->_newConfig = '';
387             $this->_generateDefinitionsTable();
388             
389             
390             $inis[$mod] .= $this->_newConfig;
391         }
392         return; // we do not generate in ifiles any more..
393          
394     }
395     
396     function generateClasses() 
397     {
398       // print_R($this->modmap);
399        // die("generateClasses");
400         $options = &PEAR::getStaticProperty('DB_DataObject','options');
401         
402         $ff = HTML_Flexyframework::get();
403         
404         $rd = $options['rootDir'];
405         $mods = $options['mods'];
406         $this->_extends = 'DB_DataObject';
407         $this->_extendsFile = 'DB/DataObject.php';
408         $cli = $options['cli'];
409
410         foreach($this->tables as $this->table) {
411             $this->table        = trim($this->table);
412             $tn  = strtolower($this->table);
413             $mod = $this->modmap[$tn];
414             
415              if (!empty($mods) && !in_array($mod, $mods)) {
416                 continue;
417             }
418             
419             
420             $this->classname    = 'Pman_'.$mod . '_DataObjects_'. ucfirst($this->table); // replace odd chars?
421            
422            
423             $outfilename    = $rd.'/'.$mod.'/'. ucfirst($this->table).'.php';
424             $orig           = $ff->page->rootDir .'/Pman/'.$mod.'/DataObjects/'. ucfirst($this->table).'.php';
425             
426            
427                 // file_get_contents???
428             
429             $oldcontents = file_exists($orig) ? file_get_contents($orig) : '';
430             
431              
432             echo "GENERATE: " .   $this->classname  . ($cli ? "\n" : "<BR>");
433             
434             $out = $this->_generateClassTable($oldcontents);
435             
436             // get rid of static GET!!!
437             $out = preg_replace('/(\n|\r\n)\s*function staticGet[^\n]+(\n|\r\n)/s', '', $out);
438             $out = preg_replace('#/\* Static get \*/#s', '', $out);
439               
440             if (!file_exists(dirname($outfilename))) {
441                 mkdir(dirname($outfilename), 0755, true);
442             }
443            // $this->debug( "writing $this->classname\n");
444             //$tmpname = tempnam(session_save_path(),'DataObject_');
445             file_put_contents($outfilename, $out);
446             
447         }
448     }
449     
450     
451         
452    // function generateDefinitions() { }
453     ////function generateForeignKeys() { }
454    // function generateClasses() { }
455    
456       
457      
458      
459    
460     function parseConfig()
461     {
462          $options = &PEAR::getStaticProperty('DB_DataObject','options');
463         
464         if (isset($options['modtables'])) {
465             $this->modtables = $options['modtables'];
466             $this->modmap = $options['modmap'];
467             $this->modsql = $options['modsql'];
468         }
469         
470         $ff = HTML_Flexyframework::get();
471         $dirs = array($ff->page->rootDir.'/Pman/DataObjects'); // not used anymore!
472         foreach($this->modtables as $m=>$ts) {
473             $dirs[] = $ff->page->rootDir.'/Pman/'.$m.'/DataObjects';
474         }
475         
476          //echo '<PRE>';print_R($ini);//exit;
477         
478         
479          
480     }
481      
482         //var_dump($table);
483         //print_r( $this->def['readers'][$table]);
484        // print_r( $this->def['colmodels'][$table]);
485         //print_r($this->def['readers'][$table]); exit;
486         
487       
488        
489     function writeFileEx($n, $f, $str) 
490     {
491         if (file_exists($f)) {
492             // all - will not overwrite stuff.. (only being specific willl)
493             if (!in_array($n, $this->overwrite)) {
494                 $this->writeFile($f.'.generated',$str);
495                 return;
496             }
497         }
498         $this->writeFile($f,$str);
499         
500         
501     }
502     function writeFile($f, $str)
503     {
504         require_once 'System.php';
505         System::mkdir(array('-p', dirname($f)));
506         // overwrite???
507         echo "write: $f\n";
508         file_put_contents($f, $str);
509     } 
510    
511 }
512