Beispiel #1
0
/**
 * Dumps a CSS/JS file
 *
 * @param $name
 * @param $mimetype
 */
function dump($name, $mimetype, $cachekey)
{
    global $cachedirdrupal;
    global $nocache;
    global $config;
    $starttime = microtime(true);
    $excludes = array();
    $dependencies = array();
    $patterns = array();
    $filetype = substr(strrchr($name, '.'), 1);
    $output = '';
    $minify = isset($_REQUEST['min']);
    $debugjavascript = $filetype === 'js' && isset($_REQUEST['debug']);
    if ($debugjavascript) {
        $output .= '// load js files in a synchronous way' . PHP_EOL;
    }
    // check whether the file is in drupal cache
    if (!is_dir($cachedirdrupal)) {
        mkdir($cachedirdrupal, 0755);
    }
    $cachefile = $cachedirdrupal . '/terrific-' . $cachekey . '-' . $name;
    if ($nocache || !is_file($cachefile)) {
        // collect excluded pattern & (less/scss) dependencies & patterns
        foreach ($config->assets->{$name} as $pattern) {
            $firstchar = substr($pattern, 0, 1);
            if ($firstchar === '!') {
                $excludes[] = substr($pattern, 1);
            } else {
                if ($firstchar === '+') {
                    $dependencies[] = substr($pattern, 1);
                } else {
                    $patterns[] = $pattern;
                }
            }
        }
        $dependencies = get_files($dependencies);
        $excludes = array_merge($dependencies, $excludes);
        $files = get_files($patterns, $excludes);
        foreach ($files as $entry) {
            if (!$debugjavascript) {
                $format = substr(strrchr($entry, '.'), 1);
                $output .= compile(BASE . $entry, $format, $dependencies);
            } else {
                $output .= "document.write('<script type=\"text/javascript\" src=\"{$entry}\"><\\/script>');" . PHP_EOL;
            }
        }
        if ($minify) {
            switch ($filetype) {
                case 'css':
                    require BASE . 'app/library/cssmin/cssmin.php';
                    $output = CssMin::minify($output);
                    break;
                case 'js':
                    require BASE . 'app/library/jshrink/Minifier.php';
                    $output = \JShrink\Minifier::minify($output);
                    break;
            }
        }
        $time_taken = microtime(TRUE) - $starttime;
        $output = get_asset_banner($name, $filetype, $minify, $time_taken) . $output;
        file_put_contents($cachefile, $output);
    } else {
        $output = file_get_contents($cachefile);
    }
    ob_start("ob_gzhandler");
    header('Content-Type: ' . $mimetype);
    echo $output;
    ob_end_flush();
}
Beispiel #2
0
 /**
  * Dumps a CSS/JS file
  *
  * @param $name
  * @param $mimetype
  */
 function dump($name, $mimetype)
 {
     global $config;
     $starttime = microtime(true);
     $excludes = array();
     $dependencies = array();
     $patterns = array();
     $filetype = substr(strrchr($name, '.'), 1);
     $output = '';
     $minify = isset($_REQUEST['min']);
     $debugjavascript = $filetype === 'js' && isset($_REQUEST['debug']);
     if ($debugjavascript) {
         $output .= '// load js files in a synchronous way' . PHP_EOL;
     }
     // collect excluded pattern & (less/scss) dependencies & patterns
     foreach ($config->assets->{$name} as $pattern) {
         $firstchar = substr($pattern, 0, 1);
         if ($firstchar === '!') {
             $excludes[] = substr($pattern, 1);
         } else {
             if ($firstchar === '+') {
                 $dependencies[] = substr($pattern, 1);
             } else {
                 $patterns[] = $pattern;
             }
         }
     }
     $dependencies = get_files($dependencies);
     $excludes = array_merge($dependencies, $excludes);
     $files = get_files($patterns, $excludes);
     foreach ($files as $entry) {
         if (!$debugjavascript) {
             $format = substr(strrchr($entry, '.'), 1);
             $output .= compile(BASE . $entry, $format, $dependencies);
         } else {
             $output .= "document.write('<script type=\"text/javascript\" src=\"{$entry}\"><\\/script>');" . PHP_EOL;
         }
     }
     if ($minify) {
         switch ($filetype) {
             case 'css':
                 require BASE . 'app/library/cssmin/cssmin.php';
                 $output = CssMin::minify($output);
                 break;
             case 'js':
                 require BASE . 'app/library/jshrink/Minifier.php';
                 $output = \JShrink\Minifier::minify($output);
                 break;
         }
     }
     $time_taken = microtime(true) - $starttime;
     $output = get_asset_banner($name, $filetype, $minify, $time_taken) . $output;
     header('Content-Type: ' . $mimetype);
     echo $output;
 }