composer package updates
[openemr.git] / vendor / symfony / translation / Extractor / AbstractFileExtractor.php
blob4b6b155df60a63a95f433476dc68d6f4d044bab1
1 <?php
3 /*
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;
16 /**
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
23 /**
24 * @param string|array $resource files, a file or a directory
26 * @return array
28 protected function extractFiles($resource)
30 if (is_array($resource) || $resource instanceof \Traversable) {
31 $files = array();
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();
39 } else {
40 $files = $this->extractFromDirectory($resource);
43 return $files;
46 /**
47 * @param string $file
49 * @return \SplFileInfo
51 private function toSplFileInfo($file)
53 return ($file instanceof \SplFileInfo) ? $file : new \SplFileInfo($file);
56 /**
57 * @param string $file
59 * @return bool
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));
69 return true;
72 /**
73 * @param string $file
75 * @return bool
77 abstract protected function canBeExtracted($file);
79 /**
80 * @param string|array $resource files, a file or a directory
82 * @return array files to be extracted
84 abstract protected function extractFromDirectory($resource);