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:
parent
3583a2cdfd
commit
617fc7324c
@ -246,6 +246,7 @@ class Fenom {
|
|||||||
throw new InvalidArgumentException("Source must be a valid path or provider object");
|
throw new InvalidArgumentException("Source must be a valid path or provider object");
|
||||||
}
|
}
|
||||||
$fenom = new static($provider);
|
$fenom = new static($provider);
|
||||||
|
/* @var Fenom $fenom */
|
||||||
$fenom->setCompileDir($compile_dir);
|
$fenom->setCompileDir($compile_dir);
|
||||||
if($options) {
|
if($options) {
|
||||||
$fenom->setOptions($options);
|
$fenom->setOptions($options);
|
||||||
@ -595,7 +596,6 @@ class Fenom {
|
|||||||
* @param $callback
|
* @param $callback
|
||||||
* @param float $chunk
|
* @param float $chunk
|
||||||
* @return \Fenom\Render
|
* @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) {
|
public function pipe($template, array $vars, $callback, $chunk = 1e6) {
|
||||||
ob_start($callback, $chunk, true);
|
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 string $tpl
|
||||||
* @param int $time
|
* @param int $time load last modified time
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getSource($tpl, &$time) {
|
public function getSource($tpl, &$time) {
|
||||||
@ -83,13 +83,37 @@ class Provider implements ProviderInterface {
|
|||||||
return file_get_contents($tpl);
|
return file_get_contents($tpl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get last modified of template by name
|
||||||
|
* @param string $tpl
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
public function getLastModified($tpl) {
|
public function getLastModified($tpl) {
|
||||||
clearstatcache(null, $tpl = $this->_getTemplatePath($tpl));
|
clearstatcache(null, $tpl = $this->_getTemplatePath($tpl));
|
||||||
return filemtime($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);
|
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)
|
* Verify templates (check change time)
|
||||||
*
|
*
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
namespace Fenom;
|
namespace Fenom;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Template provider interface
|
* Interface of templates provider
|
||||||
* @package Fenom
|
* @package Fenom
|
||||||
* @author Ivan Shalganov <a.cobest@gmail.com>
|
* @author Ivan Shalganov <a.cobest@gmail.com>
|
||||||
*/
|
*/
|
||||||
@ -20,6 +20,7 @@ interface ProviderInterface {
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function templateExists($tpl);
|
public function templateExists($tpl);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $tpl
|
* @param string $tpl
|
||||||
* @param int $time
|
* @param int $time
|
||||||
@ -34,15 +35,16 @@ interface ProviderInterface {
|
|||||||
public function getLastModified($tpl);
|
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
|
* @param array $templates [template_name => modified, ...] By conversation, you may trust the template's name
|
||||||
* @return bool
|
* @return bool if true - all templates are valid else some templates are invalid
|
||||||
*/
|
*/
|
||||||
public function verify(array $templates);
|
public function verify(array $templates);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* Get all names of template from provider
|
||||||
|
* @return array|\Iterator
|
||||||
*/
|
*/
|
||||||
public function getList();
|
public function getList();
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ class Render extends \ArrayObject {
|
|||||||
$this->_fenom = $fenom;
|
$this->_fenom = $fenom;
|
||||||
$props += self::$_props;
|
$props += self::$_props;
|
||||||
$this->_name = $props["name"];
|
$this->_name = $props["name"];
|
||||||
$this->_provider = $this->_fenom->getProvider($props["scm"]);
|
// $this->_provider = $this->_fenom->getProvider($props["scm"]);
|
||||||
$this->_scm = $props["scm"];
|
$this->_scm = $props["scm"];
|
||||||
$this->_time = $props["time"];
|
$this->_time = $props["time"];
|
||||||
$this->_depends = $props["depends"];
|
$this->_depends = $props["depends"];
|
||||||
@ -100,7 +100,7 @@ class Render extends \ArrayObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function getProvider() {
|
public function getProvider() {
|
||||||
return $this->_provider;
|
return $this->_fenom->getProvider($this->_scm);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getBaseName() {
|
public function getBaseName() {
|
||||||
@ -136,14 +136,18 @@ class Render extends \ArrayObject {
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isValid() {
|
public function isValid() {
|
||||||
$provider = $this->_fenom->getProvider(strstr($this->_name, ":"), true);
|
if(count($this->_depends) === 1) { // if no external dependencies, only self
|
||||||
if($provider->getLastModified($this->_name) >= $this->_time) {
|
$provider = $this->_fenom->getProvider($this->_scm);
|
||||||
return false;
|
if($provider->getLastModified($this->_name) !== $this->_time) {
|
||||||
}
|
|
||||||
foreach($this->_depends as $tpl => $time) {
|
|
||||||
if($this->_fenom->getTemplate($tpl)->getTime() !== $time) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
foreach($this->_depends as $scm => $templates) {
|
||||||
|
$provider = $this->_fenom->getProvider($scm);
|
||||||
|
if(!$provider->verify($templates)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,10 @@ class TestCase extends \PHPUnit_Framework_TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function tpl($name, $code) {
|
public function tpl($name, $code) {
|
||||||
|
$dir = dirname($name);
|
||||||
|
if($dir != "." && !is_dir(FENOM_RESOURCES.'/template/'.$dir)) {
|
||||||
|
mkdir(FENOM_RESOURCES.'/template/'.$dir, 0777, true);
|
||||||
|
}
|
||||||
file_put_contents(FENOM_RESOURCES.'/template/'.$name, $code);
|
file_put_contents(FENOM_RESOURCES.'/template/'.$name, $code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Fenom;
|
namespace Fenom;
|
||||||
use Fenom;
|
use Fenom;
|
||||||
|
use Fenom\TestCase;
|
||||||
|
|
||||||
class FSProviderTest extends \Fenom\TestCase {
|
class ProviderTest extends TestCase {
|
||||||
/**
|
/**
|
||||||
* @var Provider
|
* @var Provider
|
||||||
*/
|
*/
|
||||||
@ -12,15 +13,19 @@ class FSProviderTest extends \Fenom\TestCase {
|
|||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->tpl("template1.tpl", 'Template 1 {$a}');
|
$this->tpl("template1.tpl", 'Template 1 {$a}');
|
||||||
$this->tpl("template2.tpl", 'Template 2 {$a}');
|
$this->tpl("template2.tpl", 'Template 2 {$a}');
|
||||||
|
$this->tpl("sub/template3.tpl", 'Template 3 {$a}');
|
||||||
$this->provider = new Provider(FENOM_RESOURCES.'/template');
|
$this->provider = new Provider(FENOM_RESOURCES.'/template');
|
||||||
|
clearstatcache();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsTemplateExists() {
|
public function testIsTemplateExists() {
|
||||||
|
clearstatcache();
|
||||||
$this->assertTrue($this->provider->templateExists("template1.tpl"));
|
$this->assertTrue($this->provider->templateExists("template1.tpl"));
|
||||||
$this->assertFalse($this->provider->templateExists("unexists.tpl"));
|
$this->assertFalse($this->provider->templateExists("unexists.tpl"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetSource() {
|
public function testGetSource() {
|
||||||
|
clearstatcache();
|
||||||
$src = $this->provider->getSource("template1.tpl", $time);
|
$src = $this->provider->getSource("template1.tpl", $time);
|
||||||
clearstatcache();
|
clearstatcache();
|
||||||
$this->assertEquals(file_get_contents(FENOM_RESOURCES.'/template/template1.tpl'), $src);
|
$this->assertEquals(file_get_contents(FENOM_RESOURCES.'/template/template1.tpl'), $src);
|
||||||
@ -47,21 +52,45 @@ class FSProviderTest extends \Fenom\TestCase {
|
|||||||
$this->provider->getLastModified("unexists.tpl");
|
$this->provider->getLastModified("unexists.tpl");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetLastModifiedBatch() {
|
public function testVerify() {
|
||||||
$times = $this->provider->getLastModifiedBatch($tpls = array("template1.tpl", "template2.tpl"));
|
$templates = array(
|
||||||
$this->assertSame($tpls, array_keys($times));
|
"template1.tpl" => filemtime(FENOM_RESOURCES.'/template/template1.tpl'),
|
||||||
|
"template2.tpl" => filemtime(FENOM_RESOURCES.'/template/template2.tpl')
|
||||||
|
);
|
||||||
clearstatcache();
|
clearstatcache();
|
||||||
foreach($times as $template => $time) {
|
$this->assertTrue($this->provider->verify($templates));
|
||||||
$this->assertEquals(filemtime(FENOM_RESOURCES."/template/$template"), $time);
|
clearstatcache();
|
||||||
}
|
$templates = array(
|
||||||
|
"template2.tpl" => filemtime(FENOM_RESOURCES.'/template/template2.tpl'),
|
||||||
|
"template1.tpl" => filemtime(FENOM_RESOURCES.'/template/template1.tpl')
|
||||||
|
);
|
||||||
|
clearstatcache();
|
||||||
|
$this->assertTrue($this->provider->verify($templates));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testVerifyInvalid() {
|
||||||
* @expectedException \RuntimeException
|
$templates = array(
|
||||||
*/
|
"template1.tpl" => filemtime(FENOM_RESOURCES.'/template/template1.tpl'),
|
||||||
public function testGetLastModifiedBatchInvalid() {
|
"template2.tpl" => filemtime(FENOM_RESOURCES.'/template/template2.tpl') + 1
|
||||||
$this->provider->getLastModifiedBatch(array("template1.tpl", "unexists.tpl", "parent.tpl"));
|
);
|
||||||
|
clearstatcache();
|
||||||
|
$this->assertFalse($this->provider->verify($templates));
|
||||||
|
clearstatcache();
|
||||||
|
$templates = array(
|
||||||
|
"template1.tpl" => filemtime(FENOM_RESOURCES.'/template/template1.tpl'),
|
||||||
|
"unexists.tpl" => 1234567890
|
||||||
|
);
|
||||||
|
$this->assertFalse($this->provider->verify($templates));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetAll() {
|
||||||
|
$list = $this->provider->getList();
|
||||||
|
sort($list);
|
||||||
|
$this->assertSame(array(
|
||||||
|
"sub/template3.tpl",
|
||||||
|
"template1.tpl",
|
||||||
|
"template2.tpl"
|
||||||
|
), $list);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user