From fad93506cd56b976d1413ed5adef87701df715bd Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Sun, 18 May 2014 00:25:49 +0200 Subject: One file per class and new namespace. We should be able to use a PSR4 autoloader (like composer's) now. ste.php is still there as a fallback for old applications. --- FilesystemStorageAccess.php | 66 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 FilesystemStorageAccess.php (limited to 'FilesystemStorageAccess.php') diff --git a/FilesystemStorageAccess.php b/FilesystemStorageAccess.php new file mode 100644 index 0000000..61ad452 --- /dev/null +++ b/FilesystemStorageAccess.php @@ -0,0 +1,66 @@ + implementation for loading / saving templates into a directory structure. + */ +class FilesystemStorageAccess implements StorageAccess { + protected $sourcedir; + protected $transcompileddir; + + /* + * Constructor: __construct + * + * Parameters: + * $src - The directory with the sources (Writing permissions are not mandatory, because STE does not save template sources). + * $transc - The directory with the transcompiled templates (the PHP instance / the HTTP Server needs writing permissions to this directory). + */ + public function __construct($src, $transc) { + $this->sourcedir = $src; + $this->transcompileddir = $transc; + } + + public function load($tpl, &$mode) { + $src_fn = $this->sourcedir . "/" . $tpl; + $transc_fn = $this->transcompileddir . "/" . $tpl . ".php"; + + if($mode == StorageAccess::MODE_SOURCE) { + $content = @file_get_contents($src_fn); + if($content === false) { + throw new CantLoadTemplate("Template not found."); + } + return $content; + } + + $src_stat = @stat($src_fn); + $transc_stat = @stat($transc_fn); + + if(($src_stat === false) and ($transc_stat === false)) { + throw new CantLoadTemplate("Template not found."); + } else if($transc_stat === false) { + $mode = StorageAccess::MODE_SOURCE; + return file_get_contents($src_fn); + } else if($src_stat === false) { + include($transc_fn); + return $transcompile_fx; + } else { + if($src_stat["mtime"] > $transc_stat["mtime"]) { + $mode = StorageAccess::MODE_SOURCE; + return file_get_contents($src_fn); + } else { + include($transc_fn); + return $transcompile_fx; + } + } + } + + public function save($tpl, $data, $mode) { + $fn = (($mode == StorageAccess::MODE_SOURCE) ? $this->sourcedir : $this->transcompileddir) . "/" . $tpl . (($mode == StorageAccess::MODE_TRANSCOMPILED) ? ".php" : ""); + @mkdir(dirname($fn), 0777, true); + if(file_put_contents($fn, "") === false) { + throw new CantSaveTemplate("Unable to save template."); + } + } +} -- cgit v1.2.3-54-g00ecf