Done typical extends

This commit is contained in:
bzick 2013-05-19 02:04:52 +04:00
parent 12a0c9589f
commit 319bf568ba
4 changed files with 43 additions and 50 deletions

View File

@ -37,7 +37,7 @@ class Cytro {
const SMART_FUNC_PARSER = 'Cytro\Compiler::smartFuncParser';
/**
* @var array of possible options, as associative array
* @var int[] of possible options, as associative array
* @see setOptions, addOptions, delOptions
*/
private static $_option_list = array(
@ -50,7 +50,7 @@ class Cytro {
);
/**
* @var array Templates storage
* @var Cytro\Render[] Templates storage
*/
protected $_storage = array();
/**
@ -72,12 +72,12 @@ class Cytro {
*/
private $_provider;
/**
* @var array of Cytro\ProviderInterface
* @var Cytro\ProviderInterface[]
*/
protected $_providers = array();
/**
* @var array of modifiers [modifier_name => callable]
* @var string[] list of modifiers [modifier_name => callable]
*/
protected $_modifiers = array(
"upper" => 'strtoupper',
@ -104,7 +104,7 @@ class Cytro {
);
/**
* @var array of compilers and functions
* @var array[] of compilers and functions
*/
protected $_actions = array(
'foreach' => array( // {foreach ...} {break} {continue} {foreachelse} {/foreach}
@ -240,6 +240,7 @@ class Cytro {
/**
* Set compile directory
*
* @param string $dir directory to store compiled templates in
* @return Cytro
*/
@ -274,8 +275,8 @@ class Cytro {
/**
* Add modifier
*
* @param string $modifier
* @param string $callback
* @param string $modifier the modifier name
* @param string $callback the modifier callback
* @return Cytro
*/
public function addModifier($modifier, $callback) {
@ -558,7 +559,6 @@ class Cytro {
*
* @param string $template name of template
* @param array $vars array of data for template
* @internal param int $options
* @return mixed
*/
public function fetch($template, array $vars = array()) {
@ -566,15 +566,14 @@ class Cytro {
}
/**
* Return template by name
* Get template by name
*
* @param string $template
* @param int $options
* @param string $template template name with schema
* @param int $options additional options and flags
* @return Cytro\Template
*/
public function getTemplate($template, $options = 0) {
$options = $this->_options | $options;
$key = $template.".".dechex($options);
$key = dechex($this->_options | $options)."@".$template;
if(isset($this->_storage[ $key ])) {
/** @var Cytro\Template $tpl */
$tpl = $this->_storage[ $key ];
@ -592,6 +591,7 @@ class Cytro {
/**
* Add custom template into storage
*
* @param Cytro\Render $template
*/
public function addTemplate(Cytro\Render $template) {
@ -599,7 +599,7 @@ class Cytro {
}
/**
* Return template from storage or create if template doesn't exists.
* Load template from cache or create cache if it doesn't exists.
*
* @param string $tpl
* @param int $opts
@ -608,7 +608,7 @@ class Cytro {
protected function _load($tpl, $opts) {
$file_name = $this->_getCacheName($tpl, $opts);
if(!is_file($this->_compile_dir."/".$file_name)) {
return $this->compile($tpl);
return $this->compile($tpl, true, $opts);
} else {
$cytro = $this;
return include($this->_compile_dir."/".$file_name);
@ -656,30 +656,15 @@ class Cytro {
return $template;
}
/**
* @param string $tpl
* @param bool $cache
* @return bool
* Flush internal memory template cache
*/
/*public function clearCompiledTemplate($tpl, $cache = true) {
$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($cache) {
unset($this->_storage[$tpl]);
}
return unlink($file_name);
} else {
return true;
}
}*/
public function flush() {
$this->_storage = array();
}
/**
*
* Remove all compiled templates
*/
public function clearAllCompiles() {
\Cytro\FSProvider::clean($this->_compile_dir);

View File

@ -384,11 +384,9 @@ class Compiler {
if(empty($tpl->_extended)) {
$tpl->addPostCompile(__CLASS__."::extendBody");
}
/*if($tpl->getOptions() & Template::EXTENDED) {
if($tpl->getOptions() & Template::EXTENDED) {
$tpl->_compatible = true;
} else {
$tpl->_compatible = false;
}*/
}
if($name) { // static extends
$tpl->_extends = $tpl->getStorage()->getRawTemplate()->load($name, false);
// $tpl->_compatible = &$tpl->_extends->_compatible;
@ -398,6 +396,9 @@ class Compiler {
$tpl->addDepend($tpl->_extends);
return "";
} else { // dynamic extends
if(!isset($tpl->_compatible)) {
$tpl->_compatible = false;
}
$tpl->_extends = $tpl_name;
return '$parent = $tpl->getStorage()->getTemplate('.$tpl_name.', \Cytro\Template::EXTENDED);';
}

View File

@ -84,7 +84,7 @@ class Template extends Render {
*/
public function __construct(Cytro $cytro, $options) {
$this->_cytro = $cytro;
$this->_options = $this->_cytro->getOptions();
$this->_options = $options;
}
/**

View File

@ -5,17 +5,17 @@ use Symfony\Component\Process\Exception\LogicException;
class ExtendsTemplateTest extends TestCase {
public static function templates() {
public static function templates(array $vars) {
return array(
array(
"name" => "level.0.tpl",
"level" => 0,
"blocks" => array(
"b1" => "default 5",
"b1" => "default {\$default}",
"b2" => "empty 0"
),
"result" => array(
"b1" => "default 5",
"b1" => "default ".$vars["default"],
"b2" => "empty 0"
),
),
@ -57,9 +57,9 @@ class ExtendsTemplateTest extends TestCase {
);
}
public static function generate($block_mask, $extend_mask) {
public static function generate($block_mask, $extend_mask, array $vars) {
$t = array();
foreach(self::templates() as $level => $tpl) {
foreach(self::templates($vars) as $level => $tpl) {
$src = 'level#'.$level.' ';
foreach($tpl["blocks"] as $bname => &$bcode) {
$src .= sprintf($block_mask, $bname, $bname.': '.$bcode)." level#$level ";
@ -87,19 +87,26 @@ class ExtendsTemplateTest extends TestCase {
"level" => "level",
"default" => 5
);
$tpls = self::generate('{block "%s"}%s{/block}', '{extends "level.%d.tpl"}');
$tpls = self::generate('{block "%s"}%s{/block}', '{extends "level.%d.tpl"}', $vars);
foreach($tpls as $name => $tpl) {
$this->tpl($name, $tpl["src"]);
// var_dump($src, "----\n\n----", $dst);ob_flush();fgetc(STDIN);
$this->assertSame($this->cytro->fetch($name, $vars), $tpl["dst"]);
}
$tpls = self::generate('{block "{$%s}"}%s{/block}', '{extends "level.%d.tpl"}');
$vars["default"]++;
$this->cytro->flush();
$tpls = self::generate('{block "{$%s}"}%s{/block}', '{extends "level.%d.tpl"}', $vars);
arsort($tpls);
foreach($tpls as $name => $tpl) {
$this->tpl("d.".$name, $tpl["src"]);
// var_dump($tpl["src"], "----\n\n----", $tpl["dst"]);ob_flush();fgetc(STDIN);
$this->assertSame($this->cytro->fetch("d.".$name, $vars), $tpl["dst"]);
// var_dump($name);ob_flush();fgets(STDIN);
}
$vars["default"]++;
$this->cytro->flush();
$tpls = self::generate('{block "%s"}%s{/block}', '{extends "$level.%d.tpl"}', $vars);
arsort($tpls);
foreach($tpls as $name => $tpl) {
$this->tpl("x.".$name, $tpl["src"]);
$this->assertSame($this->cytro->fetch("x.".$name, $vars), $tpl["dst"]);
}
}
}