Skip to content

Commit 307fb3f

Browse files
author
Yuri Kovsher
committed
MAGETWO-33062: Update dictionary search tool to support \Magento\Framework\Phrase
1 parent 9e8434e commit 307fb3f

File tree

3 files changed

+99
-10
lines changed

3 files changed

+99
-10
lines changed

dev/tools/Magento/Tools/I18n/Parser/Adapter/Php/Tokenizer.php

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,42 @@ public function parse($filePath)
5050
$this->_tokensCount = count($this->_tokens);
5151
}
5252

53+
/**
54+
* Checks if the next set of tokens is a valid class identifier and it matches passed class name
55+
*
56+
* @param string $className
57+
* @return bool
58+
*/
59+
public function isMatchingClass($className)
60+
{
61+
/*
62+
* 1 - beginning, class identifier can begin either with identifier or with namespace separator
63+
* 2 - after namespace separator, identifier is expected
64+
* 3 - after identifier, namespace separator or open brace is expected
65+
* 4 - ending, tells that class identifier is valid
66+
*/
67+
$state = 1;
68+
$classString = '';
69+
while ($token = $this->getNextRealToken()) {
70+
if ($token->isNamespaceSeparator() && ($state != 2)) {
71+
$classString .= $token->getValue();
72+
$state = 2;
73+
} elseif ($token->isIdentifier() && ($state != 3)) {
74+
$classString .= $token->getValue();
75+
$state = 3;
76+
} elseif ($token->isOpenBrace() && $state == 3) {
77+
$state = 4;
78+
break;
79+
} else {
80+
return false;
81+
}
82+
}
83+
if ($state == 4 && $className == substr($classString, -strlen($className))) {
84+
return true;
85+
}
86+
return false;
87+
}
88+
5389
/**
5490
* Get arguments tokens of function
5591
*
@@ -139,13 +175,26 @@ public function getNextToken()
139175
}
140176

141177
/**
142-
* Check is it last token
178+
* Get next token skipping all whitespaces
179+
*
180+
* @return \Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\Token|false
181+
*/
182+
public function getNextRealToken()
183+
{
184+
do {
185+
$token = $this->getNextToken();
186+
} while ($token && $token->isWhitespace());
187+
return $token;
188+
}
189+
190+
/**
191+
* Check if it is end of loop
143192
*
144193
* @return bool
145194
*/
146-
public function isLastToken()
195+
public function isEndOfLoop()
147196
{
148-
return 0 == $this->_tokensCount || key($this->_tokens) + 1 == $this->_tokensCount;
197+
return is_null(key($this->_tokens));
149198
}
150199

151200
/**

dev/tools/Magento/Tools/I18n/Parser/Adapter/Php/Tokenizer/PhraseCollector.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function parse($file)
6262
$this->_phrases = [];
6363
$this->_file = $file;
6464
$this->_tokenizer->parse($file);
65-
while (!$this->_tokenizer->isLastToken()) {
65+
while (!$this->_tokenizer->isEndOfLoop()) {
6666
$this->_extractPhrases();
6767
}
6868
}
@@ -74,12 +74,22 @@ public function parse($file)
7474
*/
7575
protected function _extractPhrases()
7676
{
77-
$phraseStartToken = $this->_tokenizer->getNextToken();
78-
if ($phraseStartToken->isEqualFunction('__') && $this->_tokenizer->getNextToken()->isOpenBrace()) {
79-
$arguments = $this->_tokenizer->getFunctionArgumentsTokens();
80-
$phrase = $this->_collectPhrase(array_shift($arguments));
81-
if (null !== $phrase) {
82-
$this->_addPhrase($phrase, count($arguments), $this->_file, $phraseStartToken->getLine());
77+
if ($firstToken = $this->_tokenizer->getNextRealToken()) {
78+
if ($firstToken->isEqualFunction('__')) {
79+
$secondToken = $this->_tokenizer->getNextRealToken();
80+
if ($secondToken && $secondToken->isOpenBrace()) {
81+
$arguments = $this->_tokenizer->getFunctionArgumentsTokens();
82+
$phrase = $this->_collectPhrase(array_shift($arguments));
83+
if (null !== $phrase) {
84+
$this->_addPhrase($phrase, count($arguments), $this->_file, $firstToken->getLine());
85+
}
86+
}
87+
} elseif ($firstToken->isNew() && $this->_tokenizer->isMatchingClass('Phrase')) {
88+
$arguments = $this->_tokenizer->getFunctionArgumentsTokens();
89+
$phrase = $this->_collectPhrase(array_shift($arguments));
90+
if (null !== $phrase) {
91+
$this->_addPhrase($phrase, count($arguments), $this->_file, $firstToken->getLine());
92+
}
8393
}
8494
}
8595
}

dev/tools/Magento/Tools/I18n/Parser/Adapter/Php/Tokenizer/Token.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ public function getLine()
7575
return $this->_line;
7676
}
7777

78+
/**
79+
* Is "new" operator
80+
*
81+
* @return bool
82+
*/
83+
public function isNew()
84+
{
85+
return $this->getName() == T_NEW;
86+
}
87+
7888
/**
7989
* Whenever token is equal function
8090
*
@@ -155,4 +165,24 @@ public function isSemicolon()
155165
{
156166
return $this->getValue() == ';';
157167
}
168+
169+
/**
170+
* Is namespace separator
171+
*
172+
* @return bool
173+
*/
174+
public function isNamespaceSeparator()
175+
{
176+
return $this->getName() == T_NS_SEPARATOR;
177+
}
178+
179+
/**
180+
* Is identifier
181+
*
182+
* @return bool
183+
*/
184+
public function isIdentifier()
185+
{
186+
return $this->getName() == T_STRING;
187+
}
158188
}

0 commit comments

Comments
 (0)