From 3602ce2db87c9186c6f2cefcda0fa35391dc4634 Mon Sep 17 00:00:00 2001 From: Simon Welsh Date: Mon, 5 May 2014 20:55:59 +1000 Subject: [PATCH] Adds to nextString() method to ConfigStaticManifest This is used to get the classname out of the tokens, rather than assuming that the class name is a single T_STRING. --- core/manifest/ConfigStaticManifest.php | 46 +++++++++++-- .../manifest/ConfigStaticManifestTest.php | 66 +++++++++++++++++++ 2 files changed, 106 insertions(+), 6 deletions(-) diff --git a/core/manifest/ConfigStaticManifest.php b/core/manifest/ConfigStaticManifest.php index 730e674cf..3d9841fad 100644 --- a/core/manifest/ConfigStaticManifest.php +++ b/core/manifest/ConfigStaticManifest.php @@ -186,7 +186,7 @@ class SS_ConfigStaticManifest_Parser { * Get the next token to process, incrementing the pointer * * @param bool $ignoreWhitespace - if true will skip any whitespace tokens & only return non-whitespace ones - * @return null | int - Either the next token or null if there isn't one + * @return null | mixed - Either the next token or null if there isn't one */ protected function next($ignoreWhitespace = true) { do { @@ -198,6 +198,40 @@ class SS_ConfigStaticManifest_Parser { return $next; } + /** + * Get the next set of tokens that form a string to process, + * incrementing the pointer + * + * @param bool $ignoreWhitespace - if true will skip any whitespace tokens + * & only return non-whitespace ones + * @return null|string - Either the next string or null if there isn't one + */ + protected function nextString($ignoreWhitespace = true) { + static $stop = array('{', '}', '(', ')', '[', ']'); + + $string = ''; + while ($this->pos < $this->length) { + $next = $this->tokens[$this->pos]; + if (is_string($next)) { + if (!in_array($next, $stop)) { + $string .= $next; + } else { + break; + } + } else if ($next[0] == T_STRING) { + $string .= $next[1]; + } else if ($next[0] != T_WHITESPACE || !$ignoreWhitespace) { + break; + } + $this->pos++; + } + if ($string === '') { + return null; + } else { + return $string; + } + } + /** * Parse the given file to find the static variables declared in it, along with their access & values */ @@ -208,12 +242,12 @@ class SS_ConfigStaticManifest_Parser { $type = is_array($token) ? $token[0] : $token; if($type == T_CLASS) { - $next = $this->next(); - if($next[0] != T_STRING) { + $next = $this->nextString(); + if($next === null) { user_error("Couldn\'t parse {$this->path} when building config static manifest", E_USER_ERROR); } - $class = $next[1]; + $class = $next; } else if($type == T_NAMESPACE) { $namespace = ''; @@ -227,11 +261,11 @@ class SS_ConfigStaticManifest_Parser { $next = $this->next(); } - if($next[0] != T_STRING) { + if(!is_string($next) && $next[0] != T_STRING) { user_error("Couldn\'t parse {$this->path} when building config static manifest", E_USER_ERROR); } - $namespace .= $next[1]; + $namespace .= is_string($next) ? $next : $next[1]; } } else if($type == '{' || $type == T_CURLY_OPEN || $type == T_DOLLAR_OPEN_CURLY_BRACES){ diff --git a/tests/core/manifest/ConfigStaticManifestTest.php b/tests/core/manifest/ConfigStaticManifestTest.php index 5ee09660e..3709bd384 100644 --- a/tests/core/manifest/ConfigStaticManifestTest.php +++ b/tests/core/manifest/ConfigStaticManifestTest.php @@ -200,4 +200,70 @@ DOC; $this->assertEquals($expectedValue, $statics['config\staticmanifest\NamespaceTest']['db']['value']); } + + public function testParsingMultyStringClass() { + static $tokens = array( + array(T_OPEN_TAG, "parse(); + + $statics = $parser->getStatics(); + + $expected = array( + 'test' => array( + 'access' => T_PRIVATE, + 'value' => array(3) + ) + ); + + $this->assertEquals($expected, $statics[':ss:test2']); + } +} + +class ConfigStaticManifestTest_Parser extends SS_ConfigStaticManifest_Parser implements TestOnly { + public function __construct($tokens) { + $this->path = __FILE__; + $this->tokens = $tokens; + $this->length = count($this->tokens); + $this->pos = 0; + } }