4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Extractor;
14 use Symfony\Component\Translation\Exception\InvalidArgumentException;
17 * Base class used by classes that extract translation messages from files.
19 * @author Marcos D. Sánchez <marcosdsanchez@gmail.com>
21 abstract class AbstractFileExtractor
24 * @param string|array $resource files, a file or a directory
28 protected function extractFiles($resource)
30 if (is_array($resource) || $resource instanceof \Traversable) {
32 foreach ($resource as $file) {
33 if ($this->canBeExtracted($file)) {
34 $files[] = $this->toSplFileInfo($file);
37 } elseif (is_file($resource)) {
38 $files = $this->canBeExtracted($resource) ? array($this->toSplFileInfo($resource)) : array();
40 $files = $this->extractFromDirectory($resource);
49 * @return \SplFileInfo
51 private function toSplFileInfo($file)
53 return ($file instanceof \SplFileInfo) ? $file : new \SplFileInfo($file);
61 * @throws InvalidArgumentException
63 protected function isFile($file)
65 if (!is_file($file)) {
66 throw new InvalidArgumentException(sprintf('The "%s" file does not exist.', $file));
77 abstract protected function canBeExtracted($file);
80 * @param string|array $resource files, a file or a directory
82 * @return array files to be extracted
84 abstract protected function extractFromDirectory($resource);