Skip to content

Commit 1b419cb

Browse files
Move duplicated code to base class
1 parent 4b5abd8 commit 1b419cb

File tree

4 files changed

+105
-50
lines changed

4 files changed

+105
-50
lines changed

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

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Twig\Translation;
1313

1414
use Symfony\Component\Finder\Finder;
15+
use Symfony\Component\Translation\Extractor\AbstractFileExtractor;
1516
use Symfony\Component\Translation\Extractor\ExtractorInterface;
1617
use Symfony\Component\Translation\MessageCatalogue;
1718

@@ -21,7 +22,7 @@
2122
* @author Michel Salib <michelsalib@hotmail.com>
2223
* @author Fabien Potencier <fabien@symfony.com>
2324
*/
24-
class TwigExtractor implements ExtractorInterface
25+
class TwigExtractor extends AbstractFileExtractor implements ExtractorInterface
2526
{
2627
/**
2728
* Default domain for found messages.
@@ -44,6 +45,11 @@ class TwigExtractor implements ExtractorInterface
4445
*/
4546
private $twig;
4647

48+
/**
49+
* @var string
50+
*/
51+
private static $extension = 'twig';
52+
4753
public function __construct(\Twig_Environment $twig)
4854
{
4955
$this->twig = $twig;
@@ -54,19 +60,7 @@ public function __construct(\Twig_Environment $twig)
5460
*/
5561
public function extract($resource, MessageCatalogue $catalogue)
5662
{
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-
}
63+
$files = $this->extractFiles($resource);
7064
foreach ($files as $file) {
7165
try {
7266
$this->extractTemplate(file_get_contents($file->getPathname()), $catalogue);
@@ -105,22 +99,20 @@ protected function extractTemplate($template, MessageCatalogue $catalogue)
10599
*
106100
* @return bool
107101
*/
108-
private function canBeExtracted($file)
102+
protected function canBeExtracted($file)
109103
{
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);
104+
return $this->isFile($file) && static::$extension === pathinfo($file, PATHINFO_EXTENSION);
115105
}
116106

117107
/**
118-
* @param string $file
108+
* @param string|array $directory
119109
*
120-
* @return \SplFileInfo
110+
* @return array
121111
*/
122-
private function toSplFileInfo($file)
112+
protected function extractFromDirectory($directory)
123113
{
124-
return ($file instanceof \SplFileInfo) ? $file : new \SplFileInfo($file);
114+
$finder = new Finder();
115+
116+
return $finder->files()->name('*.'.static::$extension)->in($directory);
125117
}
126118
}

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

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Translation;
1313

1414
use Symfony\Component\Finder\Finder;
15+
use Symfony\Component\Translation\Extractor\AbstractFileExtractor;
1516
use Symfony\Component\Translation\MessageCatalogue;
1617
use Symfony\Component\Translation\Extractor\ExtractorInterface;
1718

@@ -20,7 +21,7 @@
2021
*
2122
* @author Michel Salib <michelsalib@hotmail.com>
2223
*/
23-
class PhpExtractor implements ExtractorInterface
24+
class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
2425
{
2526
const MESSAGE_TOKEN = 300;
2627

@@ -51,24 +52,17 @@ class PhpExtractor implements ExtractorInterface
5152
),
5253
);
5354

55+
/**
56+
* @var string
57+
*/
58+
private static $extension = 'php';
59+
5460
/**
5561
* {@inheritdoc}
5662
*/
5763
public function extract($resource, MessageCatalogue $catalog)
5864
{
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-
}
65+
$files = $this->extractFiles($resource);
7266
foreach ($files as $file) {
7367
$this->parseTokens(token_get_all(file_get_contents($file)), $catalog);
7468
}
@@ -188,24 +182,24 @@ protected function parseTokens($tokens, MessageCatalogue $catalog)
188182
/**
189183
* @param string $file
190184
*
185+
* @throws \InvalidArgumentException
186+
*
191187
* @return bool
192188
*/
193-
private function canBeExtracted($file)
189+
protected function canBeExtracted($file)
194190
{
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);
191+
return $this->isFile($file) && static::$extension === pathinfo($file, PATHINFO_EXTENSION);
200192
}
201193

202194
/**
203-
* @param string $file
195+
* @param string|array $directory
204196
*
205-
* @return \SplFileInfo
197+
* @return array
206198
*/
207-
private function toSplFileInfo($file)
199+
protected function extractFromDirectory($directory)
208200
{
209-
return ($file instanceof \SplFileInfo) ? $file : new \SplFileInfo($file);
201+
$finder = new Finder();
202+
203+
return $finder->files()->name('*.'.static::$extension)->in($directory);
210204
}
211205
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Symfony\Component\Translation\Extractor;
4+
5+
abstract class AbstractFileExtractor
6+
{
7+
/**
8+
* @param string|array $resource files, a file or a directory
9+
*
10+
* @return array
11+
*/
12+
protected function extractFiles($resource)
13+
{
14+
if (is_array($resource) || $resource instanceof \Traversable) {
15+
$files = array();
16+
foreach ($resource as $file) {
17+
if ($this->canBeExtracted($file)) {
18+
$files[] = $this->toSplFileInfo($file);
19+
}
20+
}
21+
} elseif (is_file($resource)) {
22+
$files = $this->canBeExtracted($resource) ? array($this->toSplFileInfo($resource)) : array();
23+
} else {
24+
$files = $this->extractFromDirectory($resource);
25+
}
26+
27+
return $files;
28+
}
29+
30+
/**
31+
* @param string $file
32+
*
33+
* @return \SplFileInfo
34+
*/
35+
private function toSplFileInfo($file)
36+
{
37+
return ($file instanceof \SplFileInfo) ? $file : new \SplFileInfo($file);
38+
}
39+
40+
/**
41+
* @param string $file
42+
*
43+
* @throws \InvalidArgumentException
44+
*
45+
* @return bool
46+
*/
47+
protected function isFile($file)
48+
{
49+
if (!is_file($file)) {
50+
throw new \InvalidArgumentException(sprintf('The "%s" file does not exist.', $file));
51+
}
52+
53+
return true;
54+
}
55+
56+
/**
57+
* @param string $file
58+
*
59+
* @return bool
60+
*/
61+
abstract protected function canBeExtracted($file);
62+
63+
/**
64+
* @param string|array $resource files, a file or a directory
65+
*
66+
* @return array files to be extracted
67+
*/
68+
abstract protected function extractFromDirectory($resource);
69+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ interface ExtractorInterface
2424
/**
2525
* Extracts translation messages from files, a file or a directory to the catalogue.
2626
*
27-
* @param string|array|\Traversable $resource files, a file or a directory
28-
* @param MessageCatalogue $catalogue The catalogue
27+
* @param string|array $resource files, a file or a directory
28+
* @param MessageCatalogue $catalogue The catalogue
2929
*/
3030
public function extract($resource, MessageCatalogue $catalogue);
3131

0 commit comments

Comments
 (0)