Skip to content

Instantly share code, notes, and snippets.

@jakubboucek
Last active February 23, 2016 07:39
Show Gist options
  • Save jakubboucek/05fb4f2b6e26cc35d972 to your computer and use it in GitHub Desktop.
Save jakubboucek/05fb4f2b6e26cc35d972 to your computer and use it in GitHub Desktop.
Generátor statických do cache
<?php
class fileCacheGenerator {
private $sourceDir;
private $cacheDir;
private $cacheUrl;
public function __construct( $sourceDir, $cacheDir, $cacheUrl ) {
$this->sourceDir = $sourceDir;
$this->cacheDir = $cacheDir;
$this->cacheUrl = $cacheUrl;
}
public function getFileUrl( $file ) {
if( ! file_exists( $this->sourceDir . $file ) ) {
throw new fileCacheGeneratorException("Source file $file doensn't exist.", 1);
}
$hash = getFileHash( $this->sourceDir . $file );
$extension = "." . pathinfo($path, PATHINFO_EXTENSION);
$cachedFile = $file . $extension;
if( ! file_exists( $this->cacheDir . '/' . $cachedFile ) ) {
$content = $this->generate( $this->sourceDir . $file );
if( ! file_put_contents( $this->cacheDir . '/' . $cachedFile, $content ) ) {
throw new fileCacheGeneratorException("Unable to write file $cachedFile to $this->cacheDir, is directory exists?", 1);
}
}
return $this->cacheUrl . '/' . $cachedFile;
}
private function getFileHash( $file ) {
$lastModified = filemtime( $file );
return md5( $file . $lastModified );
}
private function generate( $sourceFile ) {
//tady se napište nějaký kód, který bude do cache generovat a vracet obsah souboru
}
}
class fileCacheGeneratorException extends Exception {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment