4 require_once 'DB/DataObject/Generator.php';
7 class HTML_FlexyFramework_Generator extends DB_DataObject_Generator
9 // block class generation.
10 static $generateClasses = false;
12 function generateClasses()
14 // echo "GENERATE CLASSES?";
15 if (!HTML_FlexyFramework_Generator::$generateClasses ) {
19 //echo "GENERATE CLASSES?";
20 parent::generateClasses();
24 function generateReaders()
26 $options = &PEAR::getStaticProperty('DB_DataObject','options');
29 foreach($this->tables as $this->table) {
30 $this->table = trim($this->table);
32 $out = array_merge($out, $this->_generateReader($this->table));
36 //echo '<PRE>';print_r($out);exit;
38 file_put_contents($options["ini_{$this->_database}"] . '.reader', serialize($out));
42 * Generate the cached readers used for meta data in the queries.
45 function _generateReader($table)
47 $DB = $this->getDatabaseConnection();
48 $dbtype = $DB->phptype;
49 $def = $this->_definitions[$table] ;
52 switch (strtoupper($t->type)) {
55 case 'INT2': // postgres
56 case 'INT4': // postgres
57 case 'INT8': // postgres
58 case 'SERIAL4': // postgres
59 case 'SERIAL8': // postgres
68 $ret[$table.'.'.$t->name] = array('type' => 'int');
73 case 'DOUBLE PRECISION': // double precision (firebird)
75 case 'FLOAT4': // real (postgres)
76 case 'FLOAT8': // double precision (postgres)
78 case 'MONEY': // mssql and maybe others
80 case 'NUMBER': // oci8
81 $ret[$table.'.'.$t->name] = array('type' => 'float'); //???
87 $ret[$table.'.'.$t->name] = array('type' => 'boolean'); //???
88 // postgres needs to quote '0'
89 if ($dbtype == 'pgsql') {
90 ///$type += DB_DATAOBJECT_STR;
101 case 'SET': // not really but oh well
103 case 'POINT': // mysql geometry stuff - not really string - but will do..
105 case 'TIMESTAMPTZ': // postgres
106 case 'BPCHAR': // postgres
107 case 'INTERVAL': // postgres (eg. '12 days')
109 case 'CIDR': // postgres IP net spec
110 case 'INET': // postgres IP
111 case 'MACADDR': // postgress network Mac address.
113 case 'INTEGER[]': // postgres type
114 case 'BOOLEAN[]': // postgres type
120 case 'BLOB': /// these should really be ignored!!!???
125 case 'CLOB': // oracle character lob support
127 case 'BYTEA': // postgres blob support..
128 $ret[$table.'.'.$t->name] = $t->name; // strings are not sent as arrays..
129 // $type = DB_DATAOBJECT_STR;
136 $ret[$table.'.'.$t->name] = array('type' => 'date', 'dateFormat' => 'Y-m-d'); //???
140 $ret[$table.'.'.$t->name] = $t->name; // technically not...
145 $ret[$table.'.'.$t->name] = array('type' => 'date', 'dateFormat' => 'Y-m-d H:i:s'); //???
148 case 'TIMESTAMP': // do other databases use this???
150 $ret[$table.'.'.$t->name] = ($dbtype == 'mysql') ?
151 array('type' => 'float') :
152 array('type' => 'date', 'dateFormat' => 'Y-m-d H:i:s');
160 $ret[$table.'.'.$t->name] = $t->name;
170 * Generate the cached *.ini and links.ini files (merged for all components)
171 * @param string $iniCacheTmp a temporary file
172 * @param string $iniCache the real target.
174 static function writeCache($iniCacheTmp, $iniCache, $replace = array())
177 $fp = fopen($iniCache.".lock", "a+");
180 $ff = HTML_FlexyFramework::get();
181 $ff->debug('Framework Generator:writeCache ' . $iniCacheTmp . ' ' . $iniCache);
183 //var_dump($iniCacheTmp);
184 // echo '<PRE>';echo file_get_contents($iniCacheTmp);exit;
185 // only unpdate if nothing went wrong.
187 if (file_exists($iniCacheTmp) && filesize($iniCacheTmp)) {
188 // is the replace file exist?
190 if (isset($replace[$iniCache]) && $replace[$iniCache] != md5_file($iniCacheTmp)) {
192 // it seems to happen that in this case if there are less elements in the file than before, then we might have an error
193 // it's better to just die here, than continue.
194 $old = parse_ini_file($iniCache,true);
195 $new = parse_ini_file($iniCacheTmp,true);
196 if (count(array_keys($new)) < count(array_keys($old))) {
197 die("Generated INI file failed, try reloading");
201 if (!isset($replace[$iniCache]) || $replace[$iniCache] != md5_file($iniCacheTmp)) {
204 if (file_exists($iniCache)) {
207 $ff->debug("Writing merged ini file : $iniCache\n");
208 rename($iniCacheTmp, $iniCache);
211 unlink($iniCacheTmp);
215 // readers..??? not needed??? (historical)
216 if (file_exists($iniCacheTmp.'.reader') && filesize($iniCacheTmp.'.reader')) {
219 if (file_exists($iniCache.'.reader') ) {
220 if (!isset($replace[$iniCache] ) || $replace[$iniCache] != md5_file($iniCacheTmp.'.reader')) {
221 unlink($iniCache.'.reader');
222 rename($iniCacheTmp.'.reader', $iniCache.'.reader');
224 // do not need to touch..
225 unlink($iniCacheTmp.'.reader');
228 rename($iniCacheTmp.'.reader', $iniCache.'.reader');
234 // merge and set links.. test for generated links file..
236 $linksCacheTmp = preg_replace('/\.ini/', '.links.ini', $iniCacheTmp );
238 if (file_exists($linksCacheTmp )) {
239 $links = self::mergeIni( parse_ini_file($linksCacheTmp, true), $links);
240 unlink($linksCacheTmp);
242 // we are going to use the DataObject directories..
244 $inis = explode(PATH_SEPARATOR,$ff->DB_DataObject['class_location']);
245 //print_r($inis);exit;
246 $ff->debug("class_location = ". $ff->DB_DataObject['class_location']);
249 $lproject = strtolower(explode('/', $ff->project)[0]);
251 foreach($inis as $path) {
252 $ini = $path . '/'. strtolower( $lproject ) . '.links.ini';
254 if (!file_exists($ini)) {
255 $ff->debug("Framework Generator:writeCache PROJECT.links.ini does not exist in $path - trying glob");
257 // try scanning the directory for another ini file..
258 $ar = glob(dirname($ini).'/*.links.ini');
267 $ff->debug("Framework Generator:writeCache using {$ar[0]}");
269 // first file.. = with links removed..
270 $ini = preg_replace('/\.links\./' , '.', $ar[0]);
271 $ini = preg_replace('/\.ini$/', '.links.ini', $ini);
274 // why do this twice???
275 if (!file_exists($ini)) {
278 $ff->debug("Adding in $ini");
280 $links = self::mergeIni( parse_ini_file($ini, true), $links);
282 $iniLinksCache = preg_replace('/\.ini$/', '.links.ini', $iniCache);
284 foreach($links as $tbl=>$ar) {
285 $out[] = '['. $tbl .']';
286 foreach ($ar as $k=>$v) {
287 $out[] = $k . '=' .$v;
292 $ff->debug("Writing merged Links file : $iniLinksCache \n");
293 $out_str = implode("\n", $out);
294 // is target file different?
295 if (!isset($replace[$iniLinksCache]) || $replace[$iniLinksCache] != md5($out_str)) {
297 file_put_contents($iniCacheTmp. '.links.ini', $out_str);
298 if (file_exists($iniLinksCache)) {
299 unlink($iniLinksCache);
301 rename($iniCacheTmp. '.links.ini', $iniLinksCache);
303 touch($iniLinksCache);
310 // this sometimes seems to fail..
311 @unlink($iniCache.".lock");
314 /* bit like merge recursive, but it avoids doing stuff with arrays.. */
315 static function mergeIni($new, $old)
317 foreach($new as $g => $ar) {
318 if (!isset($old[$g])) {
322 foreach($ar as $k=>$v) {
323 if (isset($old[$g][$k])) {