Exemplo n.º 1
0
/**
 * Gets an array of files with given glob patterns
 *
 * @param $patterns
 * @param array $excludes
 *
 * @return array
 */
function get_files($patterns, $excludes = array())
{
    $files = array();
    foreach ($patterns as $pattern) {
        foreach (glob(BASE . $pattern) as $uri) {
            $file = str_replace(BASE, '', $uri);
            if (is_file($uri) && !is_excluded_file($file, $excludes)) {
                $files[] = $file;
            }
        }
    }
    return array_unique($files);
}
Exemplo n.º 2
0
function combine($type, $files, $excludes)
{
    if (!in_array($type, array('css', 'js'))) {
        return $files;
    }
    //extract local fylesystem path
    clean_file_paths($files, $excludes);
    global $registry;
    $oc_root = dirname(DIR_APPLICATION);
    $cache = NULL;
    $cachefile = NULL;
    $filename = NULL;
    if (!defined('DS')) {
        define('DS', DIRECTORY_SEPARATOR);
    }
    $cachefile = $oc_root . DS . 'assets' . DS . $type . DS . getSSLCachePrefix() . $type . '-combined.cache';
    if (!file_exists($cachefile)) {
        touch($cachefile);
        file_put_contents($cachefile, json_encode(array()));
    }
    $cache = json_decode(file_get_contents($cachefile), true);
    $comboHash = '';
    $excludedFiles = array();
    $includedFiles = 0;
    foreach ($files as $hash => $file) {
        if (!is_excluded_file($file, $excludes)) {
            $comboHash .= $hash;
            $includedFiles++;
        } else {
            $excludedFiles[$hash] = $file;
        }
    }
    $comboHash = md5($comboHash);
    $target = '/assets/' . $type . '/' . getSSLCachePrefix() . 'nitro-combined-' . $comboHash . '.' . $type;
    $targetAbsolutePath = $oc_root . DS . trim(str_replace('/', DS, $target), DS);
    $recache = false;
    foreach ($files as $hash => $file) {
        if (!is_excluded_file($file, $excludes)) {
            $filename = $oc_root . DS . trim(str_replace('/', DS, $file), DS);
            if (!empty($cache[$comboHash][$filename])) {
                if (!is_url($file) && $cache[$comboHash][$filename] != filemtime($filename)) {
                    $recache = true;
                    break;
                }
            } else {
                $recache = true;
                break;
            }
        } else {
            continue;
        }
    }
    $combinedContent = '';
    if ($recache || !file_exists($targetAbsolutePath)) {
        if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == '1')) {
            $webshopUrl = $registry->get('config')->get('config_ssl');
        } else {
            $webshopUrl = $registry->get('config')->get('config_url');
        }
        $webshopUrl = rtrim(preg_replace('~^https?\\:~i', '', $webshopUrl), '/');
        $counter = 0;
        foreach ($files as $hash => $file) {
            if (!is_excluded_file($file, $excludes)) {
                $filename = $oc_root . DS . trim(str_replace('/', DS, $file), DS);
                $content = is_url($file) ? fetchRemoteContent($file) : file_get_contents($filename);
                $urlToCurrentDir = is_url($file) ? dirname($file) : $webshopUrl . str_replace($oc_root, '', dirname('/' . trim($filename, '/')));
                if (!empty($content)) {
                    if ($type == 'js' && substr($content, -1) == ')') {
                        $content .= ';';
                    }
                    if ($type == 'css') {
                        $content = preg_replace('/(url\\()(?![\'\\"]?(?:(?:https?\\:\\/\\/)|(?:data\\:)|(?:\\/)))([\'\\"]?)\\/?(?!\\/)/', '$1$2' . $urlToCurrentDir . '/', $content);
                    }
                    $combinedContent .= ($counter > 0 ? PHP_EOL : '') . $content;
                    unset($content);
                    $counter++;
                }
                $cache[$comboHash][$filename] = is_url($file) ? 0 : filemtime($filename);
            }
        }
        if ($type == 'css') {
            /* pull imports to the top and include their content if possible */
            $imports = array();
            preg_match_all('/\\@import[^\\;]*\\;/', $combinedContent, $imports);
            if (!empty($imports)) {
                $imports = array_reverse($imports[0]);
                foreach ($imports as $import) {
                    $importUrl = preg_replace('/[^\'"\\(]*(?:[\'"\\(\\s]?)*(.*?)(?:[\'"\\)]).*/', '$1', $import);
                    if (getNitroPersistence('Mini.CSSFetchImport')) {
                        $tmpImportContent = fetchRemoteContent($importUrl);
                    }
                    if (!empty($tmpImportContent)) {
                        $combinedContent = $tmpImportContent . str_replace($import, '', $combinedContent);
                    } else {
                        $combinedContent = $import . str_replace($import, '', $combinedContent);
                    }
                }
            }
        }
        file_put_contents($targetAbsolutePath, $combinedContent);
    }
    file_put_contents($cachefile, json_encode($cache));
    if ($includedFiles > 0) {
        return array_merge($excludedFiles, array(md5($target) => trim($target, '/')));
    } else {
        return $excludedFiles;
    }
}