/**
  * Event onAfterRoute
  *
  * @param null
  *
  * @return null
  */
 public function onAfterRoute()
 {
     $app = JFactory::getApplication();
     $jinput = $app->input;
     // Don't do anything for non scriptmerge pages
     if ($jinput->getCmd('option') != 'com_scriptmerge') {
         return false;
     }
     // Check for frontend
     if ($app->isSite() == false) {
         return false;
     }
     // Require the helper
     require_once JPATH_SITE . '/components/com_scriptmerge/helpers/helper.php';
     $helper = new ScriptMergeHelper();
     // Send the content-type header
     $type = $jinput->getString('type');
     if ($type == 'css') {
         header('Content-Type: text/css');
     } else {
         header('Content-Type: application/javascript');
     }
     // Read the files parameter
     $files = $jinput->getString('files');
     $buffer = null;
     if (!empty($files)) {
         $files = $helper->decodeList($files);
         foreach ($files as $file) {
             if ($type == 'css') {
                 if (!preg_match('/\\.css$/', $file)) {
                     continue;
                 }
                 $buffer .= $helper->getCssContent($file);
             } else {
                 if (!preg_match('/\\.js$/', $file)) {
                     continue;
                 }
                 $buffer .= $helper->getJsContent($file);
             }
         }
     }
     // Clean up CSS-code
     if ($type == 'css') {
         $buffer = ScriptMergeHelper::cleanCssContent($buffer);
         // Clean up JS-code
     } else {
         $buffer = ScriptMergeHelper::cleanJsContent($buffer);
     }
     // Construct the expiration time
     $expires = (int) ($helper->getParams()->get('expiration', 30) * 60);
     // Set the expiry in the future
     if ($expires > 0) {
         header('Cache-Control: public, max-age=' . $expires);
         header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires));
         // Set the expiry in the past
     } else {
         header("Cache-Control: no-cache, no-store, must-revalidate");
         header('Expires: ' . gmdate('D, d M Y H:i:s', time() - 60 * 60 * 24));
     }
     header('Vary: Accept-Encoding');
     header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()));
     header('ETag: ' . md5($buffer));
     if (function_exists('gzencode') && ScriptMergeHelper::getParams()->get('force_gzip', 0) == 1) {
         header('Content-Encoding: gzip');
         print gzencode($buffer);
     } else {
         print $buffer;
     }
     // Close the application
     $application = JFactory::getApplication();
     $application->close();
 }
            $buffer .= $helper->getCssContent($file);
        } else {
            if (!preg_match('/\\.js$/', $file)) {
                continue;
            }
            $buffer .= $helper->getJsContent($file);
        }
    }
}
// Clean up CSS-code
if ($type == 'css') {
    $buffer = ScriptMergeHelper::cleanCssContent($buffer);
    // Clean up JS-code
} else {
    $buffer = ScriptMergeHelper::cleanJsContent($buffer);
}
// Handle GZIP support
$compression = false;
if (function_exists('gzencode') && ScriptMergeHelper::getParams()->get('force_gzip', 0) == 1) {
    $gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
    $deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');
    $compression = $gzip ? 'gzip' : ($deflate ? 'deflate' : false);
    $buffer = gzencode($buffer, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
}
// Send HTTP-headers
ScriptMergeHelper::sendHttpHeaders($buffer, $helper->getParams(), $compression);
// Print the buffer
print $buffer;
// Close the application
$application = JFactory::getApplication();
$application->close();