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

View File

@@ -384,11 +384,9 @@ class Compiler {
if(empty($tpl->_extended)) { if(empty($tpl->_extended)) {
$tpl->addPostCompile(__CLASS__."::extendBody"); $tpl->addPostCompile(__CLASS__."::extendBody");
} }
/*if($tpl->getOptions() & Template::EXTENDED) { if($tpl->getOptions() & Template::EXTENDED) {
$tpl->_compatible = true; $tpl->_compatible = true;
} else { }
$tpl->_compatible = false;
}*/
if($name) { // static extends if($name) { // static extends
$tpl->_extends = $tpl->getStorage()->getRawTemplate()->load($name, false); $tpl->_extends = $tpl->getStorage()->getRawTemplate()->load($name, false);
// $tpl->_compatible = &$tpl->_extends->_compatible; // $tpl->_compatible = &$tpl->_extends->_compatible;
@@ -398,6 +396,9 @@ class Compiler {
$tpl->addDepend($tpl->_extends); $tpl->addDepend($tpl->_extends);
return ""; return "";
} else { // dynamic extends } else { // dynamic extends
if(!isset($tpl->_compatible)) {
$tpl->_compatible = false;
}
$tpl->_extends = $tpl_name; $tpl->_extends = $tpl_name;
return '$parent = $tpl->getStorage()->getTemplate('.$tpl_name.', \Cytro\Template::EXTENDED);'; 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) { public function __construct(Cytro $cytro, $options) {
$this->_cytro = $cytro; $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 { class ExtendsTemplateTest extends TestCase {
public static function templates() { public static function templates(array $vars) {
return array( return array(
array( array(
"name" => "level.0.tpl", "name" => "level.0.tpl",
"level" => 0, "level" => 0,
"blocks" => array( "blocks" => array(
"b1" => "default 5", "b1" => "default {\$default}",
"b2" => "empty 0" "b2" => "empty 0"
), ),
"result" => array( "result" => array(
"b1" => "default 5", "b1" => "default ".$vars["default"],
"b2" => "empty 0" "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(); $t = array();
foreach(self::templates() as $level => $tpl) { foreach(self::templates($vars) as $level => $tpl) {
$src = 'level#'.$level.' '; $src = 'level#'.$level.' ';
foreach($tpl["blocks"] as $bname => &$bcode) { foreach($tpl["blocks"] as $bname => &$bcode) {
$src .= sprintf($block_mask, $bname, $bname.': '.$bcode)." level#$level "; $src .= sprintf($block_mask, $bname, $bname.': '.$bcode)." level#$level ";
@@ -87,19 +87,26 @@ class ExtendsTemplateTest extends TestCase {
"level" => "level", "level" => "level",
"default" => 5 "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) { foreach($tpls as $name => $tpl) {
$this->tpl($name, $tpl["src"]); $this->tpl($name, $tpl["src"]);
// var_dump($src, "----\n\n----", $dst);ob_flush();fgetc(STDIN);
$this->assertSame($this->cytro->fetch($name, $vars), $tpl["dst"]); $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); arsort($tpls);
foreach($tpls as $name => $tpl) { foreach($tpls as $name => $tpl) {
$this->tpl("d.".$name, $tpl["src"]); $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"]); $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"]);
} }
} }
} }