示例#1
0
文件: eco_app.php 项目: r3-gis/EcoGIS
/**
 * Build the js if necessary
 *
 */
function R3BuildJS($force = false)
{
    global $auth, $jsPacker;
    $appCode = strtolower(APPLICATION_CODE);
    $jsDestPath = R3_UPLOAD_DATA_DIR . strtolower(R3EcoGisHelper::getCurrentDomainName()) . "/js/";
    $masterFileName = "{$jsDestPath}{$appCode}_all.js";
    if (!$force) {
        $rebuild = !file_exists($masterFileName);
        if (!$rebuild) {
            $compiledAge = filemtime($masterFileName);
            $files = $jsPacker['JS_files'];
            if (isset($jsPacker['files'])) {
                foreach ($jsPacker['files'] as $name => $fileGroup) {
                    $name = str_replace('<LANG>', R3Locale::getLanguageCode(), $name);
                    if (!file_exists("{$jsDestPath}{$name}")) {
                        $rebuild = true;
                        ezcLog::getInstance()->log("JavaScript file \"{$jsDestPath}{$name}\" not found. Rebuild necessary", ezcLog::DEBUG);
                        break;
                    }
                    foreach ($fileGroup as $file) {
                        $files[] = $file;
                    }
                }
            }
            foreach ($files as $file) {
                $file = str_replace('<LANG>', R3Locale::getLanguageCode(), $file);
                if (filemtime(R3_WEB_JS_DIR . $file) > $compiledAge) {
                    // Complie needed
                    $rebuild = true;
                    break;
                }
            }
        }
    }
    if ($force || $rebuild) {
        checkPath(dirname($masterFileName), true, true);
        // Non packed files
        if (isset($jsPacker['files'])) {
            foreach ($jsPacker['files'] as $name => $fileGroup) {
                $script = '';
                foreach ($fileGroup as $file) {
                    $file = str_replace('<LANG>', R3Locale::getLanguageCode(), $file);
                    $script .= "/*** " . basename($file) . " ***/\n\n\n" . file_get_contents(R3_WEB_JS_DIR . $file) . "\n\n\n";
                }
                $name = str_replace('<LANG>', R3Locale::getLanguageCode(), dirname($masterFileName) . '/' . $name);
                ezcLog::getInstance()->log("JavaScript rebuild for \"{$name}\"", ezcLog::DEBUG);
                file_put_contents($name, $script);
            }
        }
        require_once R3_LIB_DIR . 'class.JavaScriptPacker.php';
        $script = '';
        $funcList = array();
        foreach ($jsPacker['JS_files'] as $file) {
            $data = file_get_contents(R3_WEB_JS_DIR . $file);
            $script .= "{$data}\n\n\n";
            // Check function averride!
            foreach (explode("\n", $data) as $lineNo => $line) {
                if (substr($line, 0, 9) == 'function ' && ($p = strpos($line, '(')) !== false) {
                    $name = trim(substr($line, 9, $p - 9));
                    if (isset($funcList[$name])) {
                        echo "<b>Warning</b>: JavaScript function \"{$name}\" in file {$file}:{$lineNo} already declared in file {$funcList[$name]['file']}:{$funcList[$name]['line']}.<br />\n";
                    }
                    $funcList[$name] = array('file' => $file, 'line' => $lineNo);
                }
            }
        }
        if (!isset($jsPacker['minify_output'])) {
            ezcLog::getInstance()->log("JavaScript rebuild for \"{$masterFileName}\"", ezcLog::DEBUG);
            file_put_contents($masterFileName, $script);
        } else {
            $packer = new JavaScriptPacker($script, $jsPacker['minify_output'] ? 'Normal' : 'None', true, false);
            $packed = $packer->pack();
            ezcLog::getInstance()->log("JavaScript rebuild for \"{$masterFileName}\"", ezcLog::DEBUG);
            file_put_contents($masterFileName, $packed);
        }
    }
}