HTML/FlexyFramework/Generator.php
[pear] / HTML / FlexyFramework / Generator.php
index d73079b..9f35ab4 100644 (file)
@@ -7,9 +7,17 @@ require_once 'DB/DataObject/Generator.php';
 class HTML_FlexyFramework_Generator extends DB_DataObject_Generator 
 {
     // block class generation.
+    static $generateClasses = false;
+    
     function generateClasses()
     {
-        return;
+//        echo "GENERATE CLASSES?";
+        if (!HTML_FlexyFramework_Generator::$generateClasses ) {
+            return;
+        }
+       
+        //echo "GENERATE CLASSES?";
+        parent::generateClasses();
     }
     
     
@@ -26,13 +34,17 @@ class HTML_FlexyFramework_Generator extends DB_DataObject_Generator
             
         }
         //echo '<PRE>';print_r($out);exit;
-        print_r($options);exit;
+         
         file_put_contents($options["ini_{$this->_database}"] . '.reader', serialize($out));
          
     }
+    /**
+     * Generate the cached readers used for meta data in the queries.
+     * 
+     */
     function _generateReader($table)
     {
-         $DB = $this->getDatabaseConnection();
+        $DB = $this->getDatabaseConnection();
         $dbtype = $DB->phptype;
         $def = $this->_definitions[$table] ;
         $ret = array();
@@ -154,4 +166,130 @@ class HTML_FlexyFramework_Generator extends DB_DataObject_Generator
         
         
     }
+    /**
+     * Generate the cached *.ini and links.ini files (merged for all components)
+     * 
+     */
+    static function writeCache($iniCacheTmp, $iniCache)
+    {
+        
+        $fp = fopen($iniCache.".lock", "a+");
+        flock($fp,LOCK_EX);
+
+        $ff = HTML_FlexyFramework::get();
+        $ff->debug('Framework Generator:writeCache ' . $iniCacheTmp .  ' ' . $iniCache);
+          
+        //var_dump($iniCacheTmp);
+       // echo '<PRE>';echo file_get_contents($iniCacheTmp);exit;
+        // only unpdate if nothing went wrong.
+        if (file_exists($iniCacheTmp) && filesize($iniCacheTmp)) {
+            if (file_exists($iniCache)) {
+                unlink($iniCache);
+            }
+            $ff->debug("Writing merged ini file : $iniCache\n");
+            rename($iniCacheTmp, $iniCache);
+        }
+        
+        // readers..??? not needed??? (historical)
+        if (file_exists($iniCacheTmp.'.reader') &&  filesize($iniCacheTmp.'.reader')) {
+            if (file_exists($iniCache.'.reader')) {
+                unlink($iniCache.'.reader');
+            }
+            $ff->debug("Writing merged reader file : $iniCache.reader\n");
+            rename($iniCacheTmp.'.reader', $iniCache.'.reader');
+        }
+        
+        
+        // merge and set links.. test for generated links file..
+        
+        $linksCacheTmp = preg_replace('/\.ini/', '.links.ini', $iniCacheTmp );
+        $links = array();
+        if (file_exists($linksCacheTmp )) {
+            $links = self::mergeIni( parse_ini_file($linksCacheTmp, true), $links);
+            unlink($linksCacheTmp);
+        }
+        // we are going to use the DataObject directories..
+        
+        $inis = explode(PATH_SEPARATOR,$ff->DB_DataObject['class_location']);
+        //print_r($inis);exit;
+        $ff->debug("class_location = ". $ff->DB_DataObject['class_location']);
+        
+        foreach($inis as $path) {
+            $ini = $path . '/'. strtolower( $ff->project) . '.links.ini';
+             //var_dump($ini);
+            if (!file_exists($ini)) {
+                $ff->debug("Framework Generator:writeCache PROJECT.links.ini does not exist in $path - trying glob");
+       
+                // try scanning the directory for another ini file..
+                $ar = glob(dirname($ini).'/*.links.ini');
+                
+                
+                if (empty($ar)) {
+                    continue;
+                }
+                
+                
+                sort($ar);
+                $ff->debug("Framework Generator:writeCache using {$ar[0]}");
+                
+                // first file.. = with links removed..
+                $ini = preg_replace('/\.links\./' , '.', $ar[0]);
+                $ini = preg_replace('/\.ini$/', '.links.ini', $ini);
+            }
+            
+            // why do this twice???
+            if (!file_exists($ini)) {
+                continue;
+            }
+            $ff->debug("Adding in $ini");
+            // prefer first ?
+            $links = self::mergeIni( parse_ini_file($ini, true), $links);   
+        }
+        $iniLinksCache = preg_replace('/\.ini$/', '.links.ini', $iniCache);
+        $out = array();
+        foreach($links as $tbl=>$ar) {
+            $out[] = '['. $tbl  .']';
+            foreach ($ar as $k=>$v) {
+                $out[] = $k . '=' .$v;
+            }
+            $out[] = '';
+        }
+        if (count($out)) {
+            $ff->debug("Writing merged Links file : $iniLinksCache \n");
+
+            $out  = implode("\n", $out);
+            
+            
+            file_put_contents($iniCacheTmp. '.links.ini', implode("\n", $out));
+            if (file_exists($iniLinksCache)) {                
+                unlink($iniLinksCache);
+            }
+            rename($iniCacheTmp. '.links.ini', $iniLinksCache);
+        }
+        
+        flock($fp, LOCK_UN);
+        fclose($fp);
+        unlink($fp);
+        
+    }
+    /* bit like merge recursive, but it avoids doing stuff with arrays.. */
+    static function mergeIni($new, $old) 
+    {
+        foreach($new as $g => $ar) {
+            if (!isset($old[$g])) {
+                $old[$g] = $ar;
+                continue;
+            }
+            foreach($ar as $k=>$v) {
+                if (isset($old[$g][$k])) {
+                    continue;
+                }
+                $old[$g][$k] = $v;
+            }
+        }
+        return $old;
+        
+        
+    }
+    
 }