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-07-29 14:58:14 +04:00
|
|
|
|
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-07-29 14:58:14 +04:00
|
|
|
class Provider implements ProviderInterface
|
|
|
|
{
|
2013-02-21 22:51:24 +04:00
|
|
|
private $_path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean directory from files
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*/
|
2013-07-29 14:58:14 +04:00
|
|
|
public static function clean($path)
|
|
|
|
{
|
|
|
|
if (is_file($path)) {
|
2013-02-21 22:51:24 +04:00
|
|
|
unlink($path);
|
2013-07-29 14:58:14 +04:00
|
|
|
} 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-07-29 14:58:14 +04:00
|
|
|
foreach ($iterator as $file) {
|
|
|
|
/* @var \splFileInfo $file */
|
|
|
|
if ($file->isFile()) {
|
|
|
|
if (strpos($file->getBasename(), ".") !== 0) {
|
2013-02-21 22:51:24 +04:00
|
|
|
unlink($file->getRealPath());
|
|
|
|
}
|
2013-07-29 14:58:14 +04:00
|
|
|
} elseif ($file->isDir()) {
|
2013-02-21 22:51:24 +04:00
|
|
|
rmdir($file->getRealPath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursive remove directory
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*/
|
2013-07-29 14:58:14 +04:00
|
|
|
public static function rm($path)
|
|
|
|
{
|
2013-02-21 22:51:24 +04:00
|
|
|
self::clean($path);
|
2013-07-29 14:58:14 +04:00
|
|
|
if (is_dir($path)) {
|
2013-02-21 22:51:24 +04:00
|
|
|
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-07-29 14:58:14 +04:00
|
|
|
public function __construct($template_dir)
|
|
|
|
{
|
|
|
|
if ($_dir = realpath($template_dir)) {
|
2013-02-21 22:51:24 +04:00
|
|
|
$this->_path = $_dir;
|
|
|
|
} else {
|
|
|
|
throw new \LogicException("Template directory {$template_dir} doesn't exists");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-13 12:00:19 +04:00
|
|
|
* Get source and mtime of template by name
|
2013-02-21 22:51:24 +04:00
|
|
|
* @param string $tpl
|
2013-07-13 12:00:19 +04:00
|
|
|
* @param int $time load last modified time
|
2013-02-21 22:51:24 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
2013-07-29 14:58:14 +04:00
|
|
|
public function getSource($tpl, &$time)
|
|
|
|
{
|
2013-02-21 22:51:24 +04:00
|
|
|
$tpl = $this->_getTemplatePath($tpl);
|
2014-01-20 11:54:28 +04:00
|
|
|
clearstatcache(true, $tpl);
|
2013-02-21 22:51:24 +04:00
|
|
|
$time = filemtime($tpl);
|
|
|
|
return file_get_contents($tpl);
|
|
|
|
}
|
|
|
|
|
2013-07-13 12:00:19 +04:00
|
|
|
/**
|
|
|
|
* Get last modified of template by name
|
|
|
|
* @param string $tpl
|
|
|
|
* @return int
|
|
|
|
*/
|
2013-07-29 14:58:14 +04:00
|
|
|
public function getLastModified($tpl)
|
|
|
|
{
|
2014-01-20 11:54:28 +04:00
|
|
|
clearstatcache(true, $tpl = $this->_getTemplatePath($tpl));
|
2013-02-21 22:51:24 +04:00
|
|
|
return filemtime($tpl);
|
|
|
|
}
|
|
|
|
|
2013-07-13 12:00:19 +04:00
|
|
|
/**
|
|
|
|
* Get all names of templates from provider.
|
|
|
|
*
|
|
|
|
* @param string $extension all templates must have this extension, default .tpl
|
|
|
|
* @return array|\Iterator
|
|
|
|
*/
|
2013-07-29 14:58:14 +04:00
|
|
|
public function getList($extension = "tpl")
|
|
|
|
{
|
2013-07-13 12:00:19 +04:00
|
|
|
$list = array();
|
|
|
|
$iterator = new \RecursiveIteratorIterator(
|
|
|
|
new \RecursiveDirectoryIterator($this->_path,
|
|
|
|
\FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS),
|
|
|
|
\RecursiveIteratorIterator::CHILD_FIRST
|
|
|
|
);
|
|
|
|
$path_len = strlen($this->_path);
|
2013-07-29 14:58:14 +04:00
|
|
|
foreach ($iterator as $file) {
|
2013-07-13 12:00:19 +04:00
|
|
|
/* @var \SplFileInfo $file */
|
2013-07-29 14:58:14 +04:00
|
|
|
if ($file->isFile() && $file->getExtension() == $extension) {
|
2013-07-13 12:00:19 +04:00
|
|
|
$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
|
|
|
|
*/
|
2013-07-29 14:58:14 +04:00
|
|
|
protected function _getTemplatePath($tpl)
|
|
|
|
{
|
2014-01-20 11:54:28 +04:00
|
|
|
|
2013-07-29 14:58:14 +04:00
|
|
|
if (($path = realpath($this->_path . "/" . $tpl)) && strpos($path, $this->_path) === 0) {
|
2013-02-21 22:51:24 +04:00
|
|
|
return $path;
|
|
|
|
} else {
|
|
|
|
throw new \RuntimeException("Template $tpl not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-23 16:35:11 +04:00
|
|
|
/**
|
|
|
|
* @param string $tpl
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-07-29 14:58:14 +04:00
|
|
|
public function templateExists($tpl)
|
|
|
|
{
|
2014-01-20 11:54:28 +04:00
|
|
|
return ($path = realpath($this->_path . "/" . $tpl)) && strpos($path, $this->_path) === 0;
|
|
|
|
// return file_exists($this->_path . "/" . $tpl);
|
2013-02-23 16:35:11 +04:00
|
|
|
}
|
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
|
|
|
|
*/
|
2013-07-29 14:58:14 +04:00
|
|
|
public function verify(array $templates)
|
|
|
|
{
|
|
|
|
foreach ($templates as $template => $mtime) {
|
2014-01-20 11:54:28 +04:00
|
|
|
clearstatcache(true, $template = $this->_path . '/' . $template);
|
2013-07-29 14:58:14 +04:00
|
|
|
if (@filemtime($template) !== $mtime) {
|
2013-02-21 22:51:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|