fenom/src/Fenom/Provider.php

186 lines
5.2 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-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
{
2023-02-20 00:14:08 +03:00
private string $_path;
2013-02-21 22:51:24 +04:00
2023-02-20 00:14:08 +03:00
protected bool $_clear_cache = false;
2013-02-21 22:51:24 +04:00
/**
* Clean directory from files
*
* @param string $path
*/
2023-02-20 00:14:08 +03:00
public static function clean(string $path)
2013-07-29 14:58:14 +04:00
{
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()) {
2023-02-20 00:14:08 +03:00
if (!str_starts_with($file->getBasename(), ".")) {
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
*/
2023-02-20 00:14:08 +03:00
public static function rm(string $path)
2013-07-29 14:58:14 +04:00
{
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
2023-02-20 00:14:08 +03:00
* @throws \LogicException if directory doesn't exist
2013-04-28 11:33:36 +04:00
*/
2023-02-20 00:14:08 +03:00
public function __construct(string $template_dir)
2013-07-29 14:58:14 +04:00
{
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");
}
}
/**
* Disable PHP cache for files. PHP cache some operations with files then script works.
* @see http://php.net/manual/en/function.clearstatcache.php
* @param bool $status
*/
2023-02-20 00:14:08 +03:00
public function setClearCachedStats(bool $status = true) {
$this->_clear_cache = $status;
}
2013-02-21 22:51:24 +04:00
/**
* Get source and mtime of template by name
2013-02-21 22:51:24 +04:00
* @param string $tpl
2023-02-20 00:14:08 +03:00
* @param float|null $time load last modified time
2013-02-21 22:51:24 +04:00
* @return string
*/
2023-02-20 00:14:08 +03:00
public function getSource(string $tpl, ?float &$time): string
2013-07-29 14:58:14 +04:00
{
2013-02-21 22:51:24 +04:00
$tpl = $this->_getTemplatePath($tpl);
if($this->_clear_cache) {
clearstatcache(true, $tpl);
}
2013-02-21 22:51:24 +04:00
$time = filemtime($tpl);
return file_get_contents($tpl);
}
/**
* Get last modified of template by name
* @param string $tpl
2023-02-20 00:14:08 +03:00
* @return float
*/
2023-02-20 00:14:08 +03:00
public function getLastModified(string $tpl): float
2013-07-29 14:58:14 +04:00
{
$tpl = $this->_getTemplatePath($tpl);
if($this->_clear_cache) {
clearstatcache(true, $tpl);
}
2023-02-20 00:14:08 +03:00
return (float)filemtime($tpl);
2013-02-21 22:51:24 +04:00
}
/**
* Get all names of templates from provider.
*
* @param string $extension all templates must have this extension, default .tpl
2023-02-20 00:14:08 +03:00
* @return iterable
*/
2023-02-20 00:14:08 +03:00
public function getList(string $extension = "tpl"): iterable
2013-07-29 14:58:14 +04:00
{
2014-05-06 14:22:58 +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) {
/* @var \SplFileInfo $file */
2013-07-29 14:58:14 +04:00
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
2023-02-20 00:14:08 +03:00
* @param string $tpl
2013-02-21 22:51:24 +04:00
* @return string
* @throws \RuntimeException
*/
2023-02-20 00:14:08 +03:00
protected function _getTemplatePath(string $tpl): string
2013-07-29 14:58:14 +04:00
{
2015-01-08 12:44:38 +03:00
$path = realpath($this->_path . "/" . $tpl);
2023-02-20 00:14:08 +03:00
if ($path && str_starts_with($path, $this->_path)) {
2013-02-21 22:51:24 +04:00
return $path;
} else {
throw new \RuntimeException("Template $tpl not found");
}
}
/**
* @param string $tpl
* @return bool
*/
2023-02-20 00:14:08 +03:00
public function templateExists(string $tpl): bool
2013-07-29 14:58:14 +04:00
{
2023-02-20 00:14:08 +03:00
return ($path = realpath($this->_path . "/" . $tpl)) && str_starts_with($path, $this->_path);
}
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
*/
2023-02-20 00:14:08 +03:00
public function verify(array $templates): bool
2013-07-29 14:58:14 +04:00
{
foreach ($templates as $template => $mtime) {
$template = $this->_path . '/' . $template;
if($this->_clear_cache) {
clearstatcache(true, $template);
}
2023-02-22 00:09:00 +03:00
if (@filemtime($template) != $mtime) {
2013-02-21 22:51:24 +04:00
return false;
}
}
return true;
}
}