Guest User

Untitled

a guest
Jan 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $jsFile = $_GET["jsFile"];
  2. $jsBaseUrl = "js/";
  3.  
  4. $imports = array($jsFile)
  5.  
  6. class FailedToFindJSException extends Exception { };
  7.  
  8. function importJsFile($package)
  9. {
  10.     global $jsBaseUrl, $imports;
  11.     $newJS = "";
  12.    
  13.     // package is in the form of stuff.stuff.stuff
  14.     $fileURL = $jsBaseUrl.str_replace(".", "/", $package).".js";
  15.    
  16.     // Open the file and begin reading
  17.     $h = fopen($fileURL, "r");
  18.     if(!$h) {
  19.         throw new FailedToFindJSException("Failed to find ".$package");
  20.     }
  21.    
  22.     while(($line = fgets($h)) !== false)
  23.     {
  24.         if(strstr($line, "@import") !== false
  25.         && !in_array(($pac = substr($line, strlen("@import "))), $imports)) {
  26.             $imports[] = $pac;
  27.             try {
  28.                 $newJS .= importJsFile($pac);
  29.             } catch (FailedToFindJSException $e) {
  30.                 echo $e->getMessage();
  31.                 // Zend_Registry::get('logger')->log(Level::WARNING, $e->getMessage());
  32.             }
  33.         } else {
  34.             $newJS .= $line;
  35.         }
  36.     }
  37.    
  38.     // If there was a problem reading the file, we will not be at the end
  39.     if(!feof($h))
  40.     {
  41.         throw new Exception("Problem reading JS file: ".$package);
  42.     }
  43.    
  44.     fclose($h);
  45.    
  46.     return newJS;
  47. }
Add Comment
Please, Sign In to add comment