Debian Codegraph @

lib/3rdParty/composer/symfony/translation/Extractor/AbstractFileExtractor.php

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