Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Worked on Template loader and Compiler

Template Loader (File) works, the mock up seems pretty good
  • Loading branch information...
commit 41b031f3882008c19216d66ae733de7d3c38b910 1 parent 9816b01
@crodas authored
View
5 lib/Haanga2/Compiler/Dumper.php
@@ -95,6 +95,11 @@ public function getBuffer()
return $this->buffer;
}
+ public function method($name)
+ {
+ return $this->writeLn('public static function ' . $name . '(\Haanga2\Haanga2 $__self, $__context)');
+ }
+
public function writeLn($line)
{
$this->write($line . "\n");
View
3  lib/Haanga2/Compiler/Parser/Term/Variable.php
@@ -80,6 +80,7 @@ public function isObject()
public function addPart($part, $default)
{
+ $part->isLocal();
$this->parts[] = func_get_args();
}
@@ -88,7 +89,7 @@ public function toString(Dumper $vm)
if ($this->local || $vm->isLocalVariable($this)) {
$variable = '$' . $this->name;
} else {
- $variable = '$context["' . $this->name . '"]';
+ $variable = '$__context["' . $this->name . '"]';
}
$treatDot = $this->dot;
View
2  lib/Haanga2/Extension/Tags/Block.php
@@ -54,5 +54,5 @@ function Block(Dumper $vm, Array $args, Array $body)
$name = $args[0]->getName();
$name = 'block_' . (!preg_match('/^[a-z][0-9_a-z]*$/', $name) ? sha1($name) : $name);
$vm->registerSubmodule($name, $body);
- $vm->writeLn('self::' . $name . '($context);');
+ $vm->writeLn('self::' . $name . '($__self, $__context);');
}
View
39 lib/Haanga2/Haanga2.php
@@ -53,7 +53,7 @@ public function __construct(Loader $loader)
$this->Extension->addDirectory(__DIR__ . '/Extension');
}
- public function compile($source, $context = array())
+ public function compile($source, $id)
{
$tokens = $this->Tokenizer->tokenize($source);
$parser = new Parser;
@@ -63,11 +63,11 @@ public function compile($source, $context = array())
$parser->doParse(0, 0);
$opt = new Compiler\Optimizer;
$vm = new Compiler\Dumper($opt, $this->Extension);
- $vm->writeLn('class template')
+ $vm->writeLn('class haanga2_' . $id)
->writeLn('{')
->indent();
- $vm->writeLn('public static function main($context)')
+ $vm->method('main')
->writeLn('{')
->indent()
->evaluate($parser->body)
@@ -76,7 +76,7 @@ public function compile($source, $context = array())
foreach ($vm->getSubModules() as $name => $body) {
$vm->writeLn('')
- ->writeLn('public static function ' . $name . '($context)')
+ ->method($name)
->writeLn('{')
->indent()
->evaluate($body)
@@ -86,17 +86,38 @@ public function compile($source, $context = array())
$vm->dedent()
->writeLn('}');
- echo $vm->getBuffer();
+ $code = $vm->getBuffer();
+
+ // evaluate code
+ eval('namespace { ' . $code . ' }');
+
+ return $code;
+ }
+
+ public function templateIdToClass($name)
+ {
+ return 'haanga2_' . $name;
}
public function load($tpl, $vars = array(), $return = false)
{
- $callback = $this->loader->load($tpl);
- if ($callback && is_callable($callback)) {
- return $callback($tpl, $vars, $return);
+ $tplId = $this->loader->getTplId($tpl);
+ $class = $this->templateIdToClass($tplId);
+ if (class_exists($class, false)) {
+ return $class::main($this, $vars);
+ }
+
+ if ($this->loader->load($class, $tpl)) {
+ return $class::main($this, $vars);
}
/* compile, compile, compile! */
- $this->compile($this->loader->getContent($tpl), $vars);
+ $code = $this->compile($this->loader->getContent($tpl), $tplId);
+
+ /* save execute */
+ $class::main($this, $vars);
+
+ /* save! */
+ $this->loader->save($tpl, $code);
}
}
View
2  lib/Haanga2/Loader.php
@@ -38,7 +38,7 @@
interface Loader
{
- public function load($tpl);
+ public function load($class, $tpl);
public function getContent($tpl);
View
20 lib/Haanga2/Loader/File.php
@@ -84,6 +84,9 @@ protected function getTplPath($tpl)
public function getTplId($path)
{
+ if ($path[0] !== '/') {
+ $path = $this->getTplPath($path);
+ }
return sha1($path);
}
@@ -94,24 +97,19 @@ public function save($tpl, $code)
$compiled = $this->output . '/' . $callback . '.php';
// we do not care if this fails
- Artifex::save($compiled, $code);
+ Artifex::save($compiled, "<?php\n" . $code);
}
- public function load($tpl)
+ public function load($class, $tpl)
{
- $path = $this->getTplPath($tpl);
- $callback = 'haanga2_' . $this->getTplId($path);
- if (is_callable($callback)) {
- return $callback;
- }
-
- $compiled = $this->output . '/' . $callback . '.php';
+ $path = $this->getTplPath($tpl);
+ $compiled = $this->output . '/' . $class . '.php';
if (is_file($compiled) && filemtime($compiled) > filemtime($path)) {
/* load the compiled php only if the compiled
version is newer than our tpl file */
require $compiled;
- if (is_callable($callback)) {
- return $callback;
+ if (class_exists($class, false)) {
+ return true;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.