isFile()) { if (strpos($file->getBasename(), ".") !== 0) { unlink($file->getRealPath()); } } elseif ($file->isDir()) { rmdir($file->getRealPath()); } } } } /** * Recursive remove directory * * @param string $path */ public static function rm($path) { self::clean($path); if (is_dir($path)) { rmdir($path); } } /** * @param string $template_dir directory of templates * @throws \LogicException if directory doesn't exists */ public function __construct($template_dir) { if ($_dir = realpath($template_dir)) { $this->_path = $_dir; } else { throw new \LogicException("Template directory {$template_dir} doesn't exists"); } } /** * Get source and mtime of template by name * @param string $tpl * @param int $time load last modified time * @return string */ public function getSource($tpl, &$time) { $tpl = $this->_getTemplatePath($tpl); clearstatcache(null, $tpl); $time = filemtime($tpl); return file_get_contents($tpl); } /** * Get last modified of template by name * @param string $tpl * @return int */ public function getLastModified($tpl) { clearstatcache(null, $tpl = $this->_getTemplatePath($tpl)); return filemtime($tpl); } /** * Get all names of templates from provider. * * @param string $extension all templates must have this extension, default .tpl * @return array|\Iterator */ public function getList($extension = "tpl") { $list = array(); $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($this->_path, \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST ); $path_len = strlen($this->_path); foreach ($iterator as $file) { /* @var \SplFileInfo $file */ if ($file->isFile() && $file->getExtension() == $extension) { $list[] = substr($file->getPathname(), $path_len + 1); } } return $list; } /** * Get template path * @param $tpl * @return string * @throws \RuntimeException */ protected function _getTemplatePath($tpl) { if (($path = realpath($this->_path . "/" . $tpl)) && strpos($path, $this->_path) === 0) { return $path; } else { throw new \RuntimeException("Template $tpl not found"); } } /** * @param string $tpl * @return bool */ public function templateExists($tpl) { return file_exists($this->_path . "/" . $tpl); } /** * Verify templates (check change time) * * @param array $templates [template_name => modified, ...] By conversation, you may trust the template's name * @return bool */ public function verify(array $templates) { foreach ($templates as $template => $mtime) { clearstatcache(null, $template = $this->_path . '/' . $template); if (@filemtime($template) !== $mtime) { return false; } } return true; } }