예제 #1
0
function minify($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);
    }
    if (!file_exists($oc_root . DS . 'assets')) {
        mkdir($oc_root . DS . 'assets');
    }
    if (!file_exists($oc_root . DS . 'assets' . DS . $type)) {
        mkdir($oc_root . DS . 'assets' . DS . $type);
    }
    $cachefile = $oc_root . DS . 'assets' . DS . $type . DS . getSSLCachePrefix() . $type . '.cache';
    if (!file_exists($cachefile)) {
        touch($cachefile);
        file_put_contents($cachefile, json_encode(array()));
    }
    $cache = json_decode(file_get_contents($cachefile), true);
    switch ($type) {
        case 'js':
            include_once NITRO_LIB_FOLDER . 'minifier' . DS . 'JSShrink.php';
            break;
        case 'css':
            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), '/');
            include_once NITRO_LIB_FOLDER . 'minifier' . DS . 'CSSMin.php';
            break;
    }
    foreach ($files as $hash => $file) {
        $recache = false;
        if (!is_excluded_file($file, $excludes)) {
            $filename = $oc_root . DS . trim(str_replace('/', DS, $file), DS);
            $basefilename = basename($file, '.' . $type);
            $target = '/assets/' . $type . '/' . getSSLCachePrefix() . 'nitro-mini-' . encode_filename($filename) . '.' . $type;
            $targetAbsolutePath = $oc_root . DS . trim(str_replace('/', DS, $target), DS);
            if (!empty($cache[$filename])) {
                if (!is_url($file) && $cache[$filename] != filemtime($filename)) {
                    $recache = true;
                }
            } else {
                $recache = true;
            }
            if ($recache || !file_exists($targetAbsolutePath)) {
                touch($targetAbsolutePath);
                $content = is_url($file) ? fetchRemoteContent($file) : file_get_contents($filename);
                switch ($type) {
                    case 'js':
                        $content = Minifier::minify($content, array('flaggedComments' => false));
                        break;
                    case 'css':
                        $urlToCurrentDir = is_url($file) ? dirname($file) : $webshopUrl . dirname('/' . trim($file, '/'));
                        $content = preg_replace('/(url\\()(?![\'\\"]?(?:(?:https?\\:\\/\\/)|(?:data\\:)|(?:\\/)))([\'\\"]?)\\/?(?!\\/)/', '$1$2' . $urlToCurrentDir . '/', $content);
                        $content = Nitro_Minify_CSS_Compressor::process($content);
                        break;
                }
                file_put_contents($targetAbsolutePath, $content);
                $cache[$filename] = is_url($file) ? time() : filemtime($filename);
            }
            $files[$hash] = trim($target, '/');
        }
    }
    file_put_contents($cachefile, json_encode($cache));
    return $files;
}
예제 #2
0
 /**
  * Minify a CSS string
  * 
  * @param string $css
  * 
  * @param array $options (currently ignored)
  * 
  * @return string
  */
 public static function process($css, $options = array())
 {
     $obj = new Nitro_Minify_CSS_Compressor($options);
     return $obj->_process($css);
 }