コード例 #1
0
ファイル: wsc.php プロジェクト: amoryver/ws-converter
function write_worksheet($in, $file, $options = array())
{
    $ext = pathinfo($file, PATHINFO_EXTENSION);
    if (isset($options["write"])) {
        if ($ext != "" && $options["write"] != $ext) {
            fputs(STDERR, "Warning: Ignored the extension to adopt {$options["write"]}.\n");
        }
        $ext = $options["write"];
    }
    switch (strtolower($ext)) {
        case "raw":
            $out = fopen($file, "w");
            while (($line = fgets($in)) !== false) {
                fputs($out, $line);
            }
            fclose($out);
            break;
        case "csv":
            $options["delimiter"] = ",";
            write_csv($in, $file, $options);
            break;
        case "tsv":
            $options["delimiter"] = "\t";
            write_csv($in, $file, $options);
            break;
        case "xlsx":
            write_excel($in, $file, $options);
            break;
        case "json":
            write_json($in, $file, $options);
            break;
        case "yml":
        case "yaml":
            write_yaml($in, $file, $options);
            break;
        case "xml":
            write_xml($in, $file, $options);
            break;
        case "htm":
        case "html":
            // a write only converter
            write_html($in, $file, $options);
            break;
        case "tex":
            // a write only converter
            write_tex($in, $file, $options);
            break;
        case "sql":
            // a write only converter
            write_sql($in, $file, $options);
            break;
        default:
            fputs(STDERR, "Error[{$ext}]: No writer is available.\n");
            break;
    }
    return;
}
コード例 #2
0
ファイル: app_writer.php プロジェクト: amoryver/ws-converter
function write_app($in, $out_file, $options = array())
{
    if (!isset($options["offset"])) {
        $options["offset"] = "0";
    }
    $offset = $options["offset"] + 0;
    $force = $options["force"];
    // データベーススキーマを収集する。
    $schema = array();
    while (($line = fgets($in)) !== false) {
        // "#" はワークシートの先頭行を示す。
        if (substr($line, 0, 1) != "#") {
            continue;
        }
        $worksheet_name = trim(substr($line, 1));
        $row = 0;
        while (($record = fgetcsv($in, 65536, ",", '"')) !== FALSE) {
            // 空行はワークシートの最終行を示す。
            if ($record == array('')) {
                break;
            }
            $tag = array_shift($record);
            $record = array_slice($record, $offset);
            $num_cols = count($record);
            switch ($tag) {
                case "fields":
                    $fields = $record;
                    $field_indexes = array_flip($fields);
                    break;
                case "types":
                    $types = $record;
                    break;
                case "params":
                    $params = $record;
                    break;
                case "titles":
                    $titles = $record;
                    break;
                case "record":
                    for ($col = 0; $col < count($record); $col++) {
                        $property = $fields[$col];
                        $property_jp = $titles[$col];
                        $value = $record[$col];
                        $table = $record[$field_indexes["table_db"]];
                        $field = $record[$field_indexes["field_db"]];
                        if ($table != "" && $field != "") {
                            $schema[$table][$field][$property] = $value;
                        }
                    }
                    break;
                default:
                    $contents = implode(",", $record);
                    fputs(STDERR, "Error[{$tag}]: Unkown tag. {$contents}\n");
                    return;
            }
            $row += 1;
        }
    }
    // データ入力用のワークシートを生成する。
    $tmp = tmpfile();
    foreach ($schema as $worksheet_name => $worksheet) {
        fputs($tmp, "# {$worksheet_name}\n");
        $fields = array();
        $types = array();
        $params = array();
        $titles = array();
        foreach ($worksheet as $field_name => $properties) {
            $fields[] = $field_name;
            $types[] = $properties["type_db"];
            $params[] = $properties["constraint_db"];
            $titles[] = $properties["field"];
        }
        $num_cols = count($fields);
        $record = array_fill(0, $num_cols, "");
        array_unshift($fields, "fields");
        fputcsv($tmp, $fields, ",", '"');
        array_unshift($types, "types");
        fputcsv($tmp, $types, ",", '"');
        array_unshift($params, "params");
        fputcsv($tmp, $params, ",", '"');
        array_unshift($titles, "titles");
        fputcsv($tmp, $titles, ",", '"');
        array_unshift($record, "record");
        fputcsv($tmp, $record, ",", '"');
        fputs($tmp, "\n");
    }
    fflush($tmp);
    rewind($tmp);
    $options["outheader"] = "F:C:P:T";
    write_excel($tmp, $out_file . ".xlsx", $options);
    // テンプレートのあるディレクトリと出力ディレクトリを決める。
    $template_dirs = array();
    $template_dirs[] = "./ctp_templates/";
    $template_dirs[] = dirname(__FILE__) . "/ctp_templates/";
    $instance_dirs = array();
    $instance_dirs[] = "./ctp_output/";
    $template_dir = "";
    foreach ($template_dirs as $dir) {
        if (file_exists($dir)) {
            $template_dir = $dir;
            break;
        }
    }
    if ($template_dir == "") {
        fputs(STDERR, "Error: Template directory does not exist.\n");
        return;
    }
    $instance_dir = "";
    foreach ($instance_dirs as $dir) {
        if (file_exists($dir)) {
            $instance_dir = $dir;
            break;
        }
    }
    if ($instance_dir == "") {
        if (mkdir("./ctp_output/", 0700)) {
            $instance_dir = "./ctp_output/";
        } else {
            fputs(STDERR, "Error: Output directory does not exist.\n");
            return;
        }
    }
    // PHPのコードを生成する。
    foreach ($schema as $table => $detail) {
        $in_dir = $template_dir;
        $out_dir = "{$instance_dir}/" . Inflector::camelize($table);
        if (!file_exists($out_dir)) {
            mkdir($out_dir);
        }
        $in_ext = "ctp";
        $out_ext = "ctp";
        $templates = preg_grep("/^\\w+\\.{$in_ext}\$/", scandir($template_dir));
        foreach ($templates as $template) {
            $filename = pathinfo($template, PATHINFO_FILENAME);
            $in_file = "{$in_dir}/{$filename}.{$in_ext}";
            $out_file = "{$out_dir}/{$filename}.{$out_ext}";
            $ans = render_ctp(file_get_contents($in_file), $schema, $table);
            if (file_exists($out_file) && !$force) {
                fputs(STDERR, "Error[{$out_file}]: The file already exists. Use -f option to overwrite it.\n");
                return;
            }
            file_put_contents($out_file, $ans);
        }
    }
    return;
}