Optimize extends

This commit is contained in:
bzick
2013-06-08 00:08:00 +04:00
parent f42d0cff5d
commit a515c8e969
13 changed files with 275 additions and 113 deletions

View File

@@ -14,4 +14,11 @@ function drop() {
$e = new Exception();
echo "-------\nDump trace: \n".$e->getTraceAsString()."\n";
exit();
}
function dump() {
foreach(func_get_args() as $arg) {
fwrite(STDERR, "DUMP: ".call_user_func("print_r", $arg, true)."\n");
}
}

View File

@@ -1,11 +1,25 @@
<?php
namespace Cytro;
use Cytro, Cytro\TestCase;
use Symfony\Component\Process\Exception\LogicException;
class ExtendsTemplateTest extends TestCase {
public static function templates(array $vars) {
public function _testSandbox() {
$this->cytro = Cytro::factory(CYTRO_RESOURCES.'/provider', CYTRO_RESOURCES.'/compile');
try {
print_r($this->cytro->getTemplate('use/child.tpl')->getBody());
} catch (\Exception $e) {
echo "$e";
}
exit;
}
/**
* Templates skeletons
* @param array $vars
* @return array
*/
public static function templates(array $vars) {
return array(
array(
"name" => "level.0.tpl",
@@ -22,6 +36,7 @@ class ExtendsTemplateTest extends TestCase {
array(
"name" => "level.1.tpl",
"level" => 1,
"use" => false,
"blocks" => array(
"b1" => "from level 1"
),
@@ -33,6 +48,7 @@ class ExtendsTemplateTest extends TestCase {
array(
"name" => "level.2.tpl",
"level" => 2,
"use" => false,
"blocks" => array(
"b2" => "from level 2",
"b4" => "unused block"
@@ -45,6 +61,7 @@ class ExtendsTemplateTest extends TestCase {
array(
"name" => "level.3.tpl",
"level" => 3,
"use" => false,
"blocks" => array(
"b1" => "from level 3",
"b2" => "also from level 3"
@@ -57,10 +74,19 @@ class ExtendsTemplateTest extends TestCase {
);
}
public static function generate($block_mask, $extend_mask, array $vars) {
/**
* Generate templates by skeletons
*
* @param $block_mask
* @param $extend_mask
* @param array $skels
* @return array
*/
public static function generate($block_mask, $extend_mask, $skels) {
$t = array();
foreach(self::templates($vars) as $level => $tpl) {
foreach($skels as $level => $tpl) {
$src = 'level#'.$level.' ';
foreach($tpl["blocks"] as $bname => &$bcode) {
$src .= sprintf($block_mask, $bname, $bname.': '.$bcode)." level#$level ";
}
@@ -75,10 +101,8 @@ class ExtendsTemplateTest extends TestCase {
}
return $t;
}
/**
* @group static-extend
*/
public function testTemplateExtends() {
public function _testTemplateExtends() {
$vars = array(
"b1" => "b1",
"b2" => "b2",
@@ -87,14 +111,15 @@ class ExtendsTemplateTest extends TestCase {
"level" => "level",
"default" => 5
);
$tpls = self::generate('{block "%s"}%s{/block}', '{extends "level.%d.tpl"}', $vars);
$tpls = self::generate('{block "%s"}%s{/block}', '{extends "level.%d.tpl"}', self::templates($vars));
foreach($tpls as $name => $tpl) {
$this->tpl($name, $tpl["src"]);
$this->assertSame($this->cytro->fetch($name, $vars), $tpl["dst"]);
}
return;
$vars["default"]++;
$this->cytro->flush();
$tpls = self::generate('{block "{$%s}"}%s{/block}', '{extends "level.%d.tpl"}', $vars);
$tpls = self::generate('{block "{$%s}"}%s{/block}', '{extends "level.%d.tpl"}', self::templates($vars));
arsort($tpls);
foreach($tpls as $name => $tpl) {
$this->tpl("d.".$name, $tpl["src"]);
@@ -102,12 +127,24 @@ class ExtendsTemplateTest extends TestCase {
}
$vars["default"]++;
$this->cytro->flush();
$tpls = self::generate('{block "%s"}%s{/block}', '{extends "$level.%d.tpl"}', $vars);
$tpls = self::generate('{block "%s"}%s{/block}', '{extends "$level.%d.tpl"}', self::templates($vars));
arsort($tpls);
foreach($tpls as $name => $tpl) {
$this->tpl("x.".$name, $tpl["src"]);
$this->assertSame($this->cytro->fetch("x.".$name, $vars), $tpl["dst"]);
}
}
/**
* @group use
*/
public function testUse() {
$this->cytro = Cytro::factory(CYTRO_RESOURCES.'/provider', CYTRO_RESOURCES.'/compile');
$this->assertSame("<html>\n block 1 blocks \n block 2 child \n</html>", $this->cytro->fetch('use/child.tpl'));
}
public function _testParent() {
}
}

View File

@@ -0,0 +1,2 @@
{block 'b1'} block 1 blocks {/block}
{block 'b2'} block 2 blocks {/block}

View File

@@ -0,0 +1,3 @@
{extends 'use/parent.tpl'}
{use 'use/blocks.tpl'}
{block 'b2'} block 2 child {/block}

View File

@@ -0,0 +1,4 @@
<html>
{block 'b1'} block 1 parent {/block}
{block 'b2'} block 2 parent {/block}
</html>