Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / translation / Extractor / AbstractFileExtractor.php
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
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 <marcosdsanchez@gmail.com>
20  */
21 abstract class AbstractFileExtractor
22 {
23     /**
24      * @param string|array $resource files, a file or a directory
25      *
26      * @return array
27      */
28     protected function extractFiles($resource)
29     {
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);
35                 }
36             }
37         } elseif (is_file($resource)) {
38             $files = $this->canBeExtracted($resource) ? array($this->toSplFileInfo($resource)) : array();
39         } else {
40             $files = $this->extractFromDirectory($resource);
41         }
42
43         return $files;
44     }
45
46     /**
47      * @param string $file
48      *
49      * @return \SplFileInfo
50      */
51     private function toSplFileInfo($file)
52     {
53         return ($file instanceof \SplFileInfo) ? $file : new \SplFileInfo($file);
54     }
55
56     /**
57      * @param string $file
58      *
59      * @return bool
60      *
61      * @throws InvalidArgumentException
62      */
63     protected function isFile($file)
64     {
65         if (!is_file($file)) {
66             throw new InvalidArgumentException(sprintf('The "%s" file does not exist.', $file));
67         }
68
69         return true;
70     }
71
72     /**
73      * @param string $file
74      *
75      * @return bool
76      */
77     abstract protected function canBeExtracted($file);
78
79     /**
80      * @param string|array $resource files, a file or a directory
81      *
82      * @return array files to be extracted
83      */
84     abstract protected function extractFromDirectory($resource);
85 }