| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Interpreted from code |
| 5 |
* By Hackinet |
| 6 |
*/ |
| 7 |
class JsCombiner{ |
| 8 |
|
| 9 |
private $fileNames = []; |
| 10 |
|
| 11 |
function __construct($fileNames){ |
| 12 |
$this->fileNames = $fileNames; |
| 13 |
} |
| 14 |
|
| 15 |
private function fileValidator($fileName){ |
| 16 |
|
| 17 |
$fileParts = explode('.',$fileName); |
| 18 |
$fileExtension = end($fileParts); |
| 19 |
|
| 20 |
if(strtolower($fileExtension) !== "js"){ |
| 21 |
throw new Exception("Invalid file type. The extension for the file $fileName is $fileExtension."); |
| 22 |
} |
| 23 |
|
| 24 |
if(!file_exists($fileName)){ |
| 25 |
throw new Exception("The given file $fileName does not exists."); |
| 26 |
} |
| 27 |
|
| 28 |
} |
| 29 |
|
| 30 |
private function setHeaders(){ |
| 31 |
header('Content-Type: text/javascript'); |
| 32 |
} |
| 33 |
|
| 34 |
public function combine(){ |
| 35 |
|
| 36 |
$this->setHeaders(); |
| 37 |
|
| 38 |
$js = ""; |
| 39 |
$fileNames = $this->fileNames; |
| 40 |
|
| 41 |
foreach ($fileNames as $fileName){ |
| 42 |
try{ |
| 43 |
$this->fileValidator($fileName); |
| 44 |
$fileContent = file_get_contents($fileName); |
| 45 |
$js .= $fileContent; |
| 46 |
} catch(\Exception $e) { |
| 47 |
echo 'Message: ' .$e->getMessage(); |
| 48 |
return false; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return $js; |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
} |