Last active
December 27, 2015 19:32
-
-
Save Toyz/547816676bb77a9f9a7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Controllers; | |
class StaticFileHandler { | |
private $basePath; | |
private $loadedFiles; | |
function __construct($basePath) | |
{ | |
$this->basePath = $basePath; | |
$this->loadedFiles = []; | |
} | |
public function GetFileContents($file){ | |
$path = $this->basePath.$file; | |
if(array_key_exists(md5($path), $this->loadedFiles)){ | |
return $this->loadedFiles[md5($path)]; | |
} | |
if(!$this->FileExist($file)){ | |
throw new \Exception("Error $file does not exist", 1); | |
} | |
$contents = file_get_contents($path); | |
$this->loadedFiles[md5($path)] = $contents; | |
return $contents; | |
} | |
public function GetFilePath($file){ | |
if(!$this->FileExist($file)){ | |
throw new \Exception("Error $file does not exist", 1); | |
} | |
return $this->basePath.$file; | |
} | |
private function FileExist($file){ | |
return file_exists($this->basePath.$file); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment