PhpExtractor.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Extractor;
  11. use Symfony\Component\Finder\Finder;
  12. use Symfony\Component\Translation\MessageCatalogue;
  13. /**
  14. * PhpExtractor extracts translation messages from a PHP template.
  15. *
  16. * @author Michel Salib <michelsalib@hotmail.com>
  17. */
  18. class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
  19. {
  20. public const MESSAGE_TOKEN = 300;
  21. public const METHOD_ARGUMENTS_TOKEN = 1000;
  22. public const DOMAIN_TOKEN = 1001;
  23. /**
  24. * Prefix for new found message.
  25. */
  26. private $prefix = '';
  27. /**
  28. * The sequence that captures translation messages.
  29. */
  30. protected $sequences = [
  31. [
  32. '->',
  33. 'trans',
  34. '(',
  35. self::MESSAGE_TOKEN,
  36. ',',
  37. self::METHOD_ARGUMENTS_TOKEN,
  38. ',',
  39. self::DOMAIN_TOKEN,
  40. ],
  41. [
  42. '->',
  43. 'trans',
  44. '(',
  45. self::MESSAGE_TOKEN,
  46. ],
  47. [
  48. 'new',
  49. 'TranslatableMessage',
  50. '(',
  51. self::MESSAGE_TOKEN,
  52. ',',
  53. self::METHOD_ARGUMENTS_TOKEN,
  54. ',',
  55. self::DOMAIN_TOKEN,
  56. ],
  57. [
  58. 'new',
  59. 'TranslatableMessage',
  60. '(',
  61. self::MESSAGE_TOKEN,
  62. ],
  63. [
  64. 'new',
  65. '\\',
  66. 'Symfony',
  67. '\\',
  68. 'Component',
  69. '\\',
  70. 'Translation',
  71. '\\',
  72. 'TranslatableMessage',
  73. '(',
  74. self::MESSAGE_TOKEN,
  75. ',',
  76. self::METHOD_ARGUMENTS_TOKEN,
  77. ',',
  78. self::DOMAIN_TOKEN,
  79. ],
  80. [
  81. 'new',
  82. '\Symfony\Component\Translation\TranslatableMessage',
  83. '(',
  84. self::MESSAGE_TOKEN,
  85. ',',
  86. self::METHOD_ARGUMENTS_TOKEN,
  87. ',',
  88. self::DOMAIN_TOKEN,
  89. ],
  90. [
  91. 'new',
  92. '\\',
  93. 'Symfony',
  94. '\\',
  95. 'Component',
  96. '\\',
  97. 'Translation',
  98. '\\',
  99. 'TranslatableMessage',
  100. '(',
  101. self::MESSAGE_TOKEN,
  102. ],
  103. [
  104. 'new',
  105. '\Symfony\Component\Translation\TranslatableMessage',
  106. '(',
  107. self::MESSAGE_TOKEN,
  108. ],
  109. [
  110. 't',
  111. '(',
  112. self::MESSAGE_TOKEN,
  113. ',',
  114. self::METHOD_ARGUMENTS_TOKEN,
  115. ',',
  116. self::DOMAIN_TOKEN,
  117. ],
  118. [
  119. 't',
  120. '(',
  121. self::MESSAGE_TOKEN,
  122. ],
  123. ];
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function extract($resource, MessageCatalogue $catalog)
  128. {
  129. $files = $this->extractFiles($resource);
  130. foreach ($files as $file) {
  131. $this->parseTokens(token_get_all(file_get_contents($file)), $catalog, $file);
  132. gc_mem_caches();
  133. }
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function setPrefix(string $prefix)
  139. {
  140. $this->prefix = $prefix;
  141. }
  142. /**
  143. * Normalizes a token.
  144. *
  145. * @param mixed $token
  146. *
  147. * @return string|null
  148. */
  149. protected function normalizeToken($token)
  150. {
  151. if (isset($token[1]) && 'b"' !== $token) {
  152. return $token[1];
  153. }
  154. return $token;
  155. }
  156. /**
  157. * Seeks to a non-whitespace token.
  158. */
  159. private function seekToNextRelevantToken(\Iterator $tokenIterator)
  160. {
  161. for (; $tokenIterator->valid(); $tokenIterator->next()) {
  162. $t = $tokenIterator->current();
  163. if (\T_WHITESPACE !== $t[0]) {
  164. break;
  165. }
  166. }
  167. }
  168. private function skipMethodArgument(\Iterator $tokenIterator)
  169. {
  170. $openBraces = 0;
  171. for (; $tokenIterator->valid(); $tokenIterator->next()) {
  172. $t = $tokenIterator->current();
  173. if ('[' === $t[0] || '(' === $t[0]) {
  174. ++$openBraces;
  175. }
  176. if (']' === $t[0] || ')' === $t[0]) {
  177. --$openBraces;
  178. }
  179. if ((0 === $openBraces && ',' === $t[0]) || (-1 === $openBraces && ')' === $t[0])) {
  180. break;
  181. }
  182. }
  183. }
  184. /**
  185. * Extracts the message from the iterator while the tokens
  186. * match allowed message tokens.
  187. */
  188. private function getValue(\Iterator $tokenIterator)
  189. {
  190. $message = '';
  191. $docToken = '';
  192. $docPart = '';
  193. for (; $tokenIterator->valid(); $tokenIterator->next()) {
  194. $t = $tokenIterator->current();
  195. if ('.' === $t) {
  196. // Concatenate with next token
  197. continue;
  198. }
  199. if (!isset($t[1])) {
  200. break;
  201. }
  202. switch ($t[0]) {
  203. case \T_START_HEREDOC:
  204. $docToken = $t[1];
  205. break;
  206. case \T_ENCAPSED_AND_WHITESPACE:
  207. case \T_CONSTANT_ENCAPSED_STRING:
  208. if ('' === $docToken) {
  209. $message .= PhpStringTokenParser::parse($t[1]);
  210. } else {
  211. $docPart = $t[1];
  212. }
  213. break;
  214. case \T_END_HEREDOC:
  215. if ($indentation = strspn($t[1], ' ')) {
  216. $docPartWithLineBreaks = $docPart;
  217. $docPart = '';
  218. foreach (preg_split('~(\r\n|\n|\r)~', $docPartWithLineBreaks, -1, \PREG_SPLIT_DELIM_CAPTURE) as $str) {
  219. if (\in_array($str, ["\r\n", "\n", "\r"], true)) {
  220. $docPart .= $str;
  221. } else {
  222. $docPart .= substr($str, $indentation);
  223. }
  224. }
  225. }
  226. $message .= PhpStringTokenParser::parseDocString($docToken, $docPart);
  227. $docToken = '';
  228. $docPart = '';
  229. break;
  230. case \T_WHITESPACE:
  231. break;
  232. default:
  233. break 2;
  234. }
  235. }
  236. return $message;
  237. }
  238. /**
  239. * Extracts trans message from PHP tokens.
  240. */
  241. protected function parseTokens(array $tokens, MessageCatalogue $catalog, string $filename)
  242. {
  243. $tokenIterator = new \ArrayIterator($tokens);
  244. for ($key = 0; $key < $tokenIterator->count(); ++$key) {
  245. foreach ($this->sequences as $sequence) {
  246. $message = '';
  247. $domain = 'messages';
  248. $tokenIterator->seek($key);
  249. foreach ($sequence as $sequenceKey => $item) {
  250. $this->seekToNextRelevantToken($tokenIterator);
  251. if ($this->normalizeToken($tokenIterator->current()) === $item) {
  252. $tokenIterator->next();
  253. continue;
  254. } elseif (self::MESSAGE_TOKEN === $item) {
  255. $message = $this->getValue($tokenIterator);
  256. if (\count($sequence) === ($sequenceKey + 1)) {
  257. break;
  258. }
  259. } elseif (self::METHOD_ARGUMENTS_TOKEN === $item) {
  260. $this->skipMethodArgument($tokenIterator);
  261. } elseif (self::DOMAIN_TOKEN === $item) {
  262. $domainToken = $this->getValue($tokenIterator);
  263. if ('' !== $domainToken) {
  264. $domain = $domainToken;
  265. }
  266. break;
  267. } else {
  268. break;
  269. }
  270. }
  271. if ($message) {
  272. $catalog->set($message, $this->prefix.$message, $domain);
  273. $metadata = $catalog->getMetadata($message, $domain) ?? [];
  274. $normalizedFilename = preg_replace('{[\\\\/]+}', '/', $filename);
  275. $metadata['sources'][] = $normalizedFilename.':'.$tokens[$key][2];
  276. $catalog->setMetadata($message, $metadata, $domain);
  277. break;
  278. }
  279. }
  280. }
  281. }
  282. /**
  283. * @return bool
  284. *
  285. * @throws \InvalidArgumentException
  286. */
  287. protected function canBeExtracted(string $file)
  288. {
  289. return $this->isFile($file) && 'php' === pathinfo($file, \PATHINFO_EXTENSION);
  290. }
  291. /**
  292. * {@inheritdoc}
  293. */
  294. protected function extractFromDirectory($directory)
  295. {
  296. if (!class_exists(Finder::class)) {
  297. throw new \LogicException(sprintf('You cannot use "%s" as the "symfony/finder" package is not installed. Try running "composer require symfony/finder".', static::class));
  298. }
  299. $finder = new Finder();
  300. return $finder->files()->name('*.php')->in($directory);
  301. }
  302. }