PhpExtractor.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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\Bundle\FrameworkBundle\Translation;
  11. use Symfony\Component\Finder\Finder;
  12. use Symfony\Component\Translation\Extractor\AbstractFileExtractor;
  13. use Symfony\Component\Translation\Extractor\ExtractorInterface;
  14. use Symfony\Component\Translation\MessageCatalogue;
  15. /**
  16. * PhpExtractor extracts translation messages from a PHP template.
  17. *
  18. * @author Michel Salib <michelsalib@hotmail.com>
  19. */
  20. class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
  21. {
  22. const MESSAGE_TOKEN = 300;
  23. const METHOD_ARGUMENTS_TOKEN = 1000;
  24. const DOMAIN_TOKEN = 1001;
  25. /**
  26. * Prefix for new found message.
  27. */
  28. private $prefix = '';
  29. /**
  30. * The sequence that captures translation messages.
  31. */
  32. protected $sequences = array(
  33. array(
  34. '->',
  35. 'trans',
  36. '(',
  37. self::MESSAGE_TOKEN,
  38. ',',
  39. self::METHOD_ARGUMENTS_TOKEN,
  40. ',',
  41. self::DOMAIN_TOKEN,
  42. ),
  43. array(
  44. '->',
  45. 'transChoice',
  46. '(',
  47. self::MESSAGE_TOKEN,
  48. ',',
  49. self::METHOD_ARGUMENTS_TOKEN,
  50. ',',
  51. self::METHOD_ARGUMENTS_TOKEN,
  52. ',',
  53. self::DOMAIN_TOKEN,
  54. ),
  55. array(
  56. '->',
  57. 'trans',
  58. '(',
  59. self::MESSAGE_TOKEN,
  60. ),
  61. array(
  62. '->',
  63. 'transChoice',
  64. '(',
  65. self::MESSAGE_TOKEN,
  66. ),
  67. );
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function extract($resource, MessageCatalogue $catalog)
  72. {
  73. $files = $this->extractFiles($resource);
  74. foreach ($files as $file) {
  75. $this->parseTokens(token_get_all(file_get_contents($file)), $catalog);
  76. if (\PHP_VERSION_ID >= 70000) {
  77. // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
  78. gc_mem_caches();
  79. }
  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. for (; $tokenIterator->valid(); $tokenIterator->next()) {
  140. $t = $tokenIterator->current();
  141. if (!isset($t[1])) {
  142. break;
  143. }
  144. switch ($t[0]) {
  145. case T_START_HEREDOC:
  146. $docToken = $t[1];
  147. break;
  148. case T_ENCAPSED_AND_WHITESPACE:
  149. case T_CONSTANT_ENCAPSED_STRING:
  150. $message .= $t[1];
  151. break;
  152. case T_END_HEREDOC:
  153. return PhpStringTokenParser::parseDocString($docToken, $message);
  154. default:
  155. break 2;
  156. }
  157. }
  158. if ($message) {
  159. $message = PhpStringTokenParser::parse($message);
  160. }
  161. return $message;
  162. }
  163. /**
  164. * Extracts trans message from PHP tokens.
  165. *
  166. * @param array $tokens
  167. * @param MessageCatalogue $catalog
  168. */
  169. protected function parseTokens($tokens, MessageCatalogue $catalog)
  170. {
  171. $tokenIterator = new \ArrayIterator($tokens);
  172. for ($key = 0; $key < $tokenIterator->count(); ++$key) {
  173. foreach ($this->sequences as $sequence) {
  174. $message = '';
  175. $domain = 'messages';
  176. $tokenIterator->seek($key);
  177. foreach ($sequence as $sequenceKey => $item) {
  178. $this->seekToNextRelevantToken($tokenIterator);
  179. if ($this->normalizeToken($tokenIterator->current()) === $item) {
  180. $tokenIterator->next();
  181. continue;
  182. } elseif (self::MESSAGE_TOKEN === $item) {
  183. $message = $this->getValue($tokenIterator);
  184. if (\count($sequence) === ($sequenceKey + 1)) {
  185. break;
  186. }
  187. } elseif (self::METHOD_ARGUMENTS_TOKEN === $item) {
  188. $this->skipMethodArgument($tokenIterator);
  189. } elseif (self::DOMAIN_TOKEN === $item) {
  190. $domain = $this->getValue($tokenIterator);
  191. break;
  192. } else {
  193. break;
  194. }
  195. }
  196. if ($message) {
  197. $catalog->set($message, $this->prefix.$message, $domain);
  198. break;
  199. }
  200. }
  201. }
  202. }
  203. /**
  204. * @param string $file
  205. *
  206. * @return bool
  207. *
  208. * @throws \InvalidArgumentException
  209. */
  210. protected function canBeExtracted($file)
  211. {
  212. return $this->isFile($file) && 'php' === pathinfo($file, PATHINFO_EXTENSION);
  213. }
  214. /**
  215. * @param string|array $directory
  216. *
  217. * @return array
  218. */
  219. protected function extractFromDirectory($directory)
  220. {
  221. $finder = new Finder();
  222. return $finder->files()->name('*.php')->in($directory);
  223. }
  224. }