Fix #126: disable clearcachestats() by default in Fenom\Provider. Add method Fenom\Provider->setClearCachedStats() for enable clearcachestats()

This commit is contained in:
bzick 2015-01-02 17:43:41 +03:00
parent 7e8598ae3b
commit 4ca7ccb5e0
4 changed files with 30 additions and 17 deletions

8
.gitignore vendored
View File

@ -1,17 +1,9 @@
.idea
vendor
coverage
build
composer.lock
composer.phar
tests/resources/compile/*
!.gitkeep
tests/resources/template/*
!.gitkeep
sandbox/compiled/*
!.gitkeep
benchmark/compile/*
benchmark/templates/inheritance/smarty
benchmark/templates/inheritance/twig
benchmark/sandbox/compiled/*
!.gitkeep

View File

@ -874,7 +874,8 @@ class Fenom
/**
* Execute template and write result into stdout
*
* @param string $template name of template
* @param string|array $template name of template.
* If it is array of names of templates they will be extended from left to right.
* @param array $vars array of data for template
* @return Fenom\Render
*/
@ -885,7 +886,8 @@ class Fenom
/**
*
* @param string $template name of template
* @param string|array $template name of template.
* If it is array of names of templates they will be extended from left to right.
* @param array $vars array of data for template
* @return mixed
*/
@ -897,7 +899,8 @@ class Fenom
/**
* Creates pipe-line of template's data to callback
* @note Method not works correctly in old PHP 5.3.*
* @param string $template name of the template
* @param string|array $template name of the template.
* If it is array of names of templates they will be extended from left to right.
* @param callable $callback template's data handler
* @param array $vars
* @param float $chunk amount of bytes of chunk

View File

@ -9,8 +9,6 @@
*/
namespace Fenom;
use Fenom\ProviderInterface;
/**
* Base template provider
* @author Ivan Shalganov
@ -19,6 +17,8 @@ class Provider implements ProviderInterface
{
private $_path;
protected $_clear_cache = false;
/**
* Clean directory from files
*
@ -75,6 +75,15 @@ class Provider implements ProviderInterface
}
}
/**
* 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
*/
public function setClearCachedStats($status = true) {
$this->_clear_cache = $status;
}
/**
* Get source and mtime of template by name
* @param string $tpl
@ -84,7 +93,9 @@ class Provider implements ProviderInterface
public function getSource($tpl, &$time)
{
$tpl = $this->_getTemplatePath($tpl);
clearstatcache(true, $tpl);
if($this->_clear_cache) {
clearstatcache(true, $tpl);
}
$time = filemtime($tpl);
return file_get_contents($tpl);
}
@ -96,7 +107,10 @@ class Provider implements ProviderInterface
*/
public function getLastModified($tpl)
{
clearstatcache(true, $tpl = $this->_getTemplatePath($tpl));
$tpl = $this->_getTemplatePath($tpl);
if($this->_clear_cache) {
clearstatcache(true, $tpl);
}
return filemtime($tpl);
}
@ -158,7 +172,10 @@ class Provider implements ProviderInterface
public function verify(array $templates)
{
foreach ($templates as $template => $mtime) {
clearstatcache(true, $template = $this->_path . '/' . $template);
$template = $this->_path . '/' . $template;
if($this->_clear_cache) {
clearstatcache(true, $template);
}
if (@filemtime($template) !== $mtime) {
return false;
}

View File

@ -96,6 +96,7 @@ class FenomTest extends \Fenom\TestCase
public function testCheckMTime()
{
$this->fenom->setOptions(Fenom::FORCE_COMPILE);
$this->fenom->getProvider()->setClearCachedStats();
$this->tpl('custom.tpl', 'Custom template');
$this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array()));
$tpl = $this->fenom->getTemplate('custom.tpl');