From 85a3208394f29c9d35039ae2781bfb0e18fcc713 Mon Sep 17 00:00:00 2001 From: Alan Knowles Date: Mon, 6 Sep 2010 13:45:31 +0800 Subject: [PATCH] Generator.php --- Generator.php | 612 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 612 insertions(+) diff --git a/Generator.php b/Generator.php index e69de29b..eb87b55d 100644 --- a/Generator.php +++ b/Generator.php @@ -0,0 +1,612 @@ +def['order'][$table][] = $t->name; +$this->def['readers'][$table][$t->name] = $reader; +$this->def['colmodels'][$table][$t->name] = $colmodel; +$this->def['forms'][$table][$t->name] = $form; +* +* Readers +* readersDef[table.col] +* +* +* +* **/ + + + +class Pman_Core_Generator extends DB_DataObject_Generator +{ + + + + // inherrited.. + // $tablekeys + // $tables + // $_definitions + /** + * def[order] + * [tablename] => array( list of columns ones with '-' indicate lookup + * [readers] + * [tablename][colname] -> reader foramt + * [forms] + * [tablename][colname] => xtype / name etc... + * [readerArgs] + * [tablename] => data for reader args (eg. id / total prop etc.) + * readers => + * [tablename] => array of cols with types + * forms => + * [tablename] -> array of cols + * + */ + var $def; + + + var $page = false; // page container when run from cli. + + // dont do usual stuff!!! + var $rootDir = ''; + var $tablekeys = array(); + + var $overwrite = array(); // default dont overwrite any of the files.. + // array('master', 'corejs', 'corephp', 'index', 'Roo') + // and ('js-?????' where ??? is the table name) <- for all the generated js classes. + // we always overwrite the definition!!! + // set to array('all') to overwrite everything!!! + + function start($cli=false, $mods='', $overwrite='') + { + + $ff = HTML_Flexyframework::get(); + $this->scanModules(); + //echo '
'; print_r($this->modtables); exit;
+        
+        $options = &PEAR::getStaticProperty('DB_DataObject','options');
+        
+        
+        $proj = 'pman'; //ucfirst(basename($options['database']));
+        // we are going to generate all of the code into a temporay foldler..
+        $user = posix_getpwuid(posix_getuid());
+        
+        $options['rootDir'] = ini_get('session.save_path').'/temp_'. $proj.'_'. $user['name'];
+        $options['cli'] = $cli;
+        $options['mods'] = empty($mods) ? array() : explode('/',$mods);
+       
+        if (!file_exists($options['rootDir'])) {
+            mkdir($options['rootDir'], 0775, true);
+        }
+        
+        $this->rootDir = $options['rootDir'];
+        $options['schema_location'] =  $this->rootDir .'/'.$proj.'/DataObjects';
+        $options['class_location'] = $this->rootDir .'/'.$proj.'/DataObjects';
+        $options['require_prefix'] =    $proj . '/DataObjects/';
+        $options['class_prefix'] =    $proj . '_DataObjects_';
+       //  print_r($this);exit;
+       
+       
+        $this->importSQL();
+       
+        $standard_database = $options['database'];
+       
+       
+       
+       
+       
+       
+       
+       
+       
+        parent::start();
+        
+        $this->scanModules();
+        require_once 'System.php';
+        $diff = System::which('diff');
+        // now for each of the directories copy/show diffs..
+        echo $cli ? '' : '
';
+        $flist = explode(',', $overwrite);
+        foreach($this->modtables as $m=>$ar) {
+            if ($options['database'] !=  $standard_database) {
+                $options['database'] =  $standard_database ;
+                
+                parent::start();
+            }
+            
+            $options['database'] =  $standard_database ;
+            if (isset($options['database_'. $m])) {
+                $options['database'] =  $options['database_'. $m];
+                //var_dump($url);exit;
+                
+                // start again?
+                parent::start();
+            }
+            
+            
+            if (!empty($options['mods'] ) && !in_array($m,  $options['mods'] )) {
+                continue;
+            }
+            foreach(scandir($options['rootDir'].'/'.$m) as $f) {
+                if (!strlen($f) || $f[0] == '.') {
+                    continue;
+                }
+                // does it exist!!!
+                $src = $options['rootDir']."/$m/$f";
+                $tg = $ff->page->rootDir."/Pman/$m/DataObjects/$f";
+                if (preg_match('/\.js$/', $f)) {
+                    $tg = $ff->page->rootDir."/Pman/$m/$f";
+                }
+                
+                if (!file_exists($tg) || !filesize($tg) ) {
+                  
+                    if ($cli && in_array($f, $flist)) {
+                       echo "COPY $src $tg" . ($cli ? "\n" : "
"); + copy($src, $tg); + continue; + } + echo "!!!!MISSING!!! $tg" . ($cli ? "\n" : "
"); + + continue; + } + // always copy readers and ini file.= nope - not on live.. + if ($cli && in_array($f, $flist)) { + + //|| $f=='pman.ini' || preg_match('/\.js$/', $f))) { + echo "COPY $src $tg". ($cli ? "\n" : "
"); + copy($src, $tg); + continue; + } + + // diff the two.. + $cmd = "$diff -u -w ". escapeshellarg($tg) . ' ' . escapeshellarg($src); + + $out = array(); $ret = 0; + exec($cmd, $out, $ret); + if ($ret ==0) { // files match.. + continue; + } + // var_dump($ret); + echo "\n" .implode( "\n" , $out) . "\n"; + + + } + + + } + + + + + } + + + + function importSQL() + { + $options = &PEAR::getStaticProperty('DB_DataObject','options'); + + $ff = HTML_Flexyframework::get(); + + $url = parse_url($options['database']); + // hide stuff for web.. + $cli = $options['cli']; + if (!$cli) { + $url['pass'] = '*****'; + $url['user'] = '*****'; + $url['host'] = '*****'; + } + + + + require_once 'System.php'; + $cat = System::which('cat'); + $mysql = System::which('mysql'); + print_r($options['mods'] ); + foreach($this->modsql as $m => $fl) + { + if ($cli && isset($options['database_'. $m])) { + $url =parse_url($options['database_'.$m]); + } + + $mysql_cmd = $mysql . + ' -h ' . $url['host'] . + ' -u' . escapeshellarg($url['user']) . + (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass']) : '') . + ' ' . basename($url['path']); + + echo $mysql_cmd . "\n" ; + + if (!empty($options['mods'] ) && !in_array($m, $options['mods'] )) { + continue; + } + + foreach($fl as $f) { + $fn = $ff->page->rootDir. "/Pman/$m/DataObjects/$f"; + $cmd = $cat . ' ' . escapeshellarg($fn) . " | $mysql_cmd -f "; + echo $cmd. ($cli ? "\n" : "
\n"); + if ($cli) { + passthru($cmd); + } + + } + } + + + + } + /** + * Scan the folders for DataObjects + * - Use the list of php files in DataObjects folders + * to determine which module owns which database table. + * + */ + + + function scanModules() + { + + $options = &PEAR::getStaticProperty('DB_DataObject','options'); + if (isset($options['modtables'])) { + $this->modtables = $options['modtables']; + $this->modmap = $options['modmap']; + $this->modsql = $options['modsql']; + return; + } + + $ff = HTML_Flexyframework::get(); + + $top = $ff->page->rootDir .'/Pman'; + $this->modtables = array(); + $this->modmap = array(); + $this->modmapsql = array(); + + foreach(scandir($top) as $m) { + + if (!strlen($m) || + $m[0] == '.' || + !is_dir($top .'/'.$m) || + !file_exists($top .'/'.$m.'/DataObjects') + ) { + continue; + } + $this->modtables[$m] = array(); + $this->modsql[$m] = array(); + foreach(scandir($top .'/'.$m.'/DataObjects') as $f) { + if (!strlen($f) || $m[0] == '.') { + continue; + } + if (preg_match('/\.sql$/', $f)) { + $this->modsql[$m][] = $f; + } + + if (preg_match('/\.php$/', $f)) { + $tn = strtolower(preg_replace('/\.php$/', '', $f)); + $this->modtables[$m][] = $tn; + $this->modmap[$tn] = $m; + continue; + } + } + } + $options['modtables'] = $this->modtables; + $options['modmap'] = $this->modmap; + $options['modsql'] = $this->modsql; + // print_r($options); + + } + /** + * + * this is run first, so picks up any missing dataobject files.. + */ + + function generateDefinitions() + { + if (!$this->tables) { + $this->debug("-- NO TABLES -- \n"); + return; + } + if (!isset($this->modmap)) { + $this->scanModules(); + } + $options = &PEAR::getStaticProperty('DB_DataObject','options'); + $builder_options = PEAR::getStaticProperty('Pman_Builder','options'); + $ignore = empty($builder_options['skip_tables']) ? array() : $builder_options['skip_tables']; + + $mods = $options['mods']; + $inis = array(); + $this->_newConfig = ''; + foreach($this->tables as $this->table) { + + $tn = strtolower($this->table); + //print_r($this->modmap);//[$tn]);// + + + + if (!isset($this->modmap[$tn])) { + + if (in_array($this->table, $ignore)) { + continue; + } + + + die("No existing DataObject file found for table {$this->table} + +- either add it to Pman_Builder[skip_tables] or\n +- create an empty file in the related Module/DataObjects directory +eg. +touch Pman/????/DataObjects/".ucfirst($this->table).".php + +"); + + } + $mod = $this->modmap[$tn]; + $inis[$mod] = isset($inis[$mod]) ? $inis[$mod] : ''; + + + $this->_newConfig = ''; + $this->_generateDefinitionsTable(); + + + $inis[$mod] .= $this->_newConfig; + } + + //echo '
';print_r($this->_inis); exit;
+        $options = PEAR::getStaticProperty('DB_DataObject','options');
+        
+        $rd = $options['rootDir'];
+        foreach($inis as $m=>$ini) {
+            if (!empty($mods) && !in_array($m, $mods)) {
+                continue;
+            }
+            
+            if (!file_exists($rd.'/'.$m)) {
+                mkdir($rd.'/'.$m, 0775, true);
+            }
+            $fname = '/pman.ini';
+            if (isset($options['database_'. $m])) {
+                $url = parse_url($options['database_'.$m]);
+                $fname = '/'. basename($url['path']).'.ini';
+            }
+
+            
+            file_put_contents($rd.'/'.$m.$fname, $ini);
+        }
+         
+         
+    }
+    
+    function generateClasses() 
+    {
+      // print_R($this->modmap);
+       // die("generateClasses");
+        $options = &PEAR::getStaticProperty('DB_DataObject','options');
+        
+        $ff = HTML_Flexyframework::get();
+        
+        $rd = $options['rootDir'];
+        $mods = $options['mods'];
+        $this->_extends = 'DB_DataObject';
+        $this->_extendsFile = 'DB/DataObject.php';
+        $cli = $options['cli'];
+
+        foreach($this->tables as $this->table) {
+            $this->table        = trim($this->table);
+            $tn  = strtolower($this->table);
+            $mod = $this->modmap[$tn];
+            
+             if (!empty($mods) && !in_array($mod, $mods)) {
+                continue;
+            }
+            
+            
+            $this->classname    = 'Pman_'.$mod . '_DataObjects_'. ucfirst($this->table); // replace odd chars?
+           
+           
+            $outfilename    = $rd.'/'.$mod.'/'. ucfirst($this->table).'.php';
+            $orig           = $ff->page->rootDir .'/Pman/'.$mod.'/DataObjects/'. ucfirst($this->table).'.php';
+            
+           
+                // file_get_contents???
+            $oldcontents = file_get_contents($orig);
+            
+             
+            echo "GENERATE: " .   $this->classname  . ($cli ? "\n" : "
"); + + $out = $this->_generateClassTable($oldcontents); + + // get rid of static GET!!! + $out = preg_replace('/(\n|\r\n)\s*function staticGet[^\n]+(\n|\r\n)/s', '', $out); + $out = preg_replace('#/\* Static get \*/#s', '', $out); + + + // $this->debug( "writing $this->classname\n"); + //$tmpname = tempnam(session_save_path(),'DataObject_'); + file_put_contents($outfilename, $out); + + } + } + + + + // function generateDefinitions() { } + ////function generateForeignKeys() { } + // function generateClasses() { } + + + + + + function parseConfig() + { + $options = &PEAR::getStaticProperty('DB_DataObject','options'); + + if (isset($options['modtables'])) { + $this->modtables = $options['modtables']; + $this->modmap = $options['modmap']; + $this->modsql = $options['modsql']; + } + + $ff = HTML_Flexyframework::get(); + $dirs = array($ff->page->rootDir.'/Pman/DataObjects'); // not used anymore! + foreach($this->modtables as $m=>$ts) { + $dirs[] = $ff->page->rootDir.'/Pman/'.$m.'/DataObjects'; + } + $ini = array('database__render' => array()); + foreach($dirs as $d) { + if (!file_exists($d.'/pman.links.ini')) { + continue; + } + $in = parse_ini_file($d.'/pman.links.ini',true); + $r = array(); + if (isset($in['database__render'])) { + $r = $in['database__render']; + unset($in['database__render']); + } + $ini = array_merge($ini, $in); + $ini['database__render'] = array_merge($ini['database__render'] , $r); + } + //echo '
';print_R($ini);//exit;
+        
+        
+        if (!isset($ini['database__render'])) {
+            die("database__render not available in links files.");
+            return;
+        }
+        $this->mapcols = array();
+        foreach($ini as $tab=>$conf) {
+            if ($tab == 'database__render') {
+                continue;
+            }
+            $this->mergeConfig($tab,$conf,$ini['database__render']);
+             
+        }
+        $this->renderMap = $ini['database__render'];
+    }
+    function mergeConfig($table, $conf, $render)
+    {
+        $this->mapcols[$table] = array();
+        $options = &PEAR::getStaticProperty('DB_DataObject','options');
+        if (isset($options['modtables'])) {
+            $this->modtables = $options['modtables'];
+            $this->modmap = $options['modmap'];
+            $this->modsql = $options['modsql'];
+        }
+        
+        
+        foreach($conf as $ocol=>$info) {
+            // format col => showval..
+            //list($rtc, $rshow) = explode(':', $info);
+            list($tab,$col) = explode(':', $info);
+            //print_r($render);
+            $rshow = $render[$tab];
+            
+            $this->mapcols[$table][$ocol] = array('table'=>$tab, 'col' => $col);
+            
+            // for the grid...
+            
+            // reader:
+            //- just add an extra line..
+            if (!isset($this->def['readers'][$tab][$rshow])){
+                echo "WARNING in links.ini TABLE $tab does not have renderer $rshow 
\n"; + continue; + } + + // for the readers.. we need to merge all the columns in the left to the right... + + // table => original + // ocol => column in table + // tab => remote table + // col => right col linked to... + + $rdef = $this->_definitions[$tab]; + + + foreach($rdef as $t) { + //copy typedata from old coll + $this->def['readers'][$table][$ocol.'-'. $t->name] = $this->def['readers'][$tab][$t->name]; + $this->def['readers'][$table][$ocol.'-'. $t->name]['name'] = $ocol.'_'. $t->name; + } + + + + + + // remove the def column from the id one.. + if (isset($this->def['colmodels'][$table][$ocol])) { + unset($this->def['colmodels'][$table][$ocol]); + } + $this->def['colmodels'][$table][$ocol.'-'. $rshow] = + $this->def['colmodels'][$tab][$rshow]; + + // change the header name (merge of two..) + list($colname,) = explode('_',$ocol,2); + + $this->def['colmodels'][$table][$ocol.'-'. $rshow]['dataIndex'] = $ocol.'_'. $rshow; + $this->def['colmodels'][$table][$ocol.'-'. $rshow]['id'] = $ocol.'-'. $rshow; + + $this->def['colmodels'][$table][$ocol.'-'. $rshow]['header'] = ucwords($colname . ' ' . + $this->def['colmodels'][$tab][$rshow]['header']); + + // last of all add replace the old $col, with + $p = array_search($ocol, empty($this->def['order'][$table]) ? array() : $this->def['order'][$table]); + $this->def['order'][$table][$p] = $ocol.'-'. $rshow; + $this->def['order'][$table][] = $ocol; + + // --- now for forms!!!! + + + } + //var_dump($table); + //print_r( $this->def['readers'][$table]); + // print_r( $this->def['colmodels'][$table]); + //print_r($this->def['readers'][$table]); exit; + + } + + + function writeFileEx($n, $f, $str) + { + if (file_exists($f)) { + // all - will not overwrite stuff.. (only being specific willl) + if (!in_array($n, $this->overwrite)) { + $this->writeFile($f.'.generated',$str); + return; + } + } + $this->writeFile($f,$str); + + + } + function writeFile($f, $str) + { + require_once 'System.php'; + System::mkdir(array('-p', dirname($f))); + // overwrite??? + echo "write: $f\n"; + file_put_contents($f, $str); + } + +} + -- 2.39.2