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=array())
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;
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>';
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 // this happens when we have no database tables from a module,
153 // but module code has been defined.
154 if (!file_exists($options['rootDir'].'/'.$m)) {
157 foreach(scandir($options['rootDir'].'/'.$m) as $f) {
158 if (!strlen($f) || $f[0] == '.') {
162 $src = $options['rootDir']."/$m/$f";
163 $tg = $ff->page->rootDir."/Pman/$m/DataObjects/$f";
164 if (preg_match('/\.js$/', $f)) {
165 $tg = $ff->page->rootDir."/Pman/$m/$f";
168 if (!file_exists($tg) || !filesize($tg) ) {
170 if ($cli && file_exists($tg) || in_array($f, $flist) || in_array('_all_', $flist )) {
171 echo "COPY $src $tg" . ($cli ? "\n" : "<BR>");
175 echo "!!!!MISSING!!! $tg" . ($cli ? "\n" : "<BR>");
179 // always copy readers and ini file.= nope - not on live..
180 if ($cli && in_array($f, $flist) || in_array('_all_', $flist )) {
182 //|| $f=='pman.ini' || preg_match('/\.js$/', $f))) {
183 echo "COPY $src $tg". ($cli ? "\n" : "<BR>");
189 $cmd = "$diff -u -w ". escapeshellarg($tg) . ' ' . escapeshellarg($src);
191 $out = array(); $ret = 0;
192 exec($cmd, $out, $ret);
193 if ($ret ==0) { // files match..
197 echo "\n" .implode( "\n" , $out) . "\n";
211 * Scan the folders for DataObjects
212 * - Use the list of php files in DataObjects folders
213 * to determine which module owns which database table.
218 function scanModules()
221 $options = &PEAR::getStaticProperty('DB_DataObject','options');
222 if (isset($options['modtables'])) {
223 $this->modtables = $options['modtables'];
224 $this->modmap = $options['modmap'];
225 $this->modsql = $options['modsql'];
229 $ff = HTML_Flexyframework::get();
231 $top = $ff->page->rootDir .'/Pman';
232 $this->modtables = array();
233 $this->modmap = array();
234 $this->modmapsql = array();
236 foreach(scandir($top) as $m) {
240 !is_dir($top .'/'.$m) ||
241 !file_exists($top .'/'.$m.'/DataObjects')
245 $this->modtables[$m] = array();
246 $this->modsql[$m] = array();
247 foreach(scandir($top .'/'.$m.'/DataObjects') as $f) {
248 if (!strlen($f) || $m[0] == '.') {
251 if (preg_match('/\.sql$/', $f)) {
252 $this->modsql[$m][] = $f;
255 if (preg_match('/\.php$/', $f)) {
256 $tn = strtolower(preg_replace('/\.php$/', '', $f));
257 $this->modtables[$m][] = $tn;
258 $this->modmap[$tn] = $m;
263 $options['modtables'] = $this->modtables;
264 $options['modmap'] = $this->modmap;
265 $options['modsql'] = $this->modsql;
266 // print_r($options);
271 * this is run first, so picks up any missing dataobject files..
274 function generateDefinitions()
276 if (!$this->tables) {
277 $this->debug("-- NO TABLES -- \n");
280 if (!isset($this->modmap)) {
281 $this->scanModules();
283 $options = &PEAR::getStaticProperty('DB_DataObject','options');
284 $builder_options = PEAR::getStaticProperty('Pman_Builder','options');
285 $ignore = empty($builder_options['skip_tables']) ? array() : $builder_options['skip_tables'];
287 $mods = $options['mods'];
289 $this->_newConfig = '';
290 foreach($this->tables as $this->table) {
292 $tn = strtolower($this->table);
293 //print_r($this->modmap);//[$tn]);//
297 if (!isset($this->modmap[$tn])) {
299 if (in_array($this->table, $ignore)) {
305 die("No existing DataObject file found for table {$this->table}
307 - either add it to Pman_Builder[skip_tables] or\n
308 - run generator and specify that module..
309 - create an empty file in the related Module/DataObjects directory
311 touch Pman/????/DataObjects/".ucfirst($this->table).".php
315 // use mods to determine where it should output to..
316 //var_dump($mods);exit;
317 $this->modmap[$tn] = $mods[0];
321 $mod = $this->modmap[$tn];
322 $inis[$mod] = isset($inis[$mod]) ? $inis[$mod] : '';
325 $this->_newConfig = '';
326 $this->_generateDefinitionsTable();
329 $inis[$mod] .= $this->_newConfig;
331 return; // we do not generate in ifiles any more..
335 function generateClasses()
337 // print_R($this->modmap);
338 // die("generateClasses");
339 $options = &PEAR::getStaticProperty('DB_DataObject','options');
341 $ff = HTML_Flexyframework::get();
343 $rd = $options['rootDir'];
344 $mods = $options['mods'];
345 $this->_extends = 'DB_DataObject';
346 $this->_extendsFile = 'DB/DataObject.php';
347 $cli = $options['cli'];
349 foreach($this->tables as $this->table) {
350 $this->table = trim($this->table);
351 $tn = strtolower($this->table);
352 $mod = $this->modmap[$tn];
354 if (!empty($mods) && !in_array($mod, $mods)) {
359 $this->classname = 'Pman_'.$mod . '_DataObjects_'. preg_replace('/[^A-Z0-9]+/i','_',ucfirst(trim($this->table)));; // replace odd chars?
362 $outfilename = $rd.'/'.$mod.'/'. ucfirst($this->table).'.php';
363 $orig = $ff->page->rootDir .'/Pman/'.$mod.'/DataObjects/'. ucfirst($this->table).'.php';
366 // file_get_contents???
368 $oldcontents = file_exists($orig) ? file_get_contents($orig) : '';
371 echo "GENERATE: " . $this->classname . ($cli ? "\n" : "<BR>");
373 $out = $this->_generateClassTable($oldcontents);
375 // get rid of static GET!!!
376 $out = preg_replace('/(\n|\r\n)\s*function staticGet[^\n]+(\n|\r\n)/s', '', $out);
377 $out = preg_replace('#/\* Static get \*/#s', '', $out);
379 if (!file_exists(dirname($outfilename))) {
380 mkdir(dirname($outfilename), 0755, true);
382 // $this->debug( "writing $this->classname\n");
383 //$tmpname = tempnam(session_save_path(),'DataObject_');
384 file_put_contents($outfilename, $out);
391 // function generateDefinitions() { }
392 ////function generateForeignKeys() { }
393 // function generateClasses() { }
399 function parseConfig()
401 $options = &PEAR::getStaticProperty('DB_DataObject','options');
403 if (isset($options['modtables'])) {
404 $this->modtables = $options['modtables'];
405 $this->modmap = $options['modmap'];
406 $this->modsql = $options['modsql'];
409 $ff = HTML_Flexyframework::get();
410 $dirs = array($ff->page->rootDir.'/Pman/DataObjects'); // not used anymore!
411 foreach($this->modtables as $m=>$ts) {
412 $dirs[] = $ff->page->rootDir.'/Pman/'.$m.'/DataObjects';
415 //echo '<PRE>';print_R($ini);//exit;
422 //print_r( $this->def['readers'][$table]);
423 // print_r( $this->def['colmodels'][$table]);
424 //print_r($this->def['readers'][$table]); exit;
428 function writeFileEx($n, $f, $str)
430 if (file_exists($f)) {
431 // all - will not overwrite stuff.. (only being specific willl)
432 if (!in_array($n, $this->overwrite)) {
433 $this->writeFile($f.'.generated',$str);
437 $this->writeFile($f,$str);
441 function writeFile($f, $str)
443 require_once 'System.php';
444 System::mkdir(array('-p', dirname($f)));
447 file_put_contents($f, $str);