_name = $name; parent::__construct($config); } public function makeClass($intext) { // First pass on cols: find longest col name // -- note: this is stupid, but makes things pretty! -- $colpad = 0; foreach ($this->_cols as $k => $v) if (strlen($k) > $colpad) $colpad = strlen($k); $colpad += 3; $text = str_replace('{{primary}}', $this->_primary, $intext); $cols = ''; foreach ($this->_cols as $k => $v) { $cols .= "\n " . sprintf("%-{$colpad}s", "'$k'") . "=> '$v',"; } $cols = substr($cols, 0, strlen($cols) - 1) . "\n "; $text = str_replace('{{cols}}', $cols, $text); return $text; } } $opts = getopt('h:u:p:d:o:n:H'); if (isset($opts['H'])) usage(); $host = 'localhost'; $user = null; $pass = null; $dbnm = null; $pre = ''; if (isset($opts['h'])) $host = $opts['h']; if (isset($opts['p'])) $passwd = $opts['p']; if (isset($opts['u'])) { $user = $opts['u']; } else { usage('User name was not speicifed - please specify with "-u"', 1); } if (isset($opts['d'])) { $dbnm = $opts['d']; } else { usage('Database name was not speicifed - please specify with "-d"', 1); } $output = getcwd(); if (isset($opts['o'])) $output = $opts['o']; if (! is_writable($output)) usage('Output directory ' . $output . ' is not writable', 2); if (isset($opts['n'])) $pre = $opts['n']; // Connect to DB $db = Zend_Db::factory('PDO_MYSQL', array( 'host' => $host, 'username' => $user, 'password' => $pass, 'dbname' => $dbnm )); Zend_Db_Table::setDefaultAdapter($db); // Get list of tables $res = $db->query('SHOW TABLES'); $tables = array(); while ($t = $res->fetchColumn()) $tables[$t] = null; // Read file template text $fp = fopen(__FILE__, 'r'); fseek($fp, __COMPILER_HALT_OFFSET__); $template = stream_get_contents($fp); fclose($fp); $inflector = new Zend_Db_Inflector(); // Iterate over tables foreach ($tables as $t => $v) { $tbl = new Zend_Db_Table_Writer($t); $find = array( '{{table}}', '{{class}}', '{{script}}', '{{date}}', '{{dbname}}', '{{host}}' ); $replace = array( $t, $pre . $inflector->camelize($t), basename(__FILE__), date(DATE_RFC1036), $dbnm, $host ); $text = str_replace($find, $replace, $template); $text = $tbl->makeClass($text); $file = $output . DIRECTORY_SEPARATOR . $inflector->camelize($t) . '.php'; file_put_contents($file, $text); } /** * Print out usage information, along with an optional error message * and exit (with the specified status code) * * @param string $message Error message * @param integer $code Exit status code */ function usage($message = null, $code = 0) { $script = basename(__FILE__); if ($message) echo $message . "\n"; echo << Where options are: -u Database user name -d Database name -p Database password (optional, defaults to no password) -h Database Host name (optional, defaults to 'localhost') -o Output directory (optional, defaults to current directory) -n Class name prefix (for example 'Myapp_Model_', optional) Or: -H Show this help message and exit Will connect to the given database, scan it's tables and write a series of Zend_Db_Table based classes, each in a PHP file, to the output directory. EOT; exit($code); } // Script ends here! __halt_compiler();