-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Translation][Extractor] Allow extracting an array of files besides e…
…xtracting a directory
- Loading branch information
1 parent
fd95ee2
commit 2f19ca5
Showing
2 changed files
with
87 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation\Extractor; | ||
|
||
/** | ||
* Base class used by classes that extract translation messages from files. | ||
* | ||
* @author Marcos D. Sánchez <marcosdsanchez@gmail.com> | ||
*/ | ||
abstract class AbstractFileExtractor | ||
{ | ||
/** | ||
* @param string|array $resource files, a file or a directory | ||
* | ||
* @return array | ||
*/ | ||
protected function extractFiles($resource) | ||
{ | ||
if (is_array($resource) || $resource instanceof \Traversable) { | ||
$files = array(); | ||
foreach ($resource as $file) { | ||
if ($this->canBeExtracted($file)) { | ||
$files[] = $this->toSplFileInfo($file); | ||
} | ||
} | ||
} elseif (is_file($resource)) { | ||
$files = $this->canBeExtracted($resource) ? array($this->toSplFileInfo($resource)) : array(); | ||
} else { | ||
$files = $this->extractFromDirectory($resource); | ||
} | ||
|
||
return $files; | ||
} | ||
|
||
/** | ||
* @param string $file | ||
* | ||
* @return \SplFileInfo | ||
*/ | ||
private function toSplFileInfo($file) | ||
{ | ||
return ($file instanceof \SplFileInfo) ? $file : new \SplFileInfo($file); | ||
} | ||
|
||
/** | ||
* @param string $file | ||
* | ||
* @throws \InvalidArgumentException | ||
* | ||
* @return bool | ||
*/ | ||
protected function isFile($file) | ||
{ | ||
if (!is_file($file)) { | ||
throw new \InvalidArgumentException(sprintf('The "%s" file does not exist.', $file)); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param string $file | ||
* | ||
* @return bool | ||
*/ | ||
abstract protected function canBeExtracted($file); | ||
|
||
/** | ||
* @param string|array $resource files, a file or a directory | ||
* | ||
* @return array files to be extracted | ||
*/ | ||
abstract protected function extractFromDirectory($resource); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters