6 * Generate DataObjects...
8 * This does not generate ini files any more - as that is done on the fly by the framework.
10 * note - we write to a temporary directory first...
15 require_once 'DB/DataObject/Generator.php';
18 /** basic thing now works...
20 * it needs a bit more intelligence to work out what to do...
23 * Basically we need to build up all the formats for each db column
25 * - overlay any mapping stuff.
27 * - overlay user defined settings
28 * = write it out to file..
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;
37 * readersDef[table.col]
45 class Pman_Core_Generator extends DB_DataObject_Generator
56 * [tablename] => array( list of columns ones with '-' indicate lookup
58 * [tablename][colname] -> reader foramt
60 * [tablename][colname] => xtype / name etc...
62 * [tablename] => data for reader args (eg. id / total prop etc.)
64 * [tablename] => array of cols with types
66 * [tablename] -> array of cols
72 var $page = false; // page container when run from cli.
74 // dont do usual stuff!!!
76 var $tablekeys = array();
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!!!
84 function start($cli=false, $mods='', $overwrite='')
87 $ff = HTML_Flexyframework::get();
89 //echo '<PRE>'; print_r($this->modtables); exit;
91 $options = &PEAR::getStaticProperty('DB_DataObject','options');
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());
98 $options['rootDir'] = ini_get('session.save_path').'/temp_'. $proj.'_'. $user['name'];
99 $options['cli'] = $cli;
100 $options['mods'] = empty($mods) ? array() : explode('/',$mods);
102 if (!file_exists($options['rootDir'])) {
103 mkdir($options['rootDir'], 0775, true);
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;
116 $standard_database = $options['database'];
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 ;
141 $options['database'] = $standard_database ;
142 if (isset($options['database_'. $m])) {
143 $options['database'] = $options['database_'. $m];
144 //var_dump($url);exit;
151 if (!empty($options['mods'] ) && !in_array($m, $options['mods'] )) {
154 foreach(scandir($options['rootDir'].'/'.$m) as $f) {
155 if (!strlen($f) || $f[0] == '.') {
159 $src = $options['rootDir']."/$m/$f";
160 $tg = $ff->page->rootDir."/Pman/$m/DataObjects/$f";
161 if (preg_match('/\.js$/', $f)) {
162 $tg = $ff->page->rootDir."/Pman/$m/$f";
165 if (!file_exists($tg) || !filesize($tg) ) {
167 if ($cli && !filesize($tg) || in_array($f, $flist)) {
168 echo "COPY $src $tg" . ($cli ? "\n" : "<BR>");
172 echo "!!!!MISSING!!! $tg" . ($cli ? "\n" : "<BR>");
176 // always copy readers and ini file.= nope - not on live..
177 if ($cli && in_array($f, $flist)) {
179 //|| $f=='pman.ini' || preg_match('/\.js$/', $f))) {
180 echo "COPY $src $tg". ($cli ? "\n" : "<BR>");
186 $cmd = "$diff -u -w ". escapeshellarg($tg) . ' ' . escapeshellarg($src);
188 $out = array(); $ret = 0;
189 exec($cmd, $out, $ret);
190 if ($ret ==0) { // files match..
194 echo "\n" .implode( "\n" , $out) . "\n";
211 $options = &PEAR::getStaticProperty('DB_DataObject','options');
213 $ff = HTML_Flexyframework::get();
215 $url = parse_url($options['database']);
216 // hide stuff for web..
217 $cli = $options['cli'];
219 $url['pass'] = '*****';
220 $url['user'] = '*****';
221 $url['host'] = '*****';
226 require_once 'System.php';
227 $cat = System::which('cat');
228 $mysql = System::which('mysql');
229 print_r($options['mods'] );
230 foreach($this->modsql as $m => $fl)
232 if ($cli && isset($options['database_'. $m])) {
233 $url =parse_url($options['database_'.$m]);
236 $mysql_cmd = $mysql .
237 ' -h ' . $url['host'] .
238 ' -u' . escapeshellarg($url['user']) .
239 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass']) : '') .
240 ' ' . basename($url['path']);
242 echo $mysql_cmd . "\n" ;
244 if (!empty($options['mods'] ) && !in_array($m, $options['mods'] )) {
249 $fn = $ff->page->rootDir. "/Pman/$m/DataObjects/$f";
250 if (preg_match('/migrate/i', $f)) { // skip migration scripts at present..
254 $cmd = $cat . ' ' . escapeshellarg($fn) . " | $mysql_cmd -f ";
255 echo $cmd. ($cli ? "\n" : "<BR>\n");
267 * Scan the folders for DataObjects
268 * - Use the list of php files in DataObjects folders
269 * to determine which module owns which database table.
274 function scanModules()
277 $options = &PEAR::getStaticProperty('DB_DataObject','options');
278 if (isset($options['modtables'])) {
279 $this->modtables = $options['modtables'];
280 $this->modmap = $options['modmap'];
281 $this->modsql = $options['modsql'];
285 $ff = HTML_Flexyframework::get();
287 $top = $ff->page->rootDir .'/Pman';
288 $this->modtables = array();
289 $this->modmap = array();
290 $this->modmapsql = array();
292 foreach(scandir($top) as $m) {
296 !is_dir($top .'/'.$m) ||
297 !file_exists($top .'/'.$m.'/DataObjects')
301 $this->modtables[$m] = array();
302 $this->modsql[$m] = array();
303 foreach(scandir($top .'/'.$m.'/DataObjects') as $f) {
304 if (!strlen($f) || $m[0] == '.') {
307 if (preg_match('/\.sql$/', $f)) {
308 $this->modsql[$m][] = $f;
311 if (preg_match('/\.php$/', $f)) {
312 $tn = strtolower(preg_replace('/\.php$/', '', $f));
313 $this->modtables[$m][] = $tn;
314 $this->modmap[$tn] = $m;
319 $options['modtables'] = $this->modtables;
320 $options['modmap'] = $this->modmap;
321 $options['modsql'] = $this->modsql;
322 // print_r($options);
327 * this is run first, so picks up any missing dataobject files..
330 function generateDefinitions()
332 if (!$this->tables) {
333 $this->debug("-- NO TABLES -- \n");
336 if (!isset($this->modmap)) {
337 $this->scanModules();
339 $options = &PEAR::getStaticProperty('DB_DataObject','options');
340 $builder_options = PEAR::getStaticProperty('Pman_Builder','options');
341 $ignore = empty($builder_options['skip_tables']) ? array() : $builder_options['skip_tables'];
343 $mods = $options['mods'];
345 $this->_newConfig = '';
346 foreach($this->tables as $this->table) {
348 $tn = strtolower($this->table);
349 //print_r($this->modmap);//[$tn]);//
353 if (!isset($this->modmap[$tn])) {
355 if (in_array($this->table, $ignore)) {
360 die("No existing DataObject file found for table {$this->table}
362 - either add it to Pman_Builder[skip_tables] or\n
363 - create an empty file in the related Module/DataObjects directory
365 touch Pman/????/DataObjects/".ucfirst($this->table).".php
370 $mod = $this->modmap[$tn];
371 $inis[$mod] = isset($inis[$mod]) ? $inis[$mod] : '';
374 $this->_newConfig = '';
375 $this->_generateDefinitionsTable();
378 $inis[$mod] .= $this->_newConfig;
380 return; // we do not generate in ifiles any more..
384 function generateClasses()
386 // print_R($this->modmap);
387 // die("generateClasses");
388 $options = &PEAR::getStaticProperty('DB_DataObject','options');
390 $ff = HTML_Flexyframework::get();
392 $rd = $options['rootDir'];
393 $mods = $options['mods'];
394 $this->_extends = 'DB_DataObject';
395 $this->_extendsFile = 'DB/DataObject.php';
396 $cli = $options['cli'];
398 foreach($this->tables as $this->table) {
399 $this->table = trim($this->table);
400 $tn = strtolower($this->table);
401 $mod = $this->modmap[$tn];
403 if (!empty($mods) && !in_array($mod, $mods)) {
408 $this->classname = 'Pman_'.$mod . '_DataObjects_'. ucfirst($this->table); // replace odd chars?
411 $outfilename = $rd.'/'.$mod.'/'. ucfirst($this->table).'.php';
412 $orig = $ff->page->rootDir .'/Pman/'.$mod.'/DataObjects/'. ucfirst($this->table).'.php';
415 // file_get_contents???
416 $oldcontents = file_get_contents($orig);
419 echo "GENERATE: " . $this->classname . ($cli ? "\n" : "<BR>");
421 $out = $this->_generateClassTable($oldcontents);
423 // get rid of static GET!!!
424 $out = preg_replace('/(\n|\r\n)\s*function staticGet[^\n]+(\n|\r\n)/s', '', $out);
425 $out = preg_replace('#/\* Static get \*/#s', '', $out);
427 if (!file_exists(dirname($outfilename))) {
428 mkdir(dirname($outfilename), 0755, true);
430 // $this->debug( "writing $this->classname\n");
431 //$tmpname = tempnam(session_save_path(),'DataObject_');
432 file_put_contents($outfilename, $out);
439 // function generateDefinitions() { }
440 ////function generateForeignKeys() { }
441 // function generateClasses() { }
447 function parseConfig()
449 $options = &PEAR::getStaticProperty('DB_DataObject','options');
451 if (isset($options['modtables'])) {
452 $this->modtables = $options['modtables'];
453 $this->modmap = $options['modmap'];
454 $this->modsql = $options['modsql'];
457 $ff = HTML_Flexyframework::get();
458 $dirs = array($ff->page->rootDir.'/Pman/DataObjects'); // not used anymore!
459 foreach($this->modtables as $m=>$ts) {
460 $dirs[] = $ff->page->rootDir.'/Pman/'.$m.'/DataObjects';
463 //echo '<PRE>';print_R($ini);//exit;
470 //print_r( $this->def['readers'][$table]);
471 // print_r( $this->def['colmodels'][$table]);
472 //print_r($this->def['readers'][$table]); exit;
476 function writeFileEx($n, $f, $str)
478 if (file_exists($f)) {
479 // all - will not overwrite stuff.. (only being specific willl)
480 if (!in_array($n, $this->overwrite)) {
481 $this->writeFile($f.'.generated',$str);
485 $this->writeFile($f,$str);
489 function writeFile($f, $str)
491 require_once 'System.php';
492 System::mkdir(array('-p', dirname($f)));
495 file_put_contents($f, $str);