PhpExtractor.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. * @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. 'transChoice',
  48. '(',
  49. self::MESSAGE_TOKEN,
  50. ',',
  51. self::METHOD_ARGUMENTS_TOKEN,
  52. ',',
  53. self::METHOD_ARGUMENTS_TOKEN,
  54. ',',
  55. self::DOMAIN_TOKEN,
  56. ],
  57. [
  58. '->',
  59. 'trans',
  60. '(',
  61. self::MESSAGE_TOKEN,
  62. ],
  63. [
  64. '->',
  65. 'transChoice',
  66. '(',
  67. self::MESSAGE_TOKEN,
  68. ],
  69. ];
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function extract($resource, MessageCatalogue $catalog)
  74. {
  75. $files = $this->extractFiles($resource);
  76. foreach ($files as $file) {
  77. $this->parseTokens(token_get_all(file_get_contents($file)), $catalog, $file);
  78. gc_mem_caches();
  79. }
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function setPrefix($prefix)
  85. {
  86. $this->prefix = $prefix;
  87. }
  88. /**
  89. * Normalizes a token.
  90. *
  91. * @param mixed $token
  92. *
  93. * @return string|null
  94. */
  95. protected function normalizeToken($token)
  96. {
  97. if (isset($token[1]) && 'b"' !== $token) {
  98. return $token[1];
  99. }
  100. return $token;
  101. }
  102. /**
  103. * Seeks to a non-whitespace token.
  104. */
  105. private function seekToNextRelevantToken(\Iterator $tokenIterator)
  106. {
  107. for (; $tokenIterator->valid(); $tokenIterator->next()) {
  108. $t = $tokenIterator->current();
  109. if (\T_WHITESPACE !== $t[0]) {
  110. break;
  111. }
  112. }
  113. }
  114. private function skipMethodArgument(\Iterator $tokenIterator)
  115. {
  116. $openBraces = 0;
  117. for (; $tokenIterator->valid(); $tokenIterator->next()) {
  118. $t = $tokenIterator->current();
  119. if ('[' === $t[0] || '(' === $t[0]) {
  120. ++$openBraces;
  121. }
  122. if (']' === $t[0] || ')' === $t[0]) {
  123. --$openBraces;
  124. }
  125. if ((0 === $openBraces && ',' === $t[0]) || (-1 === $openBraces && ')' === $t[0])) {
  126. break;
  127. }
  128. }
  129. }
  130. /**
  131. * Extracts the message from the iterator while the tokens
  132. * match allowed message tokens.
  133. */
  134. private function getValue(\Iterator $tokenIterator)
  135. {
  136. $message = '';
  137. $docToken = '';
  138. $docPart = '';
  139. for (; $tokenIterator->valid(); $tokenIterator->next()) {
  140. $t = $tokenIterator->current();
  141. if ('.' === $t) {
  142. // Concatenate with next token
  143. continue;
  144. }
  145. if (!isset($t[1])) {
  146. break;
  147. }
  148. switch ($t[0]) {
  149. case \T_START_HEREDOC:
  150. $docToken = $t[1];
  151. break;
  152. case \T_ENCAPSED_AND_WHITESPACE:
  153. case \T_CONSTANT_ENCAPSED_STRING:
  154. if ('' === $docToken) {
  155. $message .= PhpStringTokenParser::parse($t[1]);
  156. } else {
  157. $docPart = $t[1];
  158. }
  159. break;
  160. case \T_END_HEREDOC:
  161. if ($indentation = strspn($t[1], ' ')) {
  162. $docPartWithLineBreaks = $docPart;
  163. $docPart = '';
  164. foreach (preg_split('~(\r\n|\n|\r)~', $docPartWithLineBreaks, -1, \PREG_SPLIT_DELIM_CAPTURE) as $str) {
  165. if (\in_array($str, ["\r\n", "\n", "\r"], true)) {
  166. $docPart .= $str;
  167. } else {
  168. $docPart .= substr($str, $indentation);
  169. }
  170. }
  171. }
  172. $message .= PhpStringTokenParser::parseDocString($docToken, $docPart);
  173. $docToken = '';
  174. $docPart = '';
  175. break;
  176. case \T_WHITESPACE:
  177. break;
  178. default:
  179. break 2;
  180. }
  181. }
  182. return $message;
  183. }
  184. /**
  185. * Extracts trans message from PHP tokens.
  186. *
  187. * @param array $tokens
  188. * @param string $filename
  189. */
  190. protected function parseTokens($tokens, MessageCatalogue $catalog/* , string $filename */)
  191. {
  192. if (\func_num_args() < 3 && __CLASS__ !== static::class && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName() && !$this instanceof \PHPUnit\Framework\MockObject\MockObject && !$this instanceof \Prophecy\Prophecy\ProphecySubjectInterface && !$this instanceof \Mockery\MockInterface) {
  193. @trigger_error(sprintf('The "%s()" method will have a new "string $filename" argument in version 5.0, not defining it is deprecated since Symfony 4.3.', __METHOD__), \E_USER_DEPRECATED);
  194. }
  195. $filename = 2 < \func_num_args() ? func_get_arg(2) : '';
  196. $tokenIterator = new \ArrayIterator($tokens);
  197. for ($key = 0; $key < $tokenIterator->count(); ++$key) {
  198. foreach ($this->sequences as $sequence) {
  199. $message = '';
  200. $domain = 'messages';
  201. $tokenIterator->seek($key);
  202. foreach ($sequence as $sequenceKey => $item) {
  203. $this->seekToNextRelevantToken($tokenIterator);
  204. if ($this->normalizeToken($tokenIterator->current()) === $item) {
  205. $tokenIterator->next();
  206. continue;
  207. } elseif (self::MESSAGE_TOKEN === $item) {
  208. $message = $this->getValue($tokenIterator);
  209. if (\count($sequence) === ($sequenceKey + 1)) {
  210. break;
  211. }
  212. } elseif (self::METHOD_ARGUMENTS_TOKEN === $item) {
  213. $this->skipMethodArgument($tokenIterator);
  214. } elseif (self::DOMAIN_TOKEN === $item) {
  215. $domainToken = $this->getValue($tokenIterator);
  216. if ('' !== $domainToken) {
  217. $domain = $domainToken;
  218. }
  219. break;
  220. } else {
  221. break;
  222. }
  223. }
  224. if ($message) {
  225. $catalog->set($message, $this->prefix.$message, $domain);
  226. $metadata = $catalog->getMetadata($message, $domain) ?? [];
  227. $normalizedFilename = preg_replace('{[\\\\/]+}', '/', $filename);
  228. $metadata['sources'][] = $normalizedFilename.':'.$tokens[$key][2];
  229. $catalog->setMetadata($message, $metadata, $domain);
  230. break;
  231. }
  232. }
  233. }
  234. }
  235. /**
  236. * @param string $file
  237. *
  238. * @return bool
  239. *
  240. * @throws \InvalidArgumentException
  241. */
  242. protected function canBeExtracted($file)
  243. {
  244. return $this->isFile($file) && 'php' === pathinfo($file, \PATHINFO_EXTENSION);
  245. }
  246. /**
  247. * {@inheritdoc}
  248. */
  249. protected function extractFromDirectory($directory)
  250. {
  251. $finder = new Finder();
  252. return $finder->files()->name('*.php')->in($directory);
  253. }
  254. }