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_Builder_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($ff); //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() { } var $jsHeader = "//