6 * Generate DataObjects... and readers in the right places..
8 * note - we write to a temporary directory first...
13 require_once 'DB/DataObject/Generator.php';
16 /** basic thing now works...
18 * it needs a bit more intelligence to work out what to do...
21 * Basically we need to build up all the formats for each db column
23 * - overlay any mapping stuff.
25 * - overlay user defined settings
26 * = write it out to file..
29 $this->def['order'][$table][] = $t->name;
30 $this->def['readers'][$table][$t->name] = $reader;
31 $this->def['colmodels'][$table][$t->name] = $colmodel;
32 $this->def['forms'][$table][$t->name] = $form;
35 * readersDef[table.col]
43 class Pman_Core_Generator extends DB_DataObject_Generator
54 * [tablename] => array( list of columns ones with '-' indicate lookup
56 * [tablename][colname] -> reader foramt
58 * [tablename][colname] => xtype / name etc...
60 * [tablename] => data for reader args (eg. id / total prop etc.)
62 * [tablename] => array of cols with types
64 * [tablename] -> array of cols
70 var $page = false; // page container when run from cli.
72 // dont do usual stuff!!!
74 var $tablekeys = array();
76 var $overwrite = array(); // default dont overwrite any of the files..
77 // array('master', 'corejs', 'corephp', 'index', 'Roo')
78 // and ('js-?????' where ??? is the table name) <- for all the generated js classes.
79 // we always overwrite the definition!!!
80 // set to array('all') to overwrite everything!!!
82 function start($cli=false, $mods='', $overwrite='')
85 $ff = HTML_Flexyframework::get();
87 //echo '<PRE>'; print_r($this->modtables); exit;
89 $options = &PEAR::getStaticProperty('DB_DataObject','options');
92 $proj = 'pman'; //ucfirst(basename($options['database']));
93 // we are going to generate all of the code into a temporay foldler..
94 $user = posix_getpwuid(posix_getuid());
96 $options['rootDir'] = ini_get('session.save_path').'/temp_'. $proj.'_'. $user['name'];
97 $options['cli'] = $cli;
98 $options['mods'] = empty($mods) ? array() : explode('/',$mods);
100 if (!file_exists($options['rootDir'])) {
101 mkdir($options['rootDir'], 0775, true);
104 $this->rootDir = $options['rootDir'];
105 $options['schema_location'] = $this->rootDir .'/'.$proj.'/DataObjects';
106 $options['class_location'] = $this->rootDir .'/'.$proj.'/DataObjects';
107 $options['require_prefix'] = $proj . '/DataObjects/';
108 $options['class_prefix'] = $proj . '_DataObjects_';
109 // print_r($this);exit;
114 $standard_database = $options['database'];
126 $this->scanModules();
127 require_once 'System.php';
128 $diff = System::which('diff');
129 // now for each of the directories copy/show diffs..
130 echo $cli ? '' : '<PRE>';
131 $flist = explode(',', $overwrite);
132 foreach($this->modtables as $m=>$ar) {
133 if ($options['database'] != $standard_database) {
134 $options['database'] = $standard_database ;
139 $options['database'] = $standard_database ;
140 if (isset($options['database_'. $m])) {
141 $options['database'] = $options['database_'. $m];
142 //var_dump($url);exit;
149 if (!empty($options['mods'] ) && !in_array($m, $options['mods'] )) {
152 foreach(scandir($options['rootDir'].'/'.$m) as $f) {
153 if (!strlen($f) || $f[0] == '.') {
157 $src = $options['rootDir']."/$m/$f";
158 $tg = $ff->page->rootDir."/Pman/$m/DataObjects/$f";
159 if (preg_match('/\.js$/', $f)) {
160 $tg = $ff->page->rootDir."/Pman/$m/$f";
163 if (!file_exists($tg) || !filesize($tg) ) {
165 if ($cli && in_array($f, $flist)) {
166 echo "COPY $src $tg" . ($cli ? "\n" : "<BR>");
170 echo "!!!!MISSING!!! $tg" . ($cli ? "\n" : "<BR>");
174 // always copy readers and ini file.= nope - not on live..
175 if ($cli && in_array($f, $flist)) {
177 //|| $f=='pman.ini' || preg_match('/\.js$/', $f))) {
178 echo "COPY $src $tg". ($cli ? "\n" : "<BR>");
184 $cmd = "$diff -u -w ". escapeshellarg($tg) . ' ' . escapeshellarg($src);
186 $out = array(); $ret = 0;
187 exec($cmd, $out, $ret);
188 if ($ret ==0) { // files match..
192 echo "\n" .implode( "\n" , $out) . "\n";
209 $options = &PEAR::getStaticProperty('DB_DataObject','options');
211 $ff = HTML_Flexyframework::get();
213 $url = parse_url($options['database']);
214 // hide stuff for web..
215 $cli = $options['cli'];
217 $url['pass'] = '*****';
218 $url['user'] = '*****';
219 $url['host'] = '*****';
224 require_once 'System.php';
225 $cat = System::which('cat');
226 $mysql = System::which('mysql');
227 print_r($options['mods'] );
228 foreach($this->modsql as $m => $fl)
230 if ($cli && isset($options['database_'. $m])) {
231 $url =parse_url($options['database_'.$m]);
234 $mysql_cmd = $mysql .
235 ' -h ' . $url['host'] .
236 ' -u' . escapeshellarg($url['user']) .
237 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass']) : '') .
238 ' ' . basename($url['path']);
240 echo $mysql_cmd . "\n" ;
242 if (!empty($options['mods'] ) && !in_array($m, $options['mods'] )) {
247 $fn = $ff->page->rootDir. "/Pman/$m/DataObjects/$f";
248 $cmd = $cat . ' ' . escapeshellarg($fn) . " | $mysql_cmd -f ";
249 echo $cmd. ($cli ? "\n" : "<BR>\n");
261 * Scan the folders for DataObjects
262 * - Use the list of php files in DataObjects folders
263 * to determine which module owns which database table.
268 function scanModules()
271 $options = &PEAR::getStaticProperty('DB_DataObject','options');
272 if (isset($options['modtables'])) {
273 $this->modtables = $options['modtables'];
274 $this->modmap = $options['modmap'];
275 $this->modsql = $options['modsql'];
279 $ff = HTML_Flexyframework::get();
281 $top = $ff->page->rootDir .'/Pman';
282 $this->modtables = array();
283 $this->modmap = array();
284 $this->modmapsql = array();
286 foreach(scandir($top) as $m) {
290 !is_dir($top .'/'.$m) ||
291 !file_exists($top .'/'.$m.'/DataObjects')
295 $this->modtables[$m] = array();
296 $this->modsql[$m] = array();
297 foreach(scandir($top .'/'.$m.'/DataObjects') as $f) {
298 if (!strlen($f) || $m[0] == '.') {
301 if (preg_match('/\.sql$/', $f)) {
302 $this->modsql[$m][] = $f;
305 if (preg_match('/\.php$/', $f)) {
306 $tn = strtolower(preg_replace('/\.php$/', '', $f));
307 $this->modtables[$m][] = $tn;
308 $this->modmap[$tn] = $m;
313 $options['modtables'] = $this->modtables;
314 $options['modmap'] = $this->modmap;
315 $options['modsql'] = $this->modsql;
316 // print_r($options);
321 * this is run first, so picks up any missing dataobject files..
324 function generateDefinitions()
326 if (!$this->tables) {
327 $this->debug("-- NO TABLES -- \n");
330 if (!isset($this->modmap)) {
331 $this->scanModules();
333 $options = &PEAR::getStaticProperty('DB_DataObject','options');
334 $builder_options = PEAR::getStaticProperty('Pman_Builder','options');
335 $ignore = empty($builder_options['skip_tables']) ? array() : $builder_options['skip_tables'];
337 $mods = $options['mods'];
339 $this->_newConfig = '';
340 foreach($this->tables as $this->table) {
342 $tn = strtolower($this->table);
343 //print_r($this->modmap);//[$tn]);//
347 if (!isset($this->modmap[$tn])) {
349 if (in_array($this->table, $ignore)) {
354 die("No existing DataObject file found for table {$this->table}
356 - either add it to Pman_Builder[skip_tables] or\n
357 - create an empty file in the related Module/DataObjects directory
359 touch Pman/????/DataObjects/".ucfirst($this->table).".php
364 $mod = $this->modmap[$tn];
365 $inis[$mod] = isset($inis[$mod]) ? $inis[$mod] : '';
368 $this->_newConfig = '';
369 $this->_generateDefinitionsTable();
372 $inis[$mod] .= $this->_newConfig;
375 //echo '<PRE>';print_r($this->_inis); exit;
376 $options = PEAR::getStaticProperty('DB_DataObject','options');
378 $rd = $options['rootDir'];
379 foreach($inis as $m=>$ini) {
380 if (!empty($mods) && !in_array($m, $mods)) {
384 if (!file_exists($rd.'/'.$m)) {
385 mkdir($rd.'/'.$m, 0775, true);
387 $fname = '/pman.ini';
388 if (isset($options['database_'. $m])) {
389 $url = parse_url($options['database_'.$m]);
390 $fname = '/'. basename($url['path']).'.ini';
394 file_put_contents($rd.'/'.$m.$fname, $ini);
400 function generateClasses()
402 // print_R($this->modmap);
403 // die("generateClasses");
404 $options = &PEAR::getStaticProperty('DB_DataObject','options');
406 $ff = HTML_Flexyframework::get();
408 $rd = $options['rootDir'];
409 $mods = $options['mods'];
410 $this->_extends = 'DB_DataObject';
411 $this->_extendsFile = 'DB/DataObject.php';
412 $cli = $options['cli'];
414 foreach($this->tables as $this->table) {
415 $this->table = trim($this->table);
416 $tn = strtolower($this->table);
417 $mod = $this->modmap[$tn];
419 if (!empty($mods) && !in_array($mod, $mods)) {
424 $this->classname = 'Pman_'.$mod . '_DataObjects_'. ucfirst($this->table); // replace odd chars?
427 $outfilename = $rd.'/'.$mod.'/'. ucfirst($this->table).'.php';
428 $orig = $ff->page->rootDir .'/Pman/'.$mod.'/DataObjects/'. ucfirst($this->table).'.php';
431 // file_get_contents???
432 $oldcontents = file_get_contents($orig);
435 echo "GENERATE: " . $this->classname . ($cli ? "\n" : "<BR>");
437 $out = $this->_generateClassTable($oldcontents);
439 // get rid of static GET!!!
440 $out = preg_replace('/(\n|\r\n)\s*function staticGet[^\n]+(\n|\r\n)/s', '', $out);
441 $out = preg_replace('#/\* Static get \*/#s', '', $out);
444 // $this->debug( "writing $this->classname\n");
445 //$tmpname = tempnam(session_save_path(),'DataObject_');
446 file_put_contents($outfilename, $out);
453 // function generateDefinitions() { }
454 ////function generateForeignKeys() { }
455 // function generateClasses() { }
461 function parseConfig()
463 $options = &PEAR::getStaticProperty('DB_DataObject','options');
465 if (isset($options['modtables'])) {
466 $this->modtables = $options['modtables'];
467 $this->modmap = $options['modmap'];
468 $this->modsql = $options['modsql'];
471 $ff = HTML_Flexyframework::get();
472 $dirs = array($ff->page->rootDir.'/Pman/DataObjects'); // not used anymore!
473 foreach($this->modtables as $m=>$ts) {
474 $dirs[] = $ff->page->rootDir.'/Pman/'.$m.'/DataObjects';
477 //echo '<PRE>';print_R($ini);//exit;
484 //print_r( $this->def['readers'][$table]);
485 // print_r( $this->def['colmodels'][$table]);
486 //print_r($this->def['readers'][$table]); exit;
491 function writeFileEx($n, $f, $str)
493 if (file_exists($f)) {
494 // all - will not overwrite stuff.. (only being specific willl)
495 if (!in_array($n, $this->overwrite)) {
496 $this->writeFile($f.'.generated',$str);
500 $this->writeFile($f,$str);
504 function writeFile($f, $str)
506 require_once 'System.php';
507 System::mkdir(array('-p', dirname($f)));
510 file_put_contents($f, $str);