bda57c2598c04c1165a9d236841daf6c96bbd05c
[pear] / HTML / FlexyFramework / Generator.php
1 <?php
2
3
4 require_once 'DB/DataObject/Generator.php';
5
6
7 class HTML_FlexyFramework_Generator extends DB_DataObject_Generator 
8 {
9     // block class generation.
10     static $generateClasses = false;
11     
12     function generateClasses()
13     {
14 //        echo "GENERATE CLASSES?";
15         if (!HTML_FlexyFramework_Generator::$generateClasses ) {
16             return;
17         }
18        
19         //echo "GENERATE CLASSES?";
20         parent::generateClasses();
21     }
22     
23     
24     function generateReaders()
25     {
26         $options = &PEAR::getStaticProperty('DB_DataObject','options');
27         
28         $out = array();
29         foreach($this->tables as $this->table) {
30             $this->table        = trim($this->table);
31             
32             $out = array_merge($out, $this->_generateReader($this->table));
33             
34             
35         }
36         //echo '<PRE>';print_r($out);exit;
37          
38         file_put_contents($options["ini_{$this->_database}"] . '.reader', serialize($out));
39          
40     }
41     /**
42      * Generate the cached readers used for meta data in the queries.
43      * 
44      */
45     function _generateReader($table)
46     {
47         $DB = $this->getDatabaseConnection();
48         $dbtype = $DB->phptype;
49         $def = $this->_definitions[$table] ;
50         $ret = array();
51         foreach($def as $t) {
52              switch (strtoupper($t->type)) {
53
54                 case 'INT':
55                 case 'INT2':    // postgres
56                 case 'INT4':    // postgres
57                 case 'INT8':    // postgres
58                 case 'SERIAL4': // postgres
59                 case 'SERIAL8': // postgres
60                 case 'INTEGER':
61                 case 'TINYINT':
62                 case 'SMALLINT':
63                 case 'MEDIUMINT':
64                 case 'BIGINT':
65                 // wierd ones..
66                 case 'YEAR':
67                 
68                     $ret[$table.'.'.$t->name] = array('type' => 'int');
69                     continue 2;
70                
71                 case 'REAL':
72                 case 'DOUBLE':
73                 case 'DOUBLE PRECISION': // double precision (firebird)
74                 case 'FLOAT':
75                 case 'FLOAT4': // real (postgres)
76                 case 'FLOAT8': // double precision (postgres)
77                 case 'DECIMAL':
78                 case 'MONEY':  // mssql and maybe others
79                 case 'NUMERIC':
80                 case 'NUMBER': // oci8 
81                     $ret[$table.'.'.$t->name] = array('type' => 'float'); //???
82                     break;
83                     
84                 case 'BIT':
85                 case 'BOOL':   
86                 case 'BOOLEAN':   
87                     $ret[$table.'.'.$t->name] = array('type' => 'boolean'); //???
88                     // postgres needs to quote '0'
89                     if ($dbtype == 'pgsql') {
90                         ///$type +=  DB_DATAOBJECT_STR;
91                     }
92                     break;
93                     
94                 case 'STRING':
95                 case 'CHAR':
96                 case 'VARCHAR':
97                 case 'VARCHAR2':
98                 case 'TINYTEXT':
99                 
100                 case 'ENUM':
101                 case 'SET':         // not really but oh well
102                 
103                 case 'POINT':       // mysql geometry stuff - not really string - but will do..
104                 
105                 case 'TIMESTAMPTZ': // postgres
106                 case 'BPCHAR':      // postgres
107                 case 'INTERVAL':    // postgres (eg. '12 days')
108                 
109                 case 'CIDR':        // postgres IP net spec
110                 case 'INET':        // postgres IP
111                 case 'MACADDR':     // postgress network Mac address.
112                 
113                 case 'INTEGER[]':   // postgres type
114                 case 'BOOLEAN[]':   // postgres type
115                 
116                 
117                 case 'TEXT':
118                 case 'MEDIUMTEXT':
119                 case 'LONGTEXT':
120                 case 'BLOB':       /// these should really be ignored!!!???
121                 case 'TINYBLOB':
122                 case 'MEDIUMBLOB':
123                 case 'LONGBLOB':
124                 
125                 case 'CLOB': // oracle character lob support
126                 
127                 case 'BYTEA':   // postgres blob support..
128                     $ret[$table.'.'.$t->name] =  $t->name; // strings are not sent as arrays..
129                    // $type = DB_DATAOBJECT_STR;
130                     break;
131                 
132                 
133                 
134                 
135                 case 'DATE':    
136                     $ret[$table.'.'.$t->name] = array('type' => 'date', 'dateFormat' => 'Y-m-d'); //???
137                     break;
138                     
139                 case 'TIME':    
140                     $ret[$table.'.'.$t->name] = $t->name; // technically not...
141                     break;    
142                     
143                 
144                 case 'DATETIME': 
145                     $ret[$table.'.'.$t->name] = array('type' => 'date', 'dateFormat' => 'Y-m-d H:i:s'); //???
146                     break;    
147                     
148                 case 'TIMESTAMP': // do other databases use this???
149                     
150                     $ret[$table.'.'.$t->name] =   ($dbtype == 'mysql') ?
151                          array('type' => 'float') : 
152                         array('type' => 'date', 'dateFormat' => 'Y-m-d H:i:s');
153                     break;    
154                     
155                 
156                 
157                     
158                     
159                 default:     
160                     $ret[$table.'.'.$t->name] = $t->name;
161                     break;
162             }
163         }
164         
165         return $ret;
166         
167         
168     }
169     /**
170      * Generate the cached *.ini and links.ini files (merged for all components)
171      * 
172      */
173     static function writeCache($iniCacheTmp, $iniCache)
174     {
175         
176         $fp = fopen($iniCache.".lock", "a+");
177         flock($fp,LOCK_EX);
178
179         $ff = HTML_FlexyFramework::get();
180         $ff->debug('Framework Generator:writeCache ' . $iniCacheTmp .  ' ' . $iniCache);
181           
182         //var_dump($iniCacheTmp);
183        // echo '<PRE>';echo file_get_contents($iniCacheTmp);exit;
184         // only unpdate if nothing went wrong.
185         if (file_exists($iniCacheTmp) && filesize($iniCacheTmp)) {
186             if (file_exists($iniCache)) {
187                 unlink($iniCache);
188             }
189             $ff->debug("Writing merged ini file : $iniCache\n");
190             rename($iniCacheTmp, $iniCache);
191         }
192         
193         // readers..??? not needed??? (historical)
194         if (file_exists($iniCacheTmp.'.reader') &&  filesize($iniCacheTmp.'.reader')) {
195             if (file_exists($iniCache.'.reader')) {
196                 unlink($iniCache.'.reader');
197             }
198             $ff->debug("Writing merged reader file : $iniCache.reader\n");
199             rename($iniCacheTmp.'.reader', $iniCache.'.reader');
200         }
201         
202         
203         // merge and set links.. test for generated links file..
204         
205         $linksCacheTmp = preg_replace('/\.ini/', '.links.ini', $iniCacheTmp );
206         $links = array();
207         if (file_exists($linksCacheTmp )) {
208             $links = self::mergeIni( parse_ini_file($linksCacheTmp, true), $links);
209             unlink($linksCacheTmp);
210         }
211         // we are going to use the DataObject directories..
212         
213         $inis = explode(PATH_SEPARATOR,$ff->DB_DataObject['class_location']);
214         //print_r($inis);exit;
215         $ff->debug("class_location = ". $ff->DB_DataObject['class_location']);
216         
217         
218         $lproject = strtolower(explode('/', $ff->project)[0]);
219         
220         foreach($inis as $path) {
221             $ini = $path . '/'. strtolower( $lproject ) . '.links.ini';
222              //var_dump($ini);
223             if (!file_exists($ini)) {
224                 $ff->debug("Framework Generator:writeCache PROJECT.links.ini does not exist in $path - trying glob");
225        
226                 // try scanning the directory for another ini file..
227                 $ar = glob(dirname($ini).'/*.links.ini');
228                 
229                 
230                 if (empty($ar)) {
231                     continue;
232                 }
233                 
234                 
235                 sort($ar);
236                 $ff->debug("Framework Generator:writeCache using {$ar[0]}");
237                 
238                 // first file.. = with links removed..
239                 $ini = preg_replace('/\.links\./' , '.', $ar[0]);
240                 $ini = preg_replace('/\.ini$/', '.links.ini', $ini);
241             }
242             
243             // why do this twice???
244             if (!file_exists($ini)) {
245                 continue;
246             }
247             $ff->debug("Adding in $ini");
248             // prefer first ?
249             $links = self::mergeIni( parse_ini_file($ini, true), $links);   
250         }
251         $iniLinksCache = preg_replace('/\.ini$/', '.links.ini', $iniCache);
252         $out = array();
253         foreach($links as $tbl=>$ar) {
254             $out[] = '['. $tbl  .']';
255             foreach ($ar as $k=>$v) {
256                 $out[] = $k . '=' .$v;
257             }
258             $out[] = '';
259         }
260         if (count($out)) {
261             $ff->debug("Writing merged Links file : $iniLinksCache \n");
262
263            
264             file_put_contents($iniCacheTmp. '.links.ini', implode("\n", $out));
265             if (file_exists($iniLinksCache)) {                
266                 unlink($iniLinksCache);
267             }
268             rename($iniCacheTmp. '.links.ini', $iniLinksCache);
269         }
270         
271         flock($fp, LOCK_UN);
272         fclose($fp);
273         // this sometimes seems to fail..
274         @unlink($iniCache.".lock");
275         
276     }
277     /* bit like merge recursive, but it avoids doing stuff with arrays.. */
278     static function mergeIni($new, $old) 
279     {
280         foreach($new as $g => $ar) {
281             if (!isset($old[$g])) {
282                 $old[$g] = $ar;
283                 continue;
284             }
285             foreach($ar as $k=>$v) {
286                 if (isset($old[$g][$k])) {
287                     continue;
288                 }
289                 $old[$g][$k] = $v;
290             }
291         }
292         return $old;
293         
294         
295     }
296     
297 }