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
48 static $cli_desc = "Generate the dataobjects";
57 * [tablename] => array( list of columns ones with '-' indicate lookup
59 * [tablename][colname] -> reader foramt
61 * [tablename][colname] => xtype / name etc...
63 * [tablename] => data for reader args (eg. id / total prop etc.)
65 * [tablename] => array of cols with types
67 * [tablename] -> array of cols
73 var $page = false; // page container when run from cli.
75 // dont do usual stuff!!!
77 var $tablekeys = array();
79 var $overwrite = array(); // default dont overwrite any of the files..
80 // array('master', 'corejs', 'corephp', 'index', 'Roo')
81 // and ('js-?????' where ??? is the table name) <- for all the generated js classes.
82 // we always overwrite the definition!!!
83 // set to array('all') to overwrite everything!!!
85 function start($cli=false, $mods='', $overwrite=array())
88 $ff = HTML_Flexyframework::get();
90 //echo '<PRE>'; print_r($this->modtables); exit;
92 $options = &PEAR::getStaticProperty('DB_DataObject','options');
95 $proj = 'pman'; //ucfirst(basename($options['database']));
96 // we are going to generate all of the code into a temporay foldler..
97 $user = posix_getpwuid(posix_getuid());
99 $options['rootDir'] = ini_get('session.save_path').'/temp_'. $proj.'_'. $user['name'];
100 $options['cli'] = $cli;
101 $options['mods'] = empty($mods) ? array() : explode('/',$mods);
103 if (!file_exists($options['rootDir'])) {
104 mkdir($options['rootDir'], 0775, true);
107 $this->rootDir = $options['rootDir'];
108 $options['schema_location'] = $this->rootDir .'/'.$proj.'/DataObjects';
109 $options['class_location'] = $this->rootDir .'/'.$proj.'/DataObjects';
110 $options['require_prefix'] = $proj . '/DataObjects/';
111 $options['class_prefix'] = $proj . '_DataObjects_';
112 // print_r($this);exit;
115 $standard_database = $options['database'];
127 $this->scanModules();
128 require_once 'System.php';
129 $diff = System::which('diff');
130 // now for each of the directories copy/show diffs..
131 echo $cli ? '' : '<PRE>';
133 foreach($this->modtables as $m=>$ar) {
134 if ($options['database'] != $standard_database) {
135 $options['database'] = $standard_database ;
140 $options['database'] = $standard_database ;
141 if (isset($options['database_'. $m])) {
142 $options['database'] = $options['database_'. $m];
143 //var_dump($url);exit;
150 if (!empty($options['mods'] ) && !in_array($m, $options['mods'] )) {
153 // this happens when we have no database tables from a module,
154 // but module code has been defined.
155 if (!file_exists($options['rootDir'].'/'.$m)) {
158 foreach(scandir($options['rootDir'].'/'.$m) as $f) {
160 echo "SCAN {$options['rootDir']} $f\n";
162 if (!strlen($f) || $f[0] == '.') {
166 $src = $options['rootDir']."/$m/$f";
167 $tg = $ff->page->rootDir."/Pman/$m/DataObjects/$f";
168 if (preg_match('/\.js$/', $f)) {
169 $tg = $ff->page->rootDir."/Pman/$m/$f";
172 if (!file_exists($tg) || !filesize($tg) ) {
174 if ($cli && file_exists($tg) || in_array($f, $flist) || in_array('_all_', $flist )) {
175 echo "COPY $src $tg" . ($cli ? "\n" : "<BR>");
179 echo "!!!!MISSING!!! $tg" . ($cli ? "\n" : "<BR>");
183 // always copy readers and ini file.= nope - not on live..
184 if ($cli && in_array($f, $flist) || in_array('_all_', $flist )) {
186 //|| $f=='pman.ini' || preg_match('/\.js$/', $f))) {
187 echo "COPY $src $tg". ($cli ? "\n" : "<BR>");
193 $cmd = "$diff -u -w ". escapeshellarg($tg) . ' ' . escapeshellarg($src);
195 $out = array(); $ret = 0;
196 exec($cmd, $out, $ret);
197 if ($ret ==0) { // files match..
201 echo "\n" .implode( "\n" , $out) . "\n";
215 * Scan the folders for DataObjects
216 * - Use the list of php files in DataObjects folders
217 * to determine which module owns which database table.
222 function scanModules()
225 $options = &PEAR::getStaticProperty('DB_DataObject','options');
226 if (isset($options['modtables'])) {
227 $this->modtables = $options['modtables'];
228 $this->modmap = $options['modmap'];
229 $this->modsql = $options['modsql'];
233 $ff = HTML_Flexyframework::get();
235 $top = $ff->page->rootDir .'/Pman';
236 $this->modtables = array();
237 $this->modmap = array();
238 $this->modmapsql = array();
240 foreach(scandir($top) as $m) {
244 !is_dir($top .'/'.$m) ||
245 !file_exists($top .'/'.$m.'/DataObjects')
249 $this->modtables[$m] = array();
250 $this->modsql[$m] = array();
251 foreach(scandir($top .'/'.$m.'/DataObjects') as $f) {
252 if (!strlen($f) || $m[0] == '.') {
255 if (preg_match('/\.sql$/', $f)) {
256 $this->modsql[$m][] = $f;
259 if (preg_match('/\.php$/', $f)) {
260 $tn = strtolower(preg_replace('/\.php$/', '', $f));
261 $this->modtables[$m][] = $tn;
262 $this->modmap[$tn] = $m;
267 $options['modtables'] = $this->modtables;
268 $options['modmap'] = $this->modmap;
269 $options['modsql'] = $this->modsql;
270 // print_r($options);
275 * this is run first, so picks up any missing dataobject files..
278 function generateDefinitions()
280 if (!$this->tables) {
281 $this->debug("-- NO TABLES -- \n");
284 if (!isset($this->modmap)) {
285 $this->scanModules();
287 $options = &PEAR::getStaticProperty('DB_DataObject','options');
288 $builder_options = PEAR::getStaticProperty('Pman_Builder','options');
289 $ignore = empty($builder_options['skip_tables']) ? array() : $builder_options['skip_tables'];
291 $mods = $options['mods'];
293 $this->_newConfig = '';
294 foreach($this->tables as $this->table) {
296 $tn = strtolower($this->table);
297 //print_r($this->modmap);//[$tn]);//
301 if (!isset($this->modmap[$tn])) {
303 if (in_array($this->table, $ignore)) {
309 die("No existing DataObject file found for table {$this->table}
311 - either add it to Pman_Builder[skip_tables] or\n
312 - run generator and specify that module..
313 - create an empty file in the related Module/DataObjects directory
315 touch Pman/????/DataObjects/".ucfirst($this->table).".php
319 // use mods to determine where it should output to..
320 //var_dump($mods);exit;
321 $this->modmap[$tn] = $mods[0];
325 $mod = $this->modmap[$tn];
326 $inis[$mod] = isset($inis[$mod]) ? $inis[$mod] : '';
329 $this->_newConfig = '';
330 $this->_generateDefinitionsTable();
333 $inis[$mod] .= $this->_newConfig;
335 return; // we do not generate in ifiles any more..
339 function generateClasses()
341 // print_R($this->modmap);
342 // die("generateClasses");
343 $options = &PEAR::getStaticProperty('DB_DataObject','options');
345 $ff = HTML_Flexyframework::get();
347 $rd = $options['rootDir'];
348 $mods = $options['mods'];
349 $this->_extends = 'DB_DataObject';
350 $this->_extendsFile = 'DB/DataObject.php';
351 $cli = $options['cli'];
353 foreach($this->tables as $this->table) {
355 $this->table = trim($this->table);
356 $tn = strtolower($this->table);
357 $mod = $this->modmap[$tn];
359 if (!empty($mods) && !in_array($mod, $mods)) {
363 $clean_table = preg_replace('/[^A-Z0-9]+/i','_',ucfirst(trim($this->table)));
365 $this->classname = 'Pman_'.$mod . '_DataObjects_'. $clean_table; // replace odd chars?
368 $outfilename = $rd.'/'.$mod.'/'. $clean_table .'.php';
369 $orig = $ff->page->rootDir .'/Pman/'.$mod.'/DataObjects/'. $clean_table.'.php';
372 // file_get_contents???
374 $oldcontents = file_exists($orig) ? file_get_contents($orig) : '';
377 echo "GENERATE: " . $this->classname . ($cli ? "\n" : "<BR>");
379 $out = $this->_generateClassTable($oldcontents);
381 // get rid of static GET!!!
382 $out = preg_replace('/(\n|\r\n)\s*function staticGet[^\n]+(\n|\r\n)/s', '', $out);
383 $out = preg_replace('#/\* Static get \*/#s', '', $out);
385 if (!file_exists(dirname($outfilename))) {
386 mkdir(dirname($outfilename), 0755, true);
388 // $this->debug( "writing $this->classname\n");
389 //$tmpname = tempnam(session_save_path(),'DataObject_');
390 file_put_contents($outfilename, $out);
397 // function generateDefinitions() { }
398 ////function generateForeignKeys() { }
399 // function generateClasses() { }
405 function parseConfig()
407 $options = &PEAR::getStaticProperty('DB_DataObject','options');
409 if (isset($options['modtables'])) {
410 $this->modtables = $options['modtables'];
411 $this->modmap = $options['modmap'];
412 $this->modsql = $options['modsql'];
415 $ff = HTML_Flexyframework::get();
416 $dirs = array($ff->page->rootDir.'/Pman/DataObjects'); // not used anymore!
417 foreach($this->modtables as $m=>$ts) {
418 $dirs[] = $ff->page->rootDir.'/Pman/'.$m.'/DataObjects';
421 //echo '<PRE>';print_R($ini);//exit;
428 //print_r( $this->def['readers'][$table]);
429 // print_r( $this->def['colmodels'][$table]);
430 //print_r($this->def['readers'][$table]); exit;
434 function writeFileEx($n, $f, $str)
436 if (file_exists($f)) {
437 // all - will not overwrite stuff.. (only being specific willl)
438 if (!in_array($n, $this->overwrite)) {
439 $this->writeFile($f.'.generated',$str);
443 $this->writeFile($f,$str);
447 function writeFile($f, $str)
449 require_once 'System.php';
450 System::mkdir(array('-p', dirname($f)));
453 file_put_contents($f, $str);