Skip to content

Instantly share code, notes, and snippets.

@Toyz
Last active December 27, 2015 19:32
Show Gist options
  • Save Toyz/547816676bb77a9f9a7b to your computer and use it in GitHub Desktop.
Save Toyz/547816676bb77a9f9a7b to your computer and use it in GitHub Desktop.
<?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