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