mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Add custom additionally options for template
This commit is contained in:
parent
bd056bf75b
commit
3fe541029c
@ -561,21 +561,24 @@ class Aspect {
|
|||||||
* Return template by name
|
* Return template by name
|
||||||
*
|
*
|
||||||
* @param string $template
|
* @param string $template
|
||||||
|
* @param int $options
|
||||||
* @return Aspect\Template
|
* @return Aspect\Template
|
||||||
*/
|
*/
|
||||||
public function getTemplate($template) {
|
public function getTemplate($template, $options = 0) {
|
||||||
if(isset($this->_storage[ $template ])) {
|
$options = $this->_options | $options;
|
||||||
|
$key = $template.".".dechex($options);
|
||||||
|
if(isset($this->_storage[ $key ])) {
|
||||||
/** @var Aspect\Template $tpl */
|
/** @var Aspect\Template $tpl */
|
||||||
$tpl = $this->_storage[ $template ];
|
$tpl = $this->_storage[ $key ];
|
||||||
if(($this->_options & self::AUTO_RELOAD) && !$tpl->isValid()) {
|
if(($this->_options & self::AUTO_RELOAD) && !$tpl->isValid()) {
|
||||||
return $this->_storage[ $template ] = $this->compile($template);
|
return $this->_storage[ $key ] = $this->compile($template, true, $options);
|
||||||
} else {
|
} else {
|
||||||
return $this->_storage[ $template ];
|
return $tpl;
|
||||||
}
|
}
|
||||||
} elseif($this->_options & self::FORCE_COMPILE) {
|
} elseif($this->_options & self::FORCE_COMPILE) {
|
||||||
return $this->compile($template, $this->_options & self::DISABLE_CACHE);
|
return $this->compile($template, $this->_options & self::DISABLE_CACHE & ~self::FORCE_COMPILE, $options);
|
||||||
} else {
|
} else {
|
||||||
return $this->_storage[ $template ] = $this->_load($template);
|
return $this->_storage[ $key ] = $this->_load($template, $options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -583,19 +586,19 @@ class Aspect {
|
|||||||
* Add custom template into storage
|
* Add custom template into storage
|
||||||
* @param Aspect\Render $template
|
* @param Aspect\Render $template
|
||||||
*/
|
*/
|
||||||
public function addTemplate(Aspect\Render $template) {
|
public function addTemplate(Aspect\Render $template, $options = 0) {
|
||||||
$this->_storage[ $template->getName() ] = $template;
|
$this->_storage[ $template->getName().".".dechex($options) ] = $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return template from storage or create if template doesn't exists.
|
* Return template from storage or create if template doesn't exists.
|
||||||
*
|
*
|
||||||
* @param string $tpl
|
* @param string $tpl
|
||||||
* @throws \RuntimeException
|
* @param int $opts
|
||||||
* @return Aspect\Render
|
* @return Aspect\Render
|
||||||
*/
|
*/
|
||||||
protected function _load($tpl) {
|
protected function _load($tpl, $opts) {
|
||||||
$file_name = $this->_getHash($tpl);
|
$file_name = $this->_getCacheName($tpl, $opts);
|
||||||
if(!is_file($this->_compile_dir."/".$file_name)) {
|
if(!is_file($this->_compile_dir."/".$file_name)) {
|
||||||
return $this->compile($tpl);
|
return $this->compile($tpl);
|
||||||
} else {
|
} else {
|
||||||
@ -608,11 +611,12 @@ class Aspect {
|
|||||||
* Generate unique name of compiled template
|
* Generate unique name of compiled template
|
||||||
*
|
*
|
||||||
* @param string $tpl
|
* @param string $tpl
|
||||||
|
* @param int $options
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function _getHash($tpl) {
|
private function _getCacheName($tpl, $options) {
|
||||||
$hash = $tpl.":".$this->_options;
|
$hash = $tpl.":".$options;
|
||||||
return sprintf("%s.%u.%d.php", str_replace(":", "_", basename($tpl)), crc32($hash), strlen($hash));
|
return sprintf("%s.%x.%x.php", str_replace(":", "_", basename($tpl)), crc32($hash), strlen($hash));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -620,20 +624,22 @@ class Aspect {
|
|||||||
*
|
*
|
||||||
* @param string $tpl
|
* @param string $tpl
|
||||||
* @param bool $store store template on disk
|
* @param bool $store store template on disk
|
||||||
|
* @param int $opts
|
||||||
* @throws RuntimeException
|
* @throws RuntimeException
|
||||||
* @return \Aspect\Template
|
* @return \Aspect\Template
|
||||||
*/
|
*/
|
||||||
public function compile($tpl, $store = true) {
|
public function compile($tpl, $store = true, $opts = 0) {
|
||||||
$template = Template::factory($this)->load($tpl);
|
$template = Template::factory($this)->load($tpl);
|
||||||
if($store) {
|
if($store) {
|
||||||
$tpl_tmp = tempnam($this->_compile_dir, basename($tpl));
|
$cache = $this->_getCacheName($tpl, $opts);
|
||||||
|
$tpl_tmp = tempnam($this->_compile_dir, $cache);
|
||||||
$tpl_fp = fopen($tpl_tmp, "w");
|
$tpl_fp = fopen($tpl_tmp, "w");
|
||||||
if(!$tpl_fp) {
|
if(!$tpl_fp) {
|
||||||
throw new \RuntimeException("Can't to open temporary file $tpl_tmp. Directory ".$this->_compile_dir." is writable?");
|
throw new \RuntimeException("Can't to open temporary file $tpl_tmp. Directory ".$this->_compile_dir." is writable?");
|
||||||
}
|
}
|
||||||
fwrite($tpl_fp, $template->getTemplateCode());
|
fwrite($tpl_fp, $template->getTemplateCode());
|
||||||
fclose($tpl_fp);
|
fclose($tpl_fp);
|
||||||
$file_name = $this->_compile_dir."/".$this->_getHash($tpl);
|
$file_name = $this->_compile_dir."/".$cache;
|
||||||
if(!rename($tpl_tmp, $file_name)) {
|
if(!rename($tpl_tmp, $file_name)) {
|
||||||
throw new \RuntimeException("Can't to move $tpl_tmp to $tpl");
|
throw new \RuntimeException("Can't to move $tpl_tmp to $tpl");
|
||||||
}
|
}
|
||||||
@ -647,8 +653,12 @@ class Aspect {
|
|||||||
* @param bool $cache
|
* @param bool $cache
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function clearCompiledTemplate($tpl, $cache = true) {
|
/*public function clearCompiledTemplate($tpl, $cache = true) {
|
||||||
$file_name = $this->_compile_dir."/".$this->_getHash($tpl);
|
$file_name = $this->_compile_dir."/".$this->_getCacheName($tpl);
|
||||||
|
$it = new \GlobIterator($this->_compile_dir."/".str_replace(':', '_', basename($tpl)));
|
||||||
|
foreach() {
|
||||||
|
|
||||||
|
}
|
||||||
if(file_exists($file_name)) {
|
if(file_exists($file_name)) {
|
||||||
if($cache) {
|
if($cache) {
|
||||||
unset($this->_storage[$tpl]);
|
unset($this->_storage[$tpl]);
|
||||||
@ -657,7 +667,7 @@ class Aspect {
|
|||||||
} else {
|
} else {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -645,6 +645,7 @@ class TemplateTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @group include
|
||||||
* @dataProvider providerInclude
|
* @dataProvider providerInclude
|
||||||
*/
|
*/
|
||||||
public function testInclude($code, $vars, $result) {
|
public function testInclude($code, $vars, $result) {
|
||||||
|
@ -36,9 +36,9 @@ class AspectTest extends \Aspect\TestCase {
|
|||||||
public function testStorage() {
|
public function testStorage() {
|
||||||
$this->tpl('custom.tpl', 'Custom template');
|
$this->tpl('custom.tpl', 'Custom template');
|
||||||
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
||||||
$this->aspect->clearCompiledTemplate('custom.tpl', false);
|
//$this->aspect->clearCompiledTemplate('custom.tpl', false);
|
||||||
|
|
||||||
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
//$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
||||||
|
|
||||||
$this->tpl('custom.tpl', 'Custom template 2');
|
$this->tpl('custom.tpl', 'Custom template 2');
|
||||||
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
||||||
|
Loading…
Reference in New Issue
Block a user