Merge pull request #1837 from simonwelsh/short-array-parse

FIX Handle PHP 5.4's short array notation everywhere arrays are parsed.
This commit is contained in:
Sean Harvey 2013-05-06 20:32:51 -07:00
commit 677122256e
8 changed files with 64 additions and 8 deletions

View File

@ -211,7 +211,12 @@ abstract class Object {
} }
} else { } else {
if($tName == ')') { if($tName == '[') {
// Add an empty array to the bucket
$bucket[] = array();
$bucketStack[] = &$bucket;
$bucket = &$bucket[sizeof($bucket)-1];
} elseif($tName == ')' || $tName == ']') {
// Pop-by-reference // Pop-by-reference
$bucket = &$bucketStack[sizeof($bucketStack)-1]; $bucket = &$bucketStack[sizeof($bucketStack)-1];
array_pop($bucketStack); array_pop($bucketStack);

View File

@ -282,10 +282,9 @@ class SS_ConfigStaticManifest_Parser {
$type = is_array($token) ? $token[0] : $token; $type = is_array($token) ? $token[0] : $token;
// Track array nesting depth // Track array nesting depth
if($type == T_ARRAY) { if($type == T_ARRAY || $type == '[') {
$depth += 1; $depth += 1;
} } elseif($type == ')' || $type == ']') {
else if($type == ')') {
$depth -= 1; $depth -= 1;
} }

View File

@ -252,7 +252,7 @@ class i18nTextCollector extends Object {
$inConcat = true; $inConcat = true;
} elseif($inTransFn && $token == ',') { } elseif($inTransFn && $token == ',') {
$inConcat = false; $inConcat = false;
} elseif($inTransFn && ($token == ')' || $finalTokenDueToArray)) { } elseif($inTransFn && ($token == ')' || $finalTokenDueToArray || $token == '[')) {
// finalize definition // finalize definition
$inTransFn = false; $inTransFn = false;
$inConcat = false; $inConcat = false;

View File

@ -395,6 +395,18 @@ class ObjectTest extends SapphireTest {
Object::parse_class_spec( Object::parse_class_spec(
"Enum(array('Accepted', 'Pending', 'Declined', array('UnsubmittedA','UnsubmittedB')), 'Unsubmitted')") "Enum(array('Accepted', 'Pending', 'Declined', array('UnsubmittedA','UnsubmittedB')), 'Unsubmitted')")
); );
// 5.4 Shorthand Array
$this->assertEquals(
array('Enum',array(array('Accepted', 'Pending', 'Declined', 'Unsubmitted'), 'Unsubmitted')),
Object::parse_class_spec("Enum(['Accepted', 'Pending', 'Declined', 'Unsubmitted'), 'Unsubmitted']")
);
// 5.4 Nested shorthand array
$this->assertEquals(
array('Enum',array(array('Accepted', 'Pending', 'Declined', array('UnsubmittedA','UnsubmittedB')),
'Unsubmitted')),
Object::parse_class_spec(
"Enum(['Accepted', 'Pending', 'Declined', ['UnsubmittedA','UnsubmittedB']], 'Unsubmitted')")
);
// Namespaced class // Namespaced class
$this->assertEquals( $this->assertEquals(
array('Test\MyClass', array()), array('Test\MyClass', array()),

View File

@ -163,4 +163,23 @@ DOC;
$statics = $this->parseSelf()->getStatics(); $statics = $this->parseSelf()->getStatics();
$this->assertNull(@$statics[__CLASS__]['static_method']); $this->assertNull(@$statics[__CLASS__]['static_method']);
} }
public function testParsingShortArray() {
if(version_compare(PHP_VERSION, '5.4', '<')) {
$this->markTestSkipped('This test requires PHP 5.4 or higher');
return;
}
$parser = new SS_ConfigStaticManifest_Parser(__DIR__ . '/ConfigStaticManifestTest/ConfigStaticManifestTestMyObject.php');
$parser->parse();
$statics = $parser->getStatics();
$expectedValue = array(
'Name' => 'Varchar',
'Description' => 'Text',
);
$this->assertEquals($expectedValue, $statics['ConfigStaticManifestTestMyObject']['db']['value']);
}
} }

View File

@ -0,0 +1,8 @@
<?php
class ConfigStaticManifestTestMyObject implements TestOnly {
static private $db = [
'Name' => 'Varchar',
'Description' => 'Text',
];
}

View File

@ -327,6 +327,14 @@ _t("i18nTestModule.INJECTIONS3", "Hello {name} {greeting}. But it is late, {good
"New context (this should be ignored)", "New context (this should be ignored)",
array("name"=>"Steffen", "greeting"=>"willkommen", "goodbye"=>"wiedersehen")); array("name"=>"Steffen", "greeting"=>"willkommen", "goodbye"=>"wiedersehen"));
_t('i18nTestModule.INJECTIONS4', array("name"=>"Cat", "greeting"=>"meow", "goodbye"=>"meow")); _t('i18nTestModule.INJECTIONS4', array("name"=>"Cat", "greeting"=>"meow", "goodbye"=>"meow"));
_t('i18nTestModule.INJECTIONS5','_DOES_NOT_EXIST', "Hello {name} {greeting}. But it is late, {goodbye}",
["name"=>"Mark", "greeting"=>"welcome", "goodbye"=>"bye"]);
_t('i18nTestModule.INJECTIONS6', "Hello {name} {greeting}. But it is late, {goodbye}",
["name"=>"Paul", "greeting"=>"good you are here", "goodbye"=>"see you"]);
_t("i18nTestModule.INJECTIONS7", "Hello {name} {greeting}. But it is late, {goodbye}",
"New context (this should be ignored)",
["name"=>"Steffen", "greeting"=>"willkommen", "goodbye"=>"wiedersehen"]);
_t('i18nTestModule.INJECTIONS8', ["name"=>"Cat", "greeting"=>"meow", "goodbye"=>"meow"]);
PHP; PHP;
$collectedTranslatables = $c->collectFromCode($php, 'mymodule'); $collectedTranslatables = $c->collectFromCode($php, 'mymodule');
@ -338,6 +346,11 @@ PHP;
'i18nTestModule.INJECTIONS2' => array("Hello {name} {greeting}. But it is late, {goodbye}"), 'i18nTestModule.INJECTIONS2' => array("Hello {name} {greeting}. But it is late, {goodbye}"),
'i18nTestModule.INJECTIONS3' => array("Hello {name} {greeting}. But it is late, {goodbye}", 'i18nTestModule.INJECTIONS3' => array("Hello {name} {greeting}. But it is late, {goodbye}",
"New context (this should be ignored)"), "New context (this should be ignored)"),
'i18nTestModule.INJECTIONS5' => array("_DOES_NOT_EXIST",
"Hello {name} {greeting}. But it is late, {goodbye}"),
'i18nTestModule.INJECTIONS6' => array("Hello {name} {greeting}. But it is late, {goodbye}"),
'i18nTestModule.INJECTIONS7' => array("Hello {name} {greeting}. But it is late, {goodbye}",
"New context (this should be ignored)"),
)); ));
ksort($expectedArray); ksort($expectedArray);