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)
173 static function writeCache($iniCacheTmp, $iniCache)
176 $fp = fopen($iniCache.".lock", "a+");
179 $ff = HTML_FlexyFramework::get();
180 $ff->debug('Framework Generator:writeCache ' . $iniCacheTmp . ' ' . $iniCache);
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)) {
189 $ff->debug("Writing merged ini file : $iniCache\n");
190 rename($iniCacheTmp, $iniCache);
193 // readers..??? not needed??? (historical)
194 if (file_exists($iniCacheTmp.'.reader') && filesize($iniCacheTmp.'.reader')) {
195 if (file_exists($iniCache.'.reader')) {
196 unlink($iniCache.'.reader');
198 $ff->debug("Writing merged reader file : $iniCache.reader\n");
199 rename($iniCacheTmp.'.reader', $iniCache.'.reader');
203 // merge and set links.. test for generated links file..
205 $linksCacheTmp = preg_replace('/\.ini/', '.links.ini', $iniCacheTmp );
207 if (file_exists($linksCacheTmp )) {
208 $links = self::mergeIni( parse_ini_file($linksCacheTmp, true), $links);
209 unlink($linksCacheTmp);
211 // we are going to use the DataObject directories..
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']);
218 $project = strtolower(explode('/', $ff->project)[0]);
220 foreach($inis as $path) {
221 $ini = $path . '/'. strtolower( $ff->project) . '.links.ini';
223 if (!file_exists($ini)) {
224 $ff->debug("Framework Generator:writeCache PROJECT.links.ini does not exist in $path - trying glob");
226 // try scanning the directory for another ini file..
227 $ar = glob(dirname($ini).'/*.links.ini');
236 $ff->debug("Framework Generator:writeCache using {$ar[0]}");
238 // first file.. = with links removed..
239 $ini = preg_replace('/\.links\./' , '.', $ar[0]);
240 $ini = preg_replace('/\.ini$/', '.links.ini', $ini);
243 // why do this twice???
244 if (!file_exists($ini)) {
247 $ff->debug("Adding in $ini");
249 $links = self::mergeIni( parse_ini_file($ini, true), $links);
251 $iniLinksCache = preg_replace('/\.ini$/', '.links.ini', $iniCache);
253 foreach($links as $tbl=>$ar) {
254 $out[] = '['. $tbl .']';
255 foreach ($ar as $k=>$v) {
256 $out[] = $k . '=' .$v;
261 $ff->debug("Writing merged Links file : $iniLinksCache \n");
264 file_put_contents($iniCacheTmp. '.links.ini', implode("\n", $out));
265 if (file_exists($iniLinksCache)) {
266 unlink($iniLinksCache);
268 rename($iniCacheTmp. '.links.ini', $iniLinksCache);
273 unlink($iniCache.".lock");
276 /* bit like merge recursive, but it avoids doing stuff with arrays.. */
277 static function mergeIni($new, $old)
279 foreach($new as $g => $ar) {
280 if (!isset($old[$g])) {
284 foreach($ar as $k=>$v) {
285 if (isset($old[$g][$k])) {