This is one of my first oop code in normal php without any framework. This a code that combines all css files into one and all js files into one and it works. Any suggestion on what can i make better in terms of oop or anything else
This is how it woeks .
i run http://localhost/folderName/css/styleindex.css
i have ht acess that passes everything to index.php(code below0.
here i extract type as css and page as styleindex by exploding styleindex.css
get the array of files that needs to be combined for this page and join the
disaplay this on browser with correct mime type and cache header
define('DEBUGMODE',01); class ResourceHandler { /** * @var $FilesToParse files that needs to be parsed */ private $FilesToParse = array(); /** * @var $type weather js or css */ private $type; /** * @var $page name/category of the page */ private $page; /** * @var $AllowedTypes array of allowed types js or css */ private $AllowedTypes = array('css','js'); /** * @var $CacheTime time for which cache is valid */ private $CacheTime = 610000; function __construct($type, $page) { $this->setType($type); $this->setPage($page); header('ETag: Ei07072012745'); //header('Content-type: text/'.$this->type." charset: UTF-8"); if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) { //ob_start("ob_gzhandler"); } else { ob_start(); } header ("Vary: negotiate,accept-language,accept-charset"); header ("Cache-control: public"); header('Content-Disposition: attachment; filename="'.$this->page.'.'.$this->type.'"'); if(DEBUGMODE ==0) { $expire = "expires: " . gmdate("D, d M Y H:i:s", time() + $this->CacheTime) . " GMT"; header ($expire); } } function setPage($PageName) { $Resource = explode('.',$PageName); $this->page = $Resource[0]; } function setType($type) { $type = strtolower($type); /*type needs to be js or css else dont proceed further*/ if(!in_array($type,$this->AllowedTypes)) { exit; } $this->type = $type; } /** * Function to minify data * @return $FileContent Minified file data */ private function minifyFiles($data) { /* remove comments */ $data = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $data); /* remove tabs, spaces, newlines, etc. */ $data = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $data); return $data; } /** * Function to parse file and merge into one variable * @return $FileContent Merged file data */ private function parseFiles() { $FileContent = '' ; foreach($this->FilesToParse as $File) { $FileContent = file_get_contents($File); } return $FileContent; } function __destruct() { ob_end_flush(); } /** * Function to display css file * @return $output returns the combined, compresssed and gzipped data */ private function css() { switch($this->page) { case 'index' : $this->FilesToParse = array('style12.css','jquery.autocomplete.css'); break; DEFAULT : header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); exit; } $data = $this->parseFiles('css'); if(DEBUGMODE ==0) { $data = $this->minifyFiles($data); } return $data; } /** * Function to display js file * @return $output returns the combined and gzipped data */ private function js() { switch($this->page) { case 'index' : $this->FilesToParse = array('jquery-1.6.2.js','jquery.autocomplete.js','facebox.js'); break; default : header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); exit; } $data = $this->parseFiles('js'); return $data; } function load() { $type = $this->type; $cwd = getcwd(); chdir($type); echo $this->$type(); chdir($cwd); } } $request = explode("/resources/", $_SERVER['REQUEST_URI']); $params = array_filter(explode("/", $request[1])); if(isset($params[0]) && isset($params[1])) { $resource = new ResourceHandler($params[0],$params[1]); $resource->load(); } else { header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); } ?>