mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Improve providers. Renders now use bulk checks via ::verify
This commit is contained in:
@@ -246,6 +246,7 @@ class Fenom {
|
||||
throw new InvalidArgumentException("Source must be a valid path or provider object");
|
||||
}
|
||||
$fenom = new static($provider);
|
||||
/* @var Fenom $fenom */
|
||||
$fenom->setCompileDir($compile_dir);
|
||||
if($options) {
|
||||
$fenom->setOptions($options);
|
||||
@@ -595,7 +596,6 @@ class Fenom {
|
||||
* @param $callback
|
||||
* @param float $chunk
|
||||
* @return \Fenom\Render
|
||||
* @example $fenom->pipe("products.yml.tpl", $iterators, [new SplFileObject("/tmp/products.yml"), "fwrite"], 512*1024)
|
||||
*/
|
||||
public function pipe($template, array $vars, $callback, $chunk = 1e6) {
|
||||
ob_start($callback, $chunk, true);
|
||||
|
||||
@@ -71,9 +71,9 @@ class Provider implements ProviderInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Get source and mtime of template by name
|
||||
* @param string $tpl
|
||||
* @param int $time
|
||||
* @param int $time load last modified time
|
||||
* @return string
|
||||
*/
|
||||
public function getSource($tpl, &$time) {
|
||||
@@ -83,13 +83,37 @@ class Provider implements ProviderInterface {
|
||||
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);
|
||||
}
|
||||
|
||||
public function getList() {
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,18 +138,6 @@ class Provider implements ProviderInterface {
|
||||
return file_exists($this->_path."/".$tpl);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $tpls
|
||||
* @return array
|
||||
*/
|
||||
public function getLastModifiedBatch($tpls) {
|
||||
$tpls = array_flip($tpls);
|
||||
foreach($tpls as $tpl => &$time) {
|
||||
$time = $this->getLastModified($tpl);
|
||||
}
|
||||
return $tpls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify templates (check change time)
|
||||
*
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
namespace Fenom;
|
||||
|
||||
/**
|
||||
* Template provider interface
|
||||
* Interface of templates provider
|
||||
* @package Fenom
|
||||
* @author Ivan Shalganov <a.cobest@gmail.com>
|
||||
*/
|
||||
@@ -20,6 +20,7 @@ interface ProviderInterface {
|
||||
* @return bool
|
||||
*/
|
||||
public function templateExists($tpl);
|
||||
|
||||
/**
|
||||
* @param string $tpl
|
||||
* @param int $time
|
||||
@@ -34,15 +35,16 @@ interface ProviderInterface {
|
||||
public function getLastModified($tpl);
|
||||
|
||||
/**
|
||||
* Verify templates by change time
|
||||
* Verify templates (check mtime)
|
||||
*
|
||||
* @param array $templates [template_name => modified, ...] By conversation you may trust the template's name
|
||||
* @return bool
|
||||
* @param array $templates [template_name => modified, ...] By conversation, you may trust the template's name
|
||||
* @return bool if true - all templates are valid else some templates are invalid
|
||||
*/
|
||||
public function verify(array $templates);
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* Get all names of template from provider
|
||||
* @return array|\Iterator
|
||||
*/
|
||||
public function getList();
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ class Render extends \ArrayObject {
|
||||
$this->_fenom = $fenom;
|
||||
$props += self::$_props;
|
||||
$this->_name = $props["name"];
|
||||
$this->_provider = $this->_fenom->getProvider($props["scm"]);
|
||||
// $this->_provider = $this->_fenom->getProvider($props["scm"]);
|
||||
$this->_scm = $props["scm"];
|
||||
$this->_time = $props["time"];
|
||||
$this->_depends = $props["depends"];
|
||||
@@ -100,7 +100,7 @@ class Render extends \ArrayObject {
|
||||
}
|
||||
|
||||
public function getProvider() {
|
||||
return $this->_provider;
|
||||
return $this->_fenom->getProvider($this->_scm);
|
||||
}
|
||||
|
||||
public function getBaseName() {
|
||||
@@ -136,14 +136,18 @@ class Render extends \ArrayObject {
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid() {
|
||||
$provider = $this->_fenom->getProvider(strstr($this->_name, ":"), true);
|
||||
if($provider->getLastModified($this->_name) >= $this->_time) {
|
||||
return false;
|
||||
}
|
||||
foreach($this->_depends as $tpl => $time) {
|
||||
if($this->_fenom->getTemplate($tpl)->getTime() !== $time) {
|
||||
if(count($this->_depends) === 1) { // if no external dependencies, only self
|
||||
$provider = $this->_fenom->getProvider($this->_scm);
|
||||
if($provider->getLastModified($this->_name) !== $this->_time) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
foreach($this->_depends as $scm => $templates) {
|
||||
$provider = $this->_fenom->getProvider($scm);
|
||||
if(!$provider->verify($templates)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user