AbstractFileExtractor.php
1 month ago
ChainExtractor.php
1 month ago
ExtractorInterface.php
1 month ago
PhpExtractor.php
1 month ago
PhpStringTokenParser.php
1 month ago
AbstractFileExtractor.php
61 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace IAWPSCOPED\Symfony\Component\Translation\Extractor; |
| 12 | |
| 13 | use IAWPSCOPED\Symfony\Component\Translation\Exception\InvalidArgumentException; |
| 14 | /** |
| 15 | * Base class used by classes that extract translation messages from files. |
| 16 | * |
| 17 | * @author Marcos D. Sánchez <marcosdsanchez@gmail.com> |
| 18 | * @internal |
| 19 | */ |
| 20 | abstract class AbstractFileExtractor |
| 21 | { |
| 22 | protected function extractFiles(string|iterable $resource) : iterable |
| 23 | { |
| 24 | if (\is_iterable($resource)) { |
| 25 | $files = []; |
| 26 | foreach ($resource as $file) { |
| 27 | if ($this->canBeExtracted($file)) { |
| 28 | $files[] = $this->toSplFileInfo($file); |
| 29 | } |
| 30 | } |
| 31 | } elseif (\is_file($resource)) { |
| 32 | $files = $this->canBeExtracted($resource) ? [$this->toSplFileInfo($resource)] : []; |
| 33 | } else { |
| 34 | $files = $this->extractFromDirectory($resource); |
| 35 | } |
| 36 | return $files; |
| 37 | } |
| 38 | private function toSplFileInfo(string $file) : \SplFileInfo |
| 39 | { |
| 40 | return new \SplFileInfo($file); |
| 41 | } |
| 42 | /** |
| 43 | * @throws InvalidArgumentException |
| 44 | */ |
| 45 | protected function isFile(string $file) : bool |
| 46 | { |
| 47 | if (!\is_file($file)) { |
| 48 | throw new InvalidArgumentException(\sprintf('The "%s" file does not exist.', $file)); |
| 49 | } |
| 50 | return \true; |
| 51 | } |
| 52 | /** |
| 53 | * @return bool |
| 54 | */ |
| 55 | protected abstract function canBeExtracted(string $file); |
| 56 | /** |
| 57 | * @return iterable |
| 58 | */ |
| 59 | protected abstract function extractFromDirectory(string|array $resource); |
| 60 | } |
| 61 |