Exemple #1
0
function convert_directory($source_dir, $target_dir, $keep_mode = false)
{
    global $conf;
    if (!($dp = opendir($source_dir))) {
        fprintf(STDERR, "Error:\t [%s] directory does not exists!%s", $source_dir, PHP_EOL);
        exit(-1);
    }
    $t_dir = array();
    $t_file = array();
    while (($entry = readdir($dp)) !== false) {
        if ($entry == "." || $entry == "..") {
            continue;
        }
        $new_keep_mode = $keep_mode;
        $source_path = "{$source_dir}/{$entry}";
        $source_stat = @lstat($source_path);
        $target_path = "{$target_dir}/{$entry}";
        $target_stat = @lstat($target_path);
        if ($source_stat === false) {
            fprintf(STDERR, "Error:\t cannot stat [%s] !%s", $source_path, PHP_EOL);
            exit(-1);
        }
        if (isset($conf->t_skip) && is_array($conf->t_skip) && in_array($source_path, $conf->t_skip)) {
            continue;
        }
        if (is_link($source_path)) {
            if ($target_stat !== false && is_link($target_path) && $source_stat['mtime'] <= $target_stat['mtime']) {
                continue;
            }
            if ($target_stat !== false) {
                if (is_dir($target_path)) {
                    directory_remove($target_path);
                } else {
                    if (unlink($target_path) === false) {
                        fprintf(STDERR, "Error:\t cannot unlink [%s] !%s", $target_path, PHP_EOL);
                        exit(-1);
                    }
                }
            }
            @symlink(readlink($source_path), $target_path);
            // Do not warn on non existing symbolinc link target!
            if (strtolower(PHP_OS) == 'linux') {
                $x = `touch '{$target_path}' --no-dereference --reference='{$source_path}' `;
            }
            continue;
        }
        if (is_dir($source_path)) {
            if ($target_stat !== false) {
                if (!is_dir($target_path)) {
                    if (unlink($target_path) === false) {
                        fprintf(STDERR, "Error:\t cannot unlink [%s] !%s", $target_path, PHP_EOL);
                        exit(-1);
                    }
                }
            }
            if (!file_exists($target_path)) {
                mkdir($target_path, 0777, true);
            }
            if (isset($conf->t_keep) && is_array($conf->t_keep) && in_array($source_path, $conf->t_keep)) {
                $new_keep_mode = true;
            }
            convert_directory($source_path, $target_path, $new_keep_mode);
            continue;
        }
        if (is_file($source_path)) {
            if ($target_stat !== false && is_dir($target_path)) {
                directory_remove($target_path);
            }
            if ($target_stat !== false && $source_stat['mtime'] <= $target_stat['mtime']) {
                continue;
            }
            // do not process if source timestamp is not greater than target
            $extension = pathinfo($source_path, PATHINFO_EXTENSION);
            $keep = $keep_mode;
            if (isset($conf->t_keep) && is_array($conf->t_keep) && in_array($source_path, $conf->t_keep)) {
                $keep = true;
            }
            if (!in_array($extension, $conf->t_convert_php_extension)) {
                $keep = true;
            }
            if ($keep) {
                file_put_contents($target_path, file_get_contents($source_path));
            } else {
                $converted_str = convert_file($source_path);
                if ($converted_str === null) {
                    if (isset($conf->abort_on_error)) {
                        fprintf(STDERR, "Aborting...%s", PHP_EOL);
                        exit;
                    }
                }
                file_put_contents($target_path, $converted_str . PHP_EOL);
            }
            if ($keep) {
                file_put_contents($target_path, file_get_contents($source_path));
            }
            touch($target_path, $source_stat['mtime']);
            continue;
        }
    }
    closedir($dp);
}
Exemple #2
0
    $prettyPrinter = new myPrettyprinter();
} else {
    $prettyPrinter = new PhpParser\PrettyPrinter\Standard();
}
$traverser->addVisitor(new MyNodeVisitor());
switch ($process_mode) {
    case 'file':
        $converted_str = convert_file($source_file);
        if ($converted_str === null) {
            exit;
        }
        if ($target_file === '') {
            echo $converted_str . PHP_EOL;
            exit;
        }
        file_put_contents($target_file, $converted_str . PHP_EOL);
        exit;
    case 'directory':
        if (isset($conf->t_skip) && is_array($conf->t_skip)) {
            foreach ($conf->t_skip as $key => $val) {
                $conf->t_skip[$key] = "{$source_directory}/{$val}";
            }
        }
        if (isset($conf->t_keep) && is_array($conf->t_keep)) {
            foreach ($conf->t_keep as $key => $val) {
                $conf->t_keep[$key] = "{$source_directory}/{$val}";
            }
        }
        convert_directory($source_directory, "{$target_directory}/yakpro-mtm/converted");
        exit;
}