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
52 die("do not use this directly..");
62 * [tablename] => array( list of columns ones with '-' indicate lookup
64 * [tablename][colname] -> reader foramt
66 * [tablename][colname] => xtype / name etc...
68 * [tablename] => data for reader args (eg. id / total prop etc.)
70 * [tablename] => array of cols with types
72 * [tablename] -> array of cols
78 var $page = false; // page container when run from cli.
80 // dont do usual stuff!!!
82 var $tablekeys = array();
84 var $overwrite = array(); // default dont overwrite any of the files..
85 // array('master', 'corejs', 'corephp', 'index', 'Roo')
86 // and ('js-?????' where ??? is the table name) <- for all the generated js classes.
87 // we always overwrite the definition!!!
88 // set to array('all') to overwrite everything!!!
90 function start($cli=false, $mods='', $overwrite=array())
93 $ff = HTML_Flexyframework::get();
95 //echo '<PRE>'; print_r($this->modtables); exit;
97 $options = &PEAR::getStaticProperty('DB_DataObject','options');
100 $proj = 'pman'; //ucfirst(basename($options['database']));
101 // we are going to generate all of the code into a temporay foldler..
102 $user = posix_getpwuid(posix_getuid());
104 $options['rootDir'] = ini_get('session.save_path').'/temp_'. $proj.'_'. $user['name'];
105 $options['cli'] = $cli;
106 $options['mods'] = empty($mods) ? array() : explode('/',$mods);
108 if (!file_exists($options['rootDir'])) {
109 mkdir($options['rootDir'], 0775, true);
112 $this->rootDir = $options['rootDir'];
113 $options['schema_location'] = $this->rootDir .'/'.$proj.'/DataObjects';
114 $options['class_location'] = $this->rootDir .'/'.$proj.'/DataObjects';
115 $options['require_prefix'] = $proj . '/DataObjects/';
116 $options['class_prefix'] = $proj . '_DataObjects_';
117 // print_r($this);exit;
120 $standard_database = $options['database'];
132 $this->scanModules();
133 require_once 'System.php';
134 $diff = System::which('diff');
135 // now for each of the directories copy/show diffs..
136 echo $cli ? '' : '<PRE>';
138 foreach($this->modtables as $m=>$ar) {
139 if ($options['database'] != $standard_database) {
140 $options['database'] = $standard_database ;
145 $options['database'] = $standard_database ;
146 if (isset($options['database_'. $m])) {
147 $options['database'] = $options['database_'. $m];
148 //var_dump($url);exit;
155 if (!empty($options['mods'] ) && !in_array($m, $options['mods'] )) {
158 // this happens when we have no database tables from a module,
159 // but module code has been defined.
160 if (!file_exists($options['rootDir'].'/'.$m)) {
163 foreach(scandir($options['rootDir'].'/'.$m) as $f) {
165 echo "SCAN {$options['rootDir']} $f\n";
167 if (!strlen($f) || $f[0] == '.') {
171 $src = $options['rootDir']."/$m/$f";
172 $tg = $ff->page->rootDir."/Pman/$m/DataObjects/$f";
173 if (preg_match('/\.js$/', $f)) {
174 $tg = $ff->page->rootDir."/Pman/$m/$f";
177 if (!file_exists($tg) || !filesize($tg) ) {
179 if ($cli && file_exists($tg) || in_array($f, $flist) || in_array('_all_', $flist )) {
180 echo "COPY $src $tg" . ($cli ? "\n" : "<BR>");
184 echo "!!!!MISSING!!! $tg" . ($cli ? "\n" : "<BR>");
188 // always copy readers and ini file.= nope - not on live..
189 if ($cli && in_array($f, $flist) || in_array('_all_', $flist )) {
191 //|| $f=='pman.ini' || preg_match('/\.js$/', $f))) {
192 echo "COPY $src $tg". ($cli ? "\n" : "<BR>");
198 $cmd = "$diff -u -w ". escapeshellarg($tg) . ' ' . escapeshellarg($src);
200 $out = array(); $ret = 0;
201 exec($cmd, $out, $ret);
202 if ($ret ==0) { // files match..
206 echo "\n" .implode( "\n" , $out) . "\n";
220 * Scan the folders for DataObjects
221 * - Use the list of php files in DataObjects folders
222 * to determine which module owns which database table.
227 function scanModules()
230 $options = &PEAR::getStaticProperty('DB_DataObject','options');
231 if (isset($options['modtables'])) {
232 $this->modtables = $options['modtables'];
233 $this->modmap = $options['modmap'];
234 $this->modsql = $options['modsql'];
238 $ff = HTML_Flexyframework::get();
240 $top = $ff->page->rootDir .'/Pman';
241 $this->modtables = array();
242 $this->modmap = array();
243 $this->modmapsql = array();
245 foreach(scandir($top) as $m) {
249 !is_dir($top .'/'.$m) ||
250 !file_exists($top .'/'.$m.'/DataObjects')
254 $this->modtables[$m] = array();
255 $this->modsql[$m] = array();
256 foreach(scandir($top .'/'.$m.'/DataObjects') as $f) {
257 if (!strlen($f) || $m[0] == '.') {
260 if (preg_match('/\.sql$/', $f)) {
261 $this->modsql[$m][] = $f;
264 if (preg_match('/\.php$/', $f)) {
265 $tn = strtolower(preg_replace('/\.php$/', '', $f));
266 $this->modtables[$m][] = $tn;
267 $this->modmap[$tn] = $m;
272 $options['modtables'] = $this->modtables;
273 $options['modmap'] = $this->modmap;
274 $options['modsql'] = $this->modsql;
275 // print_r($options);
280 * this is run first, so picks up any missing dataobject files..
283 function generateDefinitions()
285 if (!$this->tables) {
286 $this->debug("-- NO TABLES -- \n");
289 if (!isset($this->modmap)) {
290 $this->scanModules();
292 $options = &PEAR::getStaticProperty('DB_DataObject','options');
293 $builder_options = PEAR::getStaticProperty('Pman_Builder','options');
294 $ignore = empty($builder_options['skip_tables']) ? array() : $builder_options['skip_tables'];
296 $mods = $options['mods'];
298 $this->_newConfig = '';
299 foreach($this->tables as $this->table) {
301 $tn = strtolower($this->table);
302 //print_r($this->modmap);//[$tn]);//
306 if (!isset($this->modmap[$tn])) {
308 if (in_array($this->table, $ignore)) {
314 die("No existing DataObject file found for table {$this->table}
316 - either add it to Pman_Builder[skip_tables] or\n
317 - run generator and specify that module..
318 - create an empty file in the related Module/DataObjects directory
320 touch Pman/????/DataObjects/".ucfirst($this->table).".php
324 // use mods to determine where it should output to..
325 //var_dump($mods);exit;
326 $this->modmap[$tn] = $mods[0];
330 $mod = $this->modmap[$tn];
331 $inis[$mod] = isset($inis[$mod]) ? $inis[$mod] : '';
334 $this->_newConfig = '';
335 $this->_generateDefinitionsTable();
338 $inis[$mod] .= $this->_newConfig;
340 return; // we do not generate in ifiles any more..
344 function generateClasses()
346 // print_R($this->modmap);
347 // die("generateClasses");
348 $options = &PEAR::getStaticProperty('DB_DataObject','options');
350 $ff = HTML_Flexyframework::get();
352 $rd = $options['rootDir'];
353 $mods = $options['mods'];
354 $this->_extends = 'DB_DataObject';
355 $this->_extendsFile = 'DB/DataObject.php';
356 $cli = $options['cli'];
358 foreach($this->tables as $this->table) {
360 $this->table = trim($this->table);
361 $tn = strtolower($this->table);
362 $mod = $this->modmap[$tn];
364 if (!empty($mods) && !in_array($mod, $mods)) {
368 $clean_table = preg_replace('/[^A-Z0-9]+/i','_',ucfirst(trim($this->table)));
370 $this->classname = 'Pman_'.$mod . '_DataObjects_'. $clean_table; // replace odd chars?
373 $outfilename = $rd.'/'.$mod.'/'. $clean_table .'.php';
374 $orig = $ff->page->rootDir .'/Pman/'.$mod.'/DataObjects/'. $clean_table.'.php';
377 // file_get_contents???
379 $oldcontents = file_exists($orig) ? file_get_contents($orig) : '';
382 echo "GENERATE: " . $this->classname . ($cli ? "\n" : "<BR>");
384 $out = $this->_generateClassTable($oldcontents);
386 // get rid of static GET!!!
387 $out = preg_replace('/(\n|\r\n)\s*function staticGet[^\n]+(\n|\r\n)/s', '', $out);
388 $out = preg_replace('#/\* Static get \*/#s', '', $out);
390 if (!file_exists(dirname($outfilename))) {
391 mkdir(dirname($outfilename), 0755, true);
393 // $this->debug( "writing $this->classname\n");
394 //$tmpname = tempnam(session_save_path(),'DataObject_');
395 file_put_contents($outfilename, $out);
402 // function generateDefinitions() { }
403 ////function generateForeignKeys() { }
404 // function generateClasses() { }
410 function parseConfig()
412 $options = &PEAR::getStaticProperty('DB_DataObject','options');
414 if (isset($options['modtables'])) {
415 $this->modtables = $options['modtables'];
416 $this->modmap = $options['modmap'];
417 $this->modsql = $options['modsql'];
420 $ff = HTML_Flexyframework::get();
421 $dirs = array($ff->page->rootDir.'/Pman/DataObjects'); // not used anymore!
422 foreach($this->modtables as $m=>$ts) {
423 $dirs[] = $ff->page->rootDir.'/Pman/'.$m.'/DataObjects';
426 //echo '<PRE>';print_R($ini);//exit;
433 //print_r( $this->def['readers'][$table]);
434 // print_r( $this->def['colmodels'][$table]);
435 //print_r($this->def['readers'][$table]); exit;
439 function writeFileEx($n, $f, $str)
441 if (file_exists($f)) {
442 // all - will not overwrite stuff.. (only being specific willl)
443 if (!in_array($n, $this->overwrite)) {
444 $this->writeFile($f.'.generated',$str);
448 $this->writeFile($f,$str);
452 function writeFile($f, $str)
454 require_once 'System.php';
455 System::mkdir(array('-p', dirname($f)));
458 file_put_contents($f, $str);