TwigExtractor.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\Bridge\Twig\Translation;
  11. use Symfony\Component\Finder\Finder;
  12. use Symfony\Component\Finder\SplFileInfo;
  13. use Symfony\Component\Translation\Extractor\AbstractFileExtractor;
  14. use Symfony\Component\Translation\Extractor\ExtractorInterface;
  15. use Symfony\Component\Translation\MessageCatalogue;
  16. use Twig\Environment;
  17. use Twig\Error\Error;
  18. use Twig\Source;
  19. /**
  20. * TwigExtractor extracts translation messages from a twig template.
  21. *
  22. * @author Michel Salib <michelsalib@hotmail.com>
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. */
  25. class TwigExtractor extends AbstractFileExtractor implements ExtractorInterface
  26. {
  27. /**
  28. * Default domain for found messages.
  29. *
  30. * @var string
  31. */
  32. private $defaultDomain = 'messages';
  33. /**
  34. * Prefix for found message.
  35. *
  36. * @var string
  37. */
  38. private $prefix = '';
  39. /**
  40. * The twig environment.
  41. *
  42. * @var Environment
  43. */
  44. private $twig;
  45. public function __construct(Environment $twig)
  46. {
  47. $this->twig = $twig;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function extract($resource, MessageCatalogue $catalogue)
  53. {
  54. $files = $this->extractFiles($resource);
  55. foreach ($files as $file) {
  56. try {
  57. $this->extractTemplate(file_get_contents($file->getPathname()), $catalogue);
  58. } catch (Error $e) {
  59. if ($file instanceof \SplFileInfo) {
  60. $path = $file->getRealPath() ?: $file->getPathname();
  61. $name = $file instanceof SplFileInfo ? $file->getRelativePathname() : $path;
  62. if (method_exists($e, 'setSourceContext')) {
  63. $e->setSourceContext(new Source('', $name, $path));
  64. } else {
  65. $e->setTemplateName($name);
  66. }
  67. }
  68. throw $e;
  69. }
  70. }
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function setPrefix($prefix)
  76. {
  77. $this->prefix = $prefix;
  78. }
  79. protected function extractTemplate($template, MessageCatalogue $catalogue)
  80. {
  81. $visitor = $this->twig->getExtension('Symfony\Bridge\Twig\Extension\TranslationExtension')->getTranslationNodeVisitor();
  82. $visitor->enable();
  83. $this->twig->parse($this->twig->tokenize(new Source($template, '')));
  84. foreach ($visitor->getMessages() as $message) {
  85. $catalogue->set(trim($message[0]), $this->prefix.trim($message[0]), $message[1] ?: $this->defaultDomain);
  86. }
  87. $visitor->disable();
  88. }
  89. /**
  90. * @param string $file
  91. *
  92. * @return bool
  93. */
  94. protected function canBeExtracted($file)
  95. {
  96. return $this->isFile($file) && 'twig' === pathinfo($file, PATHINFO_EXTENSION);
  97. }
  98. /**
  99. * @param string|array $directory
  100. *
  101. * @return array
  102. */
  103. protected function extractFromDirectory($directory)
  104. {
  105. $finder = new Finder();
  106. return $finder->files()->name('*.twig')->in($directory);
  107. }
  108. }