fenom/src/Fenom/Provider.php

157 lines
4.4 KiB
PHP
Raw Normal View History

2013-02-21 22:51:24 +04:00
<?php
2013-04-28 11:33:36 +04:00
/*
2013-06-28 11:53:53 +04:00
* This file is part of Fenom.
2013-04-28 11:33:36 +04:00
*
* (c) 2013 Ivan Shalganov
*
2013-04-28 18:08:57 +04:00
* For the full copyright and license information, please view the license.md
2013-04-28 11:33:36 +04:00
* file that was distributed with this source code.
*/
2013-06-28 11:53:53 +04:00
namespace Fenom;
2013-02-21 22:51:24 +04:00
2013-06-28 11:53:53 +04:00
use Fenom\ProviderInterface;
2013-02-21 22:51:24 +04:00
/**
2013-07-04 01:28:10 +04:00
* Base template provider
2013-02-21 22:51:24 +04:00
* @author Ivan Shalganov
*/
2013-06-28 11:53:53 +04:00
class Provider implements ProviderInterface {
2013-02-21 22:51:24 +04:00
private $_path;
/**
* Clean directory from files
*
* @param string $path
*/
public static function clean($path) {
if(is_file($path)) {
unlink($path);
} elseif(is_dir($path)) {
2013-07-03 12:10:50 +04:00
$iterator = iterator_to_array(
new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path,
\FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
)
);
2013-02-21 22:51:24 +04:00
foreach($iterator as $file) {
/* @var \splFileInfo $file*/
if($file->isFile()) {
2013-02-22 00:05:20 +04:00
if(strpos($file->getBasename(), ".") !== 0) {
2013-02-21 22:51:24 +04:00
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);
}
}
2013-04-28 11:33:36 +04:00
/**
* @param string $template_dir directory of templates
* @throws \LogicException if directory doesn't exists
*/
2013-02-21 22:51:24 +04:00
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
2013-02-21 22:51:24 +04:00
* @param string $tpl
* @param int $time load last modified time
2013-02-21 22:51:24 +04:00
* @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
*/
2013-02-21 22:51:24 +04:00
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;
2013-02-21 22:51:24 +04:00
}
/**
* 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
*/
2013-07-04 01:28:10 +04:00
public function templateExists($tpl) {
2013-02-21 22:51:24 +04:00
return file_exists($this->_path."/".$tpl);
}
2013-02-21 22:51:24 +04:00
/**
2013-04-28 11:33:36 +04:00
* Verify templates (check change time)
2013-02-21 22:51:24 +04:00
*
2013-04-28 11:33:36 +04:00
* @param array $templates [template_name => modified, ...] By conversation, you may trust the template's name
2013-02-21 22:51:24 +04:00
* @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;
}
}