8000 Give ability to extractors to extract from an array of files besides … · symfony/symfony@4b5abd8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b5abd8

Browse files
Give ability to extractors to extract from an array of files besides a directory.
The following commit adds the new feature to the following extractors: PhpExtractor and TwigExtractor. It also corrects the interface documentation to show that an array of files is also allowed as a parameter.
1 parent d1131db commit 4b5abd8

File tree

6 files changed

+151
-15
lines changed

6 files changed

+151
-15
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>{{ 'Hi!'|trans }}</h1>

src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,42 @@ public function testExtractSyntaxError()
8383
$extractor = new TwigExtractor($twig);
8484
$extractor->extract(__DIR__.'/../Fixtures', new MessageCatalogue('en'));
8585
}
86+
87+
/**
88+
* @dataProvider resourceProvider
89+
*/
90+
public function testExtractWithFiles($resource)
91+
{
92+
$loader = new \Twig_Loader_Array(array());
93+
$twig = new \Twig_Environment($loader, array(
94+
'strict_variables' => true,
95+
'debug' => true,
96+
'cache' => false,
97+
'autoescape' => false,
98+
));
99+
$twig->addExtension(new TranslationExtension($this->getMock('Symfony\Component\Translation\TranslatorInterface')));
100+
101+
$extractor = new TwigExtractor($twig);
102+
$catalogue = new MessageCatalogue('en');
103+
$extractor->extract($resource, $catalogue);
104+
105+
$this->assertTrue($catalogue->has('Hi!', 'messages'));
106+
$this->assertEquals('Hi!', $catalogue->get('Hi!', 'messages'));
107+
}
108+
109+
/**
110+
* @return array
111+
*/
112+
public function resourceProvider()
113+
{
114+
$directory = __DIR__.'/../Fixtures/extractor/';
115+
116+
return array(
117+
array($directory.'with_translations.html.twig'),
118+
array(array($directory.'with_translations.html.twig')),
119+
array(array(new \SplFileInfo($directory.'with_translations.html.twig'))),
120+
array(new \ArrayObject(array($directory.'with_translations.html.twig'))),
121+
array(new \ArrayObject(array(new \SplFileInfo($directory.'with_translations.html.twig')))),
122+
);
123+
}
86124
}

src/Symfony/Bridge/Twig/Translation/TwigExtractor.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,21 @@ public function __construct(\Twig_Environment $twig)
5252
/**
5353
* {@inheritdoc}
5454
*/
55-
public function extract($directory, MessageCatalogue $catalogue)
55+
public function extract($resource, MessageCatalogue $catalogue)
5656
{
57-
// load any existing translation files
58-
$finder = new Finder();
59-
$files = $finder->files()->name('*.twig')->sortByName()->in($directory);
57+
if (is_array($resource) || $resource instanceof \Traversable) {
58+
$files = array();
59+
foreach ($resource as $file) {
60+
if ($this->canBeExtracted($file)) {
61+
$files[] = $this->toSplFileInfo($file);
62+
}
63+
}
64+
} elseif (is_file($resource)) {
65+
$files = $this->canBeExtracted($resource) ? array($this->toSplFileInfo($resource)) : array();
66+
} else {
67+
$finder = new Finder();
68+
$files = $finder->files()->name('*.twig')->in($resource);
69+
}
6070
foreach ($files as $file) {
6171
try {
6272
$this->extractTemplate(file_get_contents($file->getPathname()), $catalogue);
@@ -89,4 +99,28 @@ protected function extractTemplate($template, MessageCatalogue $catalogue)
8999

90100
$visitor->disable();
91101
}
102+
103+
/**
104+
* @param string $file
105+
*
106+
* @return bool
107+
*/
108+
private function canBeExtracted($file)
109+
{
110+
if (!is_file($file)) {
111+
throw new \InvalidArgumentException(sprintf('The "%s" file does not exist.', $file));
112+
}
113+
114+
return 'twig' === pathinfo($file, PATHINFO_EXTENSION);
115+
}
116+
117+
/**
118+
* @param string $file
119+
*
120+
* @return \SplFileInfo
121+
*/
122+
private function toSplFileInfo($file)
123+
{
124+
return ($file instanceof \SplFileInfo) ? $file : new \SplFileInfo($file);
125+
}
92126
}

src/Symfony/Bundle/FrameworkBundle/Tests/Translation/PhpExtractorTest.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@
1717

1818
class PhpExtractorTest extends TestCase
1919
{
20-
public function testExtraction()
20+
/**
21+
* @dataProvider resourcesProvider
22+
*
23+
* @param array|string $resource
24+
*/
25+
public function testExtraction($resource)
2126
{
2227
// Arrange
2328
$extractor = new PhpExtractor();
2429
$extractor->setPrefix('prefix');
2530
$catalogue = new MessageCatalogue('en');
2631

2732
// Act
28-
$extractor->extract(__DIR__.'/../Fixtures/Resources/views/', $catalogue);
33+
$extractor->extract($resource, $catalogue);
2934

3035
$expectedHeredoc = <<<EOF
3136
heredoc key with whitespace and escaped \$\n sequences
@@ -50,4 +55,28 @@ public function testExtraction()
5055

5156
$this->assertEquals($expectedCatalogue, $actualCatalogue);
5257
}
58+
59+
public function resourcesProvider()
60+
{
61+
$directory = __DIR__.'/../Fixtures/Resources/views/';
62+
$splFiles = array();
63+
foreach (new \DirectoryIterator($directory) as $fileInfo) {
64+
if ($fileInfo->isDot()) {
65+
continue;
66+
}
67+
if ('translation.html.php' === $fileInfo->getBasename()) {
68+
$phpFile = $fileInfo->getPathname();
69+
}
70+
$splFiles[] = $fileInfo->getFileInfo();
71+
}
72+
73+
return array(
74+
array($directory),
75+
array($phpFile),
76+
array(glob($directory.'*')),
77+
array($splFiles),
78+
array(new \ArrayObject(glob($directory.'*'))),
79+
array(new \ArrayObject($splFiles)),
80+
);
81+
}
5382
}

src/Symfony/Bundle/FrameworkBundle/Translation/PhpExtractor.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,21 @@ class PhpExtractor implements ExtractorInterface
5454
/**
5555
* {@inheritdoc}
5656
*/
57-
public function extract($directory, MessageCatalogue $catalog)
57+
public function extract($resource, MessageCatalogue $catalog)
5858
{
59-
// load any existing translation files
60-
$finder = new Finder();
61-
$files = $finder->files()->name('*.php')->in($directory);
59+
if (is_array($resource) || $resource instanceof \Traversable) {
60+
$files = array();
61+
foreach ($resource as $file) {
62+
if ($this->canBeExtracted($file)) {
63+
$files[] = $this->toSplFileInfo($file);
64+
}
65+
}
66+
} elseif (is_file($resource)) {
67+
$files = $this->canBeExtracted($resource) ? array($this->toSplFileInfo($resource)) : array();
68+
} else {
69+
$finder = new Finder();
70+
$files = $finder->files()->name('*.php')->in($resource);
71+
}
6272
foreach ($files as $file) {
6373
$this->parseTokens(token_get_all(file_get_contents($file)), $catalog);
6474
}
@@ -174,4 +184,28 @@ protected function parseTokens($tokens, MessageCatalogue $catalog)
174184
}
175185
}
176186
}
187+
188+
/**
189+
* @param string $file
190+
*
191+
* @return bool
192+
*/
193+
private function canBeExtracted($file)
194+
{
195+
if (!is_file($file)) {
196+
throw new \InvalidArgumentException(sprintf('The "%s" file does not exist.', $file));
197+
}
198+
199+
return 'php' === pathinfo($file, PATHINFO_EXTENSION);
200+
}
201+
202+
/**
203+
* @param string $file
204+
*
205+
* @return \SplFileInfo
206+
*/
207+
private function toSplFileInfo($file)
208+
{
209+
return ($file instanceof \SplFileInfo) ? $file : new \SplFileInfo($file);
210+
}
177211
}

src/Symfony/Component/Translation/Extractor/ExtractorInterface.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
use Symfony\Component\Translation\MessageCatalogue;
1515

1616
/**
17-
* Extracts translation messages from a template directory to the catalogue.
17+
* Extracts translation messages from a directory or files to the catalogue.
1818
* New found messages are injected to the catalogue using the prefix.
1919
*
2020
* @author Michel Salib <michelsalib@hotmail.com>
2121
*/
2222
interface ExtractorInterface
2323
{
2424
/**
25-
* Extracts translation messages from a template directory to the catalogue.
25+
* Extracts translation messages from files, a file or a directory to the catalogue.
2626
*
27-
* @param string $directory The path to look into
28-
* @param MessageCatalogue $catalogue The catalogue
27+
* @param string|array|\Traversable $resource files, a file or a directory
28+
* @param MessageCatalogue $catalogue The catalogue
2929
*/
30-
public function extract($directory, MessageCatalogue $catalogue);
30+
public function extract($resource, MessageCatalogue $catalogue);
3131

3232
/**
3333
* Sets the prefix that should be used for new found messages.

0 commit comments

Comments
 (0)
0