PhpExtractor.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. const MESSAGE_TOKEN = 300;
  21. const METHOD_ARGUMENTS_TOKEN = 1000;
  22. const DOMAIN_TOKEN = 1001;
  23. /**
  24. * Prefix for new found message.
  25. *
  26. * @var string
  27. */
  28. private $prefix = '';
  29. /**
  30. * The sequence that captures translation messages.
  31. *
  32. * @var array
  33. */
  34. protected $sequences = [
  35. [
  36. '->',
  37. 'trans',
  38. '(',
  39. self::MESSAGE_TOKEN,
  40. ',',
  41. self::METHOD_ARGUMENTS_TOKEN,
  42. ',',
  43. self::DOMAIN_TOKEN,
  44. ],
  45. [
  46. '->',
  47. 'trans',
  48. '(',
  49. self::MESSAGE_TOKEN,
  50. ],
  51. ];
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function extract($resource, MessageCatalogue $catalog)
  56. {
  57. $files = $this->extractFiles($resource);
  58. foreach ($files as $file) {
  59. $this->parseTokens(token_get_all(file_get_contents($file)), $catalog, $file);
  60. gc_mem_caches();
  61. }
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function setPrefix(string $prefix)
  67. {
  68. $this->prefix = $prefix;
  69. }
  70. /**
  71. * Normalizes a token.
  72. *
  73. * @param mixed $token
  74. *
  75. * @return string|null
  76. */
  77. protected function normalizeToken($token)
  78. {
  79. if (isset($token[1]) && 'b"' !== $token) {
  80. return $token[1];
  81. }
  82. return $token;
  83. }
  84. /**
  85. * Seeks to a non-whitespace token.
  86. */
  87. private function seekToNextRelevantToken(\Iterator $tokenIterator)
  88. {
  89. for (; $tokenIterator->valid(); $tokenIterator->next()) {
  90. $t = $tokenIterator->current();
  91. if (T_WHITESPACE !== $t[0]) {
  92. break;
  93. }
  94. }
  95. }
  96. private function skipMethodArgument(\Iterator $tokenIterator)
  97. {
  98. $openBraces = 0;
  99. for (; $tokenIterator->valid(); $tokenIterator->next()) {
  100. $t = $tokenIterator->current();
  101. if ('[' === $t[0] || '(' === $t[0]) {
  102. ++$openBraces;
  103. }
  104. if (']' === $t[0] || ')' === $t[0]) {
  105. --$openBraces;
  106. }
  107. if ((0 === $openBraces && ',' === $t[0]) || (-1 === $openBraces && ')' === $t[0])) {
  108. break;
  109. }
  110. }
  111. }
  112. /**
  113. * Extracts the message from the iterator while the tokens
  114. * match allowed message tokens.
  115. */
  116. private function getValue(\Iterator $tokenIterator)
  117. {
  118. $message = '';
  119. $docToken = '';
  120. $docPart = '';
  121. for (; $tokenIterator->valid(); $tokenIterator->next()) {
  122. $t = $tokenIterator->current();
  123. if ('.' === $t) {
  124. // Concatenate with next token
  125. continue;
  126. }
  127. if (!isset($t[1])) {
  128. break;
  129. }
  130. switch ($t[0]) {
  131. case T_START_HEREDOC:
  132. $docToken = $t[1];
  133. break;
  134. case T_ENCAPSED_AND_WHITESPACE:
  135. case T_CONSTANT_ENCAPSED_STRING:
  136. if ('' === $docToken) {
  137. $message .= PhpStringTokenParser::parse($t[1]);
  138. } else {
  139. $docPart = $t[1];
  140. }
  141. break;
  142. case T_END_HEREDOC:
  143. $message .= PhpStringTokenParser::parseDocString($docToken, $docPart);
  144. $docToken = '';
  145. $docPart = '';
  146. break;
  147. case T_WHITESPACE:
  148. break;
  149. default:
  150. break 2;
  151. }
  152. }
  153. return $message;
  154. }
  155. /**
  156. * Extracts trans message from PHP tokens.
  157. */
  158. protected function parseTokens(array $tokens, MessageCatalogue $catalog, string $filename)
  159. {
  160. $tokenIterator = new \ArrayIterator($tokens);
  161. for ($key = 0; $key < $tokenIterator->count(); ++$key) {
  162. foreach ($this->sequences as $sequence) {
  163. $message = '';
  164. $domain = 'messages';
  165. $tokenIterator->seek($key);
  166. foreach ($sequence as $sequenceKey => $item) {
  167. $this->seekToNextRelevantToken($tokenIterator);
  168. if ($this->normalizeToken($tokenIterator->current()) === $item) {
  169. $tokenIterator->next();
  170. continue;
  171. } elseif (self::MESSAGE_TOKEN === $item) {
  172. $message = $this->getValue($tokenIterator);
  173. if (\count($sequence) === ($sequenceKey + 1)) {
  174. break;
  175. }
  176. } elseif (self::METHOD_ARGUMENTS_TOKEN === $item) {
  177. $this->skipMethodArgument($tokenIterator);
  178. } elseif (self::DOMAIN_TOKEN === $item) {
  179. $domainToken = $this->getValue($tokenIterator);
  180. if ('' !== $domainToken) {
  181. $domain = $domainToken;
  182. }
  183. break;
  184. } else {
  185. break;
  186. }
  187. }
  188. if ($message) {
  189. $catalog->set($message, $this->prefix.$message, $domain);
  190. $metadata = $catalog->getMetadata($message, $domain) ?? [];
  191. $normalizedFilename = preg_replace('{[\\\\/]+}', '/', $filename);
  192. $metadata['sources'][] = $normalizedFilename.':'.$tokens[$key][2];
  193. $catalog->setMetadata($message, $metadata, $domain);
  194. break;
  195. }
  196. }
  197. }
  198. }
  199. /**
  200. * @return bool
  201. *
  202. * @throws \InvalidArgumentException
  203. */
  204. protected function canBeExtracted(string $file)
  205. {
  206. return $this->isFile($file) && 'php' === pathinfo($file, PATHINFO_EXTENSION);
  207. }
  208. /**
  209. * {@inheritdoc}
  210. */
  211. protected function extractFromDirectory($directory)
  212. {
  213. $finder = new Finder();
  214. return $finder->files()->name('*.php')->in($directory);
  215. }
  216. }