Merge pull request #10813 from creative-commoners/pulls/5.0/textcollecttraits

FIX Handle __TRAIT__ in i18nTextCollector
This commit is contained in:
Guy Sartorelli 2023-06-13 16:50:31 +12:00 committed by GitHub
commit 6c266ec6ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 5 deletions

View File

@ -625,14 +625,14 @@ class i18nTextCollector
}
}
// Check class
if ($id === T_CLASS) {
// Check class and trait
if ($id === T_CLASS || $id === T_TRAIT) {
// Skip if previous token was '::'. E.g. 'Object::class'
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;
// for __CLASS__/__TRAIT__ to handle an array of class parts
$id = $id === T_TRAIT ? T_TRAIT_C : T_CLASS_C;
$inSelf = false;
} elseif ($potentialClassName) {
$id = T_CONSTANT_ENCAPSED_STRING;
@ -722,7 +722,7 @@ class i18nTextCollector
} else {
throw new LogicException("Invalid string escape: " . $text);
}
} elseif ($id === T_CLASS_C) {
} elseif ($id === T_CLASS_C || $id === T_TRAIT_C) {
// Evaluate __CLASS__ . '.KEY' and self::class concatenation
$text = implode('\\', $currentClass);
} else {

View File

@ -407,6 +407,39 @@ PHP;
);
}
public function testCollectFromTrait()
{
$c = i18nTextCollector::create();
$mymodule = ModuleLoader::inst()->getManifest()->getModule('i18ntestmodule');
$php = <<<PHP
<?php
namespace SilverStripe\Framework\Core;
use SilverStripe\ORM\DataObject;
class MyTrait extends Base implements SomeService {
public function getNewLines(\$class) {
if (
!is_subclass_of(\$class, DataObject::class)
|| !Object::has_extension(\$class, \SilverStripe\Versioned\Versioned::class)
) {
return null;
}
return _t(
__TRAIT__.'.NEWLINES',
'New Lines'
);
}
}
PHP;
$this->assertEquals(
[
'SilverStripe\\Framework\\Core\\MyTrait.NEWLINES' => "New Lines",
],
$c->collectFromCode($php, null, $mymodule)
);
}
public function testNewlinesInEntityValues()
{
$c = i18nTextCollector::create();