Improve providers. Renders now use bulk checks via ::verify

This commit is contained in:
bzick 2013-07-13 12:00:19 +04:00
parent 3583a2cdfd
commit 617fc7324c
6 changed files with 93 additions and 42 deletions

View File

@ -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);

View File

@ -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)
*

View File

@ -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();
}

View File

@ -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;
}

View File

@ -55,6 +55,10 @@ class TestCase extends \PHPUnit_Framework_TestCase {
}
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);
}

View File

@ -1,8 +1,9 @@
<?php
namespace Fenom;
use Fenom;
use Fenom\TestCase;
class FSProviderTest extends \Fenom\TestCase {
class ProviderTest extends TestCase {
/**
* @var Provider
*/
@ -12,15 +13,19 @@ class FSProviderTest extends \Fenom\TestCase {
parent::setUp();
$this->tpl("template1.tpl", 'Template 1 {$a}');
$this->tpl("template2.tpl", 'Template 2 {$a}');
$this->tpl("sub/template3.tpl", 'Template 3 {$a}');
$this->provider = new Provider(FENOM_RESOURCES.'/template');
clearstatcache();
}
public function testIsTemplateExists() {
clearstatcache();
$this->assertTrue($this->provider->templateExists("template1.tpl"));
$this->assertFalse($this->provider->templateExists("unexists.tpl"));
}
public function testGetSource() {
clearstatcache();
$src = $this->provider->getSource("template1.tpl", $time);
clearstatcache();
$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");
}
public function testGetLastModifiedBatch() {
$times = $this->provider->getLastModifiedBatch($tpls = array("template1.tpl", "template2.tpl"));
$this->assertSame($tpls, array_keys($times));
public function testVerify() {
$templates = array(
"template1.tpl" => filemtime(FENOM_RESOURCES.'/template/template1.tpl'),
"template2.tpl" => filemtime(FENOM_RESOURCES.'/template/template2.tpl')
);
clearstatcache();
foreach($times as $template => $time) {
$this->assertEquals(filemtime(FENOM_RESOURCES."/template/$template"), $time);
}
$this->assertTrue($this->provider->verify($templates));
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));
}
/**
* @expectedException \RuntimeException
*/
public function testGetLastModifiedBatchInvalid() {
$this->provider->getLastModifiedBatch(array("template1.tpl", "unexists.tpl", "parent.tpl"));
public function testVerifyInvalid() {
$templates = array(
"template1.tpl" => filemtime(FENOM_RESOURCES.'/template/template1.tpl'),
"template2.tpl" => filemtime(FENOM_RESOURCES.'/template/template2.tpl') + 1
);
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);
}
}