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;
15 * Base class used by classes that extract translation messages from files.
17 * @author Marcos D. Sánchez <marcosdsanchez@gmail.com>
19 abstract class AbstractFileExtractor
22 * @param string|array $resource files, a file or a directory
26 protected function extractFiles($resource)
28 if (is_array($resource) || $resource instanceof \Traversable) {
30 foreach ($resource as $file) {
31 if ($this->canBeExtracted($file)) {
32 $files[] = $this->toSplFileInfo($file);
35 } elseif (is_file($resource)) {
36 $files = $this->canBeExtracted($resource) ? array($this->toSplFileInfo($resource)) : array();
38 $files = $this->extractFromDirectory($resource);
47 * @return \SplFileInfo
49 private function toSplFileInfo($file)
51 return ($file instanceof \SplFileInfo) ? $file : new \SplFileInfo($file);
59 * @throws \InvalidArgumentException
61 protected function isFile($file)
63 if (!is_file($file)) {
64 throw new \InvalidArgumentException(sprintf('The "%s" file does not exist.', $file));
75 abstract protected function canBeExtracted($file);
78 * @param string|array $resource files, a file or a directory
80 * @return array files to be extracted
82 abstract protected function extractFromDirectory($resource);