mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
FIX Support self::class text collection
This commit is contained in:
parent
07a0f75426
commit
b7ea05900e
@ -558,6 +558,7 @@ class i18nTextCollector
|
|||||||
$inNamespace = false;
|
$inNamespace = false;
|
||||||
$inClass = false; // after `class` but before `{`
|
$inClass = false; // after `class` but before `{`
|
||||||
$inArrayClosedBy = false; // Set to the expected closing token, or false if not in array
|
$inArrayClosedBy = false; // Set to the expected closing token, or false if not in array
|
||||||
|
$inSelf = false; // Tracks progress of collecting self::class
|
||||||
$currentEntity = array();
|
$currentEntity = array();
|
||||||
$currentClass = []; // Class components
|
$currentClass = []; // Class components
|
||||||
$previousToken = null;
|
$previousToken = null;
|
||||||
@ -584,11 +585,21 @@ class i18nTextCollector
|
|||||||
if ($id === T_CLASS) {
|
if ($id === T_CLASS) {
|
||||||
// Skip if previous token was '::'. E.g. 'Object::class'
|
// Skip if previous token was '::'. E.g. 'Object::class'
|
||||||
if (is_array($previousToken) && $previousToken[0] === T_DOUBLE_COLON) {
|
if (is_array($previousToken) && $previousToken[0] === T_DOUBLE_COLON) {
|
||||||
|
if ($inSelf) {
|
||||||
|
// Handle self::class by allowing logic further down
|
||||||
|
// for __CLASS__ to handle an array of class parts
|
||||||
|
$id = T_CLASS_C;
|
||||||
|
$inSelf = false;
|
||||||
|
} else {
|
||||||
|
// Don't handle other ::class definitions. We can't determine which
|
||||||
|
// class was invoked, so parent::class is not possible at this point.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
$inClass = true;
|
$inClass = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if ($inClass && $id === T_STRING) {
|
if ($inClass && $id === T_STRING) {
|
||||||
$currentClass[] = $text;
|
$currentClass[] = $text;
|
||||||
$inClass = false;
|
$inClass = false;
|
||||||
@ -614,7 +625,7 @@ class i18nTextCollector
|
|||||||
|
|
||||||
// If inside this translation, some elements might be unreachable
|
// If inside this translation, some elements might be unreachable
|
||||||
if (in_array($id, [T_VARIABLE, T_STATIC]) ||
|
if (in_array($id, [T_VARIABLE, T_STATIC]) ||
|
||||||
($id === T_STRING && in_array($text, ['self', 'static', 'parent']))
|
($id === T_STRING && in_array($text, ['static', 'parent']))
|
||||||
) {
|
) {
|
||||||
// Un-collectable strings such as _t(static::class.'.KEY').
|
// Un-collectable strings such as _t(static::class.'.KEY').
|
||||||
// Should be provided by i18nEntityProvider instead
|
// Should be provided by i18nEntityProvider instead
|
||||||
@ -625,6 +636,12 @@ class i18nTextCollector
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start collecting self::class declarations
|
||||||
|
if ($id === T_STRING && $text === 'self') {
|
||||||
|
$inSelf = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Check text
|
// Check text
|
||||||
if ($id == T_CONSTANT_ENCAPSED_STRING) {
|
if ($id == T_CONSTANT_ENCAPSED_STRING) {
|
||||||
// Fixed quoting escapes, and remove leading/trailing quotes
|
// Fixed quoting escapes, and remove leading/trailing quotes
|
||||||
@ -648,7 +665,7 @@ class i18nTextCollector
|
|||||||
throw new LogicException("Invalid string escape: " .$text);
|
throw new LogicException("Invalid string escape: " .$text);
|
||||||
}
|
}
|
||||||
} elseif ($id === T_CLASS_C) {
|
} elseif ($id === T_CLASS_C) {
|
||||||
// Evaluate __CLASS__ . '.KEY' concatenation
|
// Evaluate __CLASS__ . '.KEY' and self::class concatenation
|
||||||
$text = implode('\\', $currentClass);
|
$text = implode('\\', $currentClass);
|
||||||
} else {
|
} else {
|
||||||
continue;
|
continue;
|
||||||
|
@ -323,13 +323,21 @@ class MyClass extends Base implements SomeService {
|
|||||||
"Slash=\\\\, Quote=\\""
|
"Slash=\\\\, Quote=\\""
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
public function getMagicConstantStringFromSelf()
|
||||||
|
{
|
||||||
|
return _t(
|
||||||
|
self::class . '.SELF_CLASS',
|
||||||
|
'Self Class'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
PHP;
|
PHP;
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
[
|
[
|
||||||
'SilverStripe\\Framework\\Core\\MyClass.NEWLINES' => "New Lines",
|
'SilverStripe\\Framework\\Core\\MyClass.NEWLINES' => "New Lines",
|
||||||
'SilverStripe\\Framework\\MyClass.ANOTHER_STRING' => 'Slash=\\, Quote=\'',
|
'SilverStripe\\Framework\\MyClass.ANOTHER_STRING' => 'Slash=\\, Quote=\'',
|
||||||
'SilverStripe\\Framework\\MyClass.DOUBLE_STRING' => 'Slash=\\, Quote="'
|
'SilverStripe\\Framework\\MyClass.DOUBLE_STRING' => 'Slash=\\, Quote="',
|
||||||
|
'SilverStripe\\Framework\\Core\\MyClass.SELF_CLASS' => 'Self Class',
|
||||||
],
|
],
|
||||||
$c->collectFromCode($php, null, $mymodule)
|
$c->collectFromCode($php, null, $mymodule)
|
||||||
);
|
);
|
||||||
@ -434,7 +442,7 @@ PHP;
|
|||||||
|
|
||||||
$php = <<<PHP
|
$php = <<<PHP
|
||||||
_t(static::class.'.KEY1', 'Default');
|
_t(static::class.'.KEY1', 'Default');
|
||||||
_t(self::class.'.KEY2', 'Default');
|
_t(parent::class.'.KEY1', 'Default');
|
||||||
_t('Collectable.KEY4', 'Default');
|
_t('Collectable.KEY4', 'Default');
|
||||||
PHP;
|
PHP;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user