PhpExtractor.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. '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);
  78. // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
  79. gc_mem_caches();
  80. }
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function setPrefix($prefix)
  86. {
  87. $this->prefix = $prefix;
  88. }
  89. /**
  90. * Normalizes a token.
  91. *
  92. * @param mixed $token
  93. *
  94. * @return string
  95. */
  96. protected function normalizeToken($token)
  97. {
  98. if (isset($token[1]) && 'b"' !== $token) {
  99. return $token[1];
  100. }
  101. return $token;
  102. }
  103. /**
  104. * Seeks to a non-whitespace token.
  105. */
  106. private function seekToNextRelevantToken(\Iterator $tokenIterator)
  107. {
  108. for (; $tokenIterator->valid(); $tokenIterator->next()) {
  109. $t = $tokenIterator->current();
  110. if (T_WHITESPACE !== $t[0]) {
  111. break;
  112. }
  113. }
  114. }
  115. private function skipMethodArgument(\Iterator $tokenIterator)
  116. {
  117. $openBraces = 0;
  118. for (; $tokenIterator->valid(); $tokenIterator->next()) {
  119. $t = $tokenIterator->current();
  120. if ('[' === $t[0] || '(' === $t[0]) {
  121. ++$openBraces;
  122. }
  123. if (']' === $t[0] || ')' === $t[0]) {
  124. --$openBraces;
  125. }
  126. if ((0 === $openBraces && ',' === $t[0]) || (-1 === $openBraces && ')' === $t[0])) {
  127. break;
  128. }
  129. }
  130. }
  131. /**
  132. * Extracts the message from the iterator while the tokens
  133. * match allowed message tokens.
  134. */
  135. private function getValue(\Iterator $tokenIterator)
  136. {
  137. $message = '';
  138. $docToken = '';
  139. $docPart = '';
  140. for (; $tokenIterator->valid(); $tokenIterator->next()) {
  141. $t = $tokenIterator->current();
  142. if ('.' === $t) {
  143. // Concatenate with next token
  144. continue;
  145. }
  146. if (!isset($t[1])) {
  147. break;
  148. }
  149. switch ($t[0]) {
  150. case T_START_HEREDOC:
  151. $docToken = $t[1];
  152. break;
  153. case T_ENCAPSED_AND_WHITESPACE:
  154. case T_CONSTANT_ENCAPSED_STRING:
  155. if ('' === $docToken) {
  156. $message .= PhpStringTokenParser::parse($t[1]);
  157. } else {
  158. $docPart = $t[1];
  159. }
  160. break;
  161. case T_END_HEREDOC:
  162. $message .= PhpStringTokenParser::parseDocString($docToken, $docPart);
  163. $docToken = '';
  164. $docPart = '';
  165. break;
  166. case T_WHITESPACE:
  167. break;
  168. default:
  169. break 2;
  170. }
  171. }
  172. return $message;
  173. }
  174. /**
  175. * Extracts trans message from PHP tokens.
  176. *
  177. * @param array $tokens
  178. * @param MessageCatalogue $catalog
  179. */
  180. protected function parseTokens($tokens, MessageCatalogue $catalog)
  181. {
  182. $tokenIterator = new \ArrayIterator($tokens);
  183. for ($key = 0; $key < $tokenIterator->count(); ++$key) {
  184. foreach ($this->sequences as $sequence) {
  185. $message = '';
  186. $domain = 'messages';
  187. $tokenIterator->seek($key);
  188. foreach ($sequence as $sequenceKey => $item) {
  189. $this->seekToNextRelevantToken($tokenIterator);
  190. if ($this->normalizeToken($tokenIterator->current()) === $item) {
  191. $tokenIterator->next();
  192. continue;
  193. } elseif (self::MESSAGE_TOKEN === $item) {
  194. $message = $this->getValue($tokenIterator);
  195. if (\count($sequence) === ($sequenceKey + 1)) {
  196. break;
  197. }
  198. } elseif (self::METHOD_ARGUMENTS_TOKEN === $item) {
  199. $this->skipMethodArgument($tokenIterator);
  200. } elseif (self::DOMAIN_TOKEN === $item) {
  201. $domain = $this->getValue($tokenIterator);
  202. break;
  203. } else {
  204. break;
  205. }
  206. }
  207. if ($message) {
  208. $catalog->set($message, $this->prefix.$message, $domain);
  209. break;
  210. }
  211. }
  212. }
  213. }
  214. /**
  215. * @param string $file
  216. *
  217. * @return bool
  218. *
  219. * @throws \InvalidArgumentException
  220. */
  221. protected function canBeExtracted($file)
  222. {
  223. return $this->isFile($file) && 'php' === pathinfo($file, PATHINFO_EXTENSION);
  224. }
  225. /**
  226. * @param string|array $directory
  227. *
  228. * @return array
  229. */
  230. protected function extractFromDirectory($directory)
  231. {
  232. $finder = new Finder();
  233. return $finder->files()->name('*.php')->in($directory);
  234. }
  235. }