5 * This works by creating '_generate** methods, which are called by start()
9 require_once 'PDO/DataObject/Generator.php';
12 class HTML_FlexyFramework2_Generator extends PDO_DataObject_Generator
14 // block class generation.
15 static $generateClasses = false;
17 function generateClasses()
19 // echo "GENERATE CLASSES?";
20 if (!HTML_FlexyFramework2_Generator::$generateClasses ) {
24 //echo "GENERATE CLASSES?";
25 parent::generateClasses();
29 function generateReaders()
31 $options = PDO_DataObject::config();
34 foreach($this->tables as $table) {
36 $out = array_merge($out, $this->_generateReader($table));
41 file_put_contents($options["schema_location"] . '.reader', serialize($out));
45 function generatePhp ()
51 * Generate the cached readers used for meta data in the queries.
54 function _generateReader($table)
57 $dbtype = $DB->getAttribute(PDO::ATTR_DRIVER_NAME);;
60 foreach($table->columns as $t) {
61 switch (strtoupper($t->type)) {
64 case 'INT2': // postgres
65 case 'INT4': // postgres
66 case 'INT8': // postgres
67 case 'SERIAL4': // postgres
68 case 'SERIAL8': // postgres
77 $ret[$table->table.'.'.$t->name] = array('type' => 'int');
82 case 'DOUBLE PRECISION': // double precision (firebird)
84 case 'FLOAT4': // real (postgres)
85 case 'FLOAT8': // double precision (postgres)
87 case 'MONEY': // mssql and maybe others
89 case 'NUMBER': // oci8
90 $ret[$table->table.'.'.$t->name] = array('type' => 'float'); //???
96 $ret[$table->table.'.'.$t->name] = array('type' => 'boolean'); //???
97 // postgres needs to quote '0'
98 if ($dbtype == 'pgsql') {
99 ///$type += DB_DATAOBJECT_STR;
110 case 'SET': // not really but oh well
112 case 'POINT': // mysql geometry stuff - not really string - but will do..
114 case 'TIMESTAMPTZ': // postgres
115 case 'BPCHAR': // postgres
116 case 'INTERVAL': // postgres (eg. '12 days')
118 case 'CIDR': // postgres IP net spec
119 case 'INET': // postgres IP
120 case 'MACADDR': // postgress network Mac address.
122 case 'INTEGER[]': // postgres type
123 case 'BOOLEAN[]': // postgres type
129 case 'BLOB': /// these should really be ignored!!!???
134 case 'CLOB': // oracle character lob support
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;
145 $ret[$table->table.'.'.$t->name] = array('type' => 'date', 'dateFormat' => 'Y-m-d'); //???
149 $ret[$table->table.'.'.$t->name] = $t->name; // technically not...
154 $ret[$table->table.'.'.$t->name] = array('type' => 'date', 'dateFormat' => 'Y-m-d H:i:s'); //???
157 case 'TIMESTAMP': // do other databases use this???
159 $ret[$table->table.'.'.$t->name] = ($dbtype == 'mysql') ?
160 array('type' => 'float') :
161 array('type' => 'date', 'dateFormat' => 'Y-m-d H:i:s');
169 $ret[$table->table.'.'.$t->name] = $t->name;
179 * Generate the cached *.ini and links.ini files (merged for all components)
182 static function writeCache($iniCacheTmp, $iniCache, $replace = array())
185 $fp = fopen($iniCache.".lock", "a+");
188 $ff = HTML_FlexyFramework2::get();
189 $ff->debug('Framework Generator:writeCache ' . $iniCacheTmp . ' ' . $iniCache);
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)) {
199 if (file_exists($iniCache)) {
202 $ff->debug("Writing merged ini file : $iniCache\n");
203 rename($iniCacheTmp, $iniCache);
208 // readers..??? not needed??? (historical)
209 if (file_exists($iniCacheTmp.'.reader') && filesize($iniCacheTmp.'.reader')) {
212 if (file_exists($iniCache.'.reader') ) {
213 if ($replace[$iniCache] != md5_file($iniCacheTmp.'.reader')) {
214 unlink($iniCache.'.reader');
215 rename($iniCacheTmp.'.reader', $iniCache.'.reader');
217 // do not need to touch..
218 unlink($iniCacheTmp.'.reader');
221 rename($iniCacheTmp.'.reader', $iniCache.'.reader');
227 // merge and set links.. test for generated links file..
229 $linksCacheTmp = preg_replace('/\.ini/', '.links.ini', $iniCacheTmp );
231 if (file_exists($linksCacheTmp )) {
232 $links = self::mergeIni( parse_ini_file($linksCacheTmp, true), $links);
233 unlink($linksCacheTmp);
235 // we are going to use the DataObject directories..
237 $inis = explode(PATH_SEPARATOR,$ff->PDO_DataObject['class_location']);
238 //print_r($inis);exit;
239 $ff->debug("class_location = ". $ff->PDO_DataObject['class_location']);
242 $lproject = strtolower(explode('/', $ff->project)[0]);
244 foreach($inis as $path) {
245 $ini = $path . '/'. strtolower( $lproject ) . '.links.ini';
247 if (!file_exists($ini)) {
248 $ff->debug("Framework Generator:writeCache PROJECT.links.ini does not exist in $path - trying glob");
250 // try scanning the directory for another ini file..
251 $ar = glob(dirname($ini).'/*.links.ini');
260 $ff->debug("Framework Generator:writeCache using {$ar[0]}");
262 // first file.. = with links removed..
263 $ini = preg_replace('/\.links\./' , '.', $ar[0]);
264 $ini = preg_replace('/\.ini$/', '.links.ini', $ini);
267 // why do this twice???
268 if (!file_exists($ini)) {
271 $ff->debug("Adding in $ini");
273 $links = self::mergeIni( parse_ini_file($ini, true), $links);
275 $iniLinksCache = preg_replace('/\.ini$/', '.links.ini', $iniCache);
277 foreach($links as $tbl=>$ar) {
278 $out[] = '['. $tbl .']';
279 foreach ($ar as $k=>$v) {
280 $out[] = $k . '=' .$v;
285 $ff->debug("Writing merged Links file : $iniLinksCache \n");
286 $out_str = implode("\n", $out);
287 // is target file different?
288 if (!isset($replace[$iniLinksCache]) || $replace[$iniLinksCache] != md5($out)) {
290 file_put_contents($iniCacheTmp. '.links.ini', $out_str);
291 if (file_exists($iniLinksCache)) {
292 unlink($iniLinksCache);
294 rename($iniCacheTmp. '.links.ini', $iniLinksCache);
296 touch($iniLinksCache);
299 } // we ignore that we might need to delete old links.ini
306 if(file_exists($iniCache.".lock")){
307 unlink($iniCache.".lock");
311 /* bit like merge recursive, but it avoids doing stuff with arrays.. */
312 static function mergeIni($new, $old)
314 foreach($new as $g => $ar) {
315 if (!isset($old[$g])) {
319 foreach($ar as $k=>$v) {
320 if (isset($old[$g][$k])) {