/** * Send the client side script code to the client * * Does not cache the client side script code if the HTTP header Cache-Control or Pragma is set to no-cache. * * @param bool $minified Whether to compress the client side script code */ public static function send($minified = false) { header('Content-Type: application/javascript'); $basedir = Icinga::app()->getBootstrapDirectory(); $js = $out = ''; $min = $minified ? '.min' : ''; // Prepare vendor file list $vendorFiles = array(); foreach (self::$vendorFiles as $file) { $vendorFiles[] = $basedir . '/' . $file . $min . '.js'; } // Prepare Icinga JS file list $jsFiles = array(); foreach (self::$jsFiles as $file) { $jsFiles[] = $basedir . '/' . $file; } foreach (Icinga::app()->getModuleManager()->getLoadedModules() as $name => $module) { if ($module->hasJs()) { foreach ($module->getJsFiles() as $path) { if (file_exists($path)) { $jsFiles[] = $path; } } } } $files = array_merge($vendorFiles, $jsFiles); $request = Icinga::app()->getRequest(); $noCache = $request->getHeader('Cache-Control') === 'no-cache' || $request->getHeader('Pragma') === 'no-cache'; if (!$noCache && FileCache::etagMatchesFiles($files)) { header("HTTP/1.1 304 Not Modified"); return; } else { $etag = FileCache::etagForFiles($files); } header('Cache-Control: public'); header('ETag: "' . $etag . '"'); header('Content-Type: application/javascript'); $cacheFile = 'icinga-' . $etag . $min . '.js'; $cache = FileCache::instance(); if (!$noCache && $cache->has($cacheFile)) { $cache->send($cacheFile); return; } // We do not minify vendor files foreach ($vendorFiles as $file) { $out .= ';' . ltrim(trim(file_get_contents($file)), ';') . "\n"; } foreach ($jsFiles as $file) { $js .= file_get_contents($file) . "\n\n\n"; } if ($minified) { require_once 'JShrink/Minifier.php'; $out .= Minifier::minify($js, array('flaggedComments' => false)); } else { $out .= $js; } $cache->store($cacheFile, $out); echo $out; }
public static function send($minified = false) { self::checkPhp(); $app = Icinga::app(); $basedir = $app->getBootstrapDirectory(); foreach (self::$lessFiles as $file) { $lessFiles[] = $basedir . '/' . $file; } $files = $lessFiles; foreach ($app->getModuleManager()->getLoadedModules() as $name => $module) { if ($module->hasCss()) { foreach ($module->getCssFiles() as $path) { if (file_exists($path)) { $files[] = $path; } } } } if ($etag = FileCache::etagMatchesFiles($files)) { header("HTTP/1.1 304 Not Modified"); return; } else { $etag = FileCache::etagForFiles($files); } header('Cache-Control: public'); header('ETag: "' . $etag . '"'); header('Content-Type: text/css'); $min = $minified ? '.min' : ''; $cacheFile = 'icinga-' . $etag . $min . '.css'; $cache = FileCache::instance(); if ($cache->has($cacheFile)) { $cache->send($cacheFile); return; } $less = new LessCompiler(); $less->disableExtendedImport(); foreach ($lessFiles as $file) { $less->addFile($file); } $less->addLoadedModules(); if ($minified) { $less->compress(); } $out = self::fixModuleLayoutCss($less->compile()); $cache->store($cacheFile, $out); echo $out; }
/** * Send the stylesheet to the client * * Does not cache the stylesheet if the HTTP header Cache-Control or Pragma is set to no-cache. * * @param bool $minified Whether to compress the stylesheet */ public static function send($minified = false) { $styleSheet = new self(); $request = $styleSheet->app->getRequest(); $response = $styleSheet->app->getResponse(); $noCache = $request->getHeader('Cache-Control') === 'no-cache' || $request->getHeader('Pragma') === 'no-cache'; if (!$noCache && FileCache::etagMatchesFiles($styleSheet->lessCompiler->getLessFiles())) { $response->setHttpResponseCode(304)->sendHeaders(); return; } $etag = FileCache::etagForFiles($styleSheet->lessCompiler->getLessFiles()); $response->setHeader('Cache-Control', 'public', true)->setHeader('ETag', $etag, true)->setHeader('Content-Type', 'text/css', true); $cacheFile = 'icinga-' . $etag . ($minified ? '.min' : '') . '.css'; $cache = FileCache::instance(); if (!$noCache && $cache->has($cacheFile)) { $response->setBody($cache->get($cacheFile)); } else { $css = $styleSheet->render($minified); $response->setBody($css); $cache->store($cacheFile, $css); } $response->sendResponse(); }