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