<?php
/**
 * 命令行表模型批量生成工具
 * @copyright reginx.com
 * $Id: tab.php 5 2013-01-14 13:22:55Z reginx $
 */
class tab_tool {

    // contruct
    public function tab_tool(){
        $conf = include('../../config/default.php');
        $conf = isset($conf['db']['mysql']) ? $conf['db']['mysql'] : array();
        if(empty($conf)){
            die('Only supports mysql !');
        }
        include('../extra/db/mysql.db.php');
        $db = new mysql_db($conf , 0 );
        $query = $db->query('show tables');
        $pre = $conf[0]['pre'];
        $tabs = array();
        while($row = $db->fetch_array($query , MYSQL_NUM)){
            if(strpos($row[0] , $pre) !== false){
                $tabs[] = str_replace($pre , '' , $row[0]);
            }
        }
        $db->close();
        $code = '<?php' . PHP_EOL;
        $code.= '/**' . PHP_EOL;
        $code.= ' * 表模型 %s ' . PHP_EOL;
        $code.= ' * @copyright reginx.com' . PHP_EOL;
        $code.= ' * @Created By Reginx tab tool' . PHP_EOL;
        $code.= ' */' . PHP_EOL;
        $code.= 'class %s_tab extends tab {}' . PHP_EOL . '?>';
        foreach($tabs as $v){
            $f = str_replace('_' , '/' , $v);
            $file = '../../include/tab/' . $f.'.tab.php';
            echo($file . "\n");
            if(!file_exists($file)){
                if(!is_dir(dirname($file))){
                    mkdir(dirname($file) , 0755);
                }
                file_put_contents($file , sprintf($code , $v , $v));
            }
        }

    }

}

// run in cli
if(php_sapi_name() != 'cli'){
    die('Only run from command line');
}
new tab_tool();
?>