HTML/FlexyFramework/Generator.php
[pear] / HTML / FlexyFramework / Generator.php
index 2079efc..f868088 100644 (file)
@@ -11,7 +11,7 @@ class HTML_FlexyFramework_Generator extends DB_DataObject_Generator
     
     function generateClasses()
     {
-        //echo "GENERATE CLASSES?";
+//        echo "GENERATE CLASSES?";
         if (!HTML_FlexyFramework_Generator::$generateClasses ) {
             return;
         }
@@ -172,12 +172,17 @@ class HTML_FlexyFramework_Generator extends DB_DataObject_Generator
      */
     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 (filesize($iniCacheTmp)) {
+        if (file_exists($iniCacheTmp) && filesize($iniCacheTmp)) {
             if (file_exists($iniCache)) {
                 unlink($iniCache);
             }
@@ -185,8 +190,8 @@ class HTML_FlexyFramework_Generator extends DB_DataObject_Generator
             rename($iniCacheTmp, $iniCache);
         }
         
-        // readers..
-        if (filesize($iniCacheTmp.'.reader')) {
+        // readers..??? not needed??? (historical)
+        if (file_exists($iniCacheTmp.'.reader') &&  filesize($iniCacheTmp.'.reader')) {
             if (file_exists($iniCache.'.reader')) {
                 unlink($iniCache.'.reader');
             }
@@ -195,27 +200,50 @@ class HTML_FlexyFramework_Generator extends DB_DataObject_Generator
         }
         
         
-        // merge and set links..
+        // merge and set links.. test for generated links file..
         
-        $inis = explode(PATH_SEPARATOR,$ff->dataObjectsOriginalIni);
+        $linksCacheTmp = preg_replace('/\.ini/', '.links.ini', $iniCacheTmp );
         $links = array();
-        foreach($inis as $ini) {
-            $ini = preg_replace('/\.ini$/', '.links.ini', $ini);
+        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).'/*.ini');
+                $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;
             }
-            $links = array_merge_recursive( parse_ini_file($ini, true), $links);   
+            $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();
@@ -229,13 +257,37 @@ class HTML_FlexyFramework_Generator extends DB_DataObject_Generator
         if (count($out)) {
             $ff->debug("Writing merged Links file : $iniLinksCache \n");
 
-            file_put_contents($iniLinksCache. '.tmp', implode("\n", $out));
-            if (file_exists($iniLinksCache)) {
+           
+            file_put_contents($iniCacheTmp. '.links.ini', implode("\n", $out));
+            if (file_exists($iniLinksCache)) {                
                 unlink($iniLinksCache);
             }
-            rename($iniLinksCache. '.tmp', $iniLinksCache);
+            rename($iniCacheTmp. '.links.ini', $iniLinksCache);
         }
+        
+        flock($fp, LOCK_UN);
+        fclose($fp);
+        unlink($iniCache.".lock");
+        
+    }
+    /* 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;
+        
+        
     }
-    
     
 }