Skip to content

Commit

Permalink
Fixes #74: Added support for static vars and methods access on namesp…
Browse files Browse the repository at this point in the history
…aced classes
  • Loading branch information
Seldaek committed Jul 18, 2010
1 parent 68c813a commit e7734ba
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -15,6 +15,7 @@
+ Added {return} plugin that allows any included template to return
variables into the one that included it, or to the main controller
code via $dwoo->getReturnValues()
+ Added support for static vars and methods access on namespaced classes
+ Moved Dwoo code to Dwoo_Core that is extended by Dwoo, so you can use
the Dwoo directory as an svn:externals without problems now and just
use Dwoo_Core in place of Dwoo in your code
Expand Down
6 changes: 3 additions & 3 deletions lib/Dwoo/Compiler.php
Expand Up @@ -1360,7 +1360,7 @@ protected function parse($in, $from, $to, $parsingParams = false, $curBlock='',
} elseif (($first==='"' || $first==="'") && !(is_array($parsingParams) && preg_match('#^([\'"])[a-z0-9_]+\1\s*=>?(?:\s+|[^=])#i', $substr))) {
// string
$out = $this->parseString($in, $from, $to, $parsingParams, $curBlock, $pointer);
} elseif (preg_match('/^[a-z_][a-z0-9_]*(?:::[a-z_][a-z0-9_]*)?('.(is_array($parsingParams)||$curBlock!='root'?'':'\s+[^(]|').'\s*\(|\s*'.$this->rdr.'|\s*;)/i', $substr)) {
} elseif (preg_match('/^\\\\?[a-z_](?:\\\\?[a-z0-9_]+)*(?:::[a-z_][a-z0-9_]*)?('.(is_array($parsingParams)||$curBlock!='root'?'':'\s+[^(]|').'\s*\(|\s*'.$this->rdr.'|\s*;)/i', $substr)) {
// func
$out = $this->parseFunction($in, $from, $to, $parsingParams, $curBlock, $pointer);
$parsed = 'func';
Expand Down Expand Up @@ -1412,7 +1412,7 @@ protected function parse($in, $from, $to, $parsingParams = false, $curBlock='',

$parsingParams[] = $output;
return $parsingParams;
} elseif (preg_match('#^([a-z0-9_]+::\$[a-z0-9_]+)#i', $substr, $match)) {
} elseif (preg_match('#^(\\\\?[a-z_](?:\\\\?[a-z0-9_]+)*::\$[a-z0-9_]+)#i', $substr, $match)) {
// static member access
$parsed = 'var';
if (is_array($parsingParams)) {
Expand Down Expand Up @@ -1573,7 +1573,7 @@ protected function parse($in, $from, $to, $parsingParams = false, $curBlock='',
protected function parseFunction($in, $from, $to, $parsingParams = false, $curBlock='', &$pointer = null)
{
$cmdstr = substr($in, $from, $to-$from);
preg_match('/^([a-z_][a-z0-9_]*(?:::[a-z_][a-z0-9_]*)?)(\s*'.$this->rdr.'|\s*;)?/i', $cmdstr, $match);
preg_match('/^(\\\\?[a-z_](?:\\\\?[a-z0-9_]+)*(?:::[a-z_][a-z0-9_]*)?)(\s*'.$this->rdr.'|\s*;)?/i', $cmdstr, $match);

if (empty($match[1])) {
throw new Dwoo_Compilation_Exception($this, 'Parse error, invalid function name : '.substr($cmdstr, 0, 15));
Expand Down
29 changes: 25 additions & 4 deletions tests/CompilerTests.php
Expand Up @@ -711,14 +711,35 @@ public function testParsingOfMethodWithFollowingArgs()
$tpl->forceCompilation();
$this->assertEquals('testtest', $this->dwoo->get($tpl, array('obj'=>new PluginHelper()), $this->compiler));
}

public function testFunctionCanStartWithUnderscore()
{
$tpl = new Dwoo_Template_String('{_underscoreHelper("test", _underscoreHelper("bar", 10))|_underscoreModifierHelper}');
$tpl->forceCompilation();
$this->assertEquals('_--10bar-test-_', $this->dwoo->get($tpl, array(), $this->compiler));
}


public function testNamespaceStaticMethodAccess()
{
if (version_compare(phpversion(), '5.3.0', '<')) {
$this->markTestSkipped();
}
include_once __DIR__.'/resources/namespace.php';
$tpl = new Dwoo_Template_String('{\Dwoo\TestHelper::execute(foo)}');
$tpl->forceCompilation();
$this->assertEquals('foo', $this->dwoo->get($tpl, array()));
}

public function testNamespaceStaticVarAccess()
{
if (version_compare(phpversion(), '5.3.0', '<')) {
$this->markTestSkipped();
}
include_once __DIR__.'/resources/namespace.php';
$tpl = new Dwoo_Template_String('{\Dwoo\TestHelper::$var}');
$tpl->forceCompilation();
$this->assertEquals('foo', $this->dwoo->get($tpl, array()));
}
}

function excessArgsHelper($a) {
Expand Down Expand Up @@ -837,11 +858,11 @@ public function __toString() { return 'obj'; }
public static function staticFoo($bar, $baz) {
return "-$baz$bar-";
}

public function _foo($bar, $baz) {
return "-$baz$bar-";
}

public function _fooChain() {
return $this;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/resources/namespace.php
@@ -0,0 +1,11 @@
<?php

namespace Dwoo;

class TestHelper {
public static $var = 'foo';

public static function execute($arg) {
return $arg;
}
}

0 comments on commit e7734ba

Please sign in to comment.