Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / config / FileLocator.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\Config;
13
14 use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
15
16 /**
17  * FileLocator uses an array of pre-defined paths to find files.
18  *
19  * @author Fabien Potencier <fabien@symfony.com>
20  */
21 class FileLocator implements FileLocatorInterface
22 {
23     protected $paths;
24
25     /**
26      * Constructor.
27      *
28      * @param string|array $paths A path or an array of paths where to look for resources
29      */
30     public function __construct($paths = array())
31     {
32         $this->paths = (array) $paths;
33     }
34
35     /**
36      * {@inheritdoc}
37      */
38     public function locate($name, $currentPath = null, $first = true)
39     {
40         if ('' == $name) {
41             throw new \InvalidArgumentException('An empty file name is not valid to be located.');
42         }
43
44         if ($this->isAbsolutePath($name)) {
45             if (!file_exists($name)) {
46                 throw new FileLocatorFileNotFoundException(sprintf('The file "%s" does not exist.', $name));
47             }
48
49             return $name;
50         }
51
52         $paths = $this->paths;
53
54         if (null !== $currentPath) {
55             array_unshift($paths, $currentPath);
56         }
57
58         $paths = array_unique($paths);
59         $filepaths = array();
60
61         foreach ($paths as $path) {
62             if (@file_exists($file = $path.DIRECTORY_SEPARATOR.$name)) {
63                 if (true === $first) {
64                     return $file;
65                 }
66                 $filepaths[] = $file;
67             }
68         }
69
70         if (!$filepaths) {
71             throw new FileLocatorFileNotFoundException(sprintf('The file "%s" does not exist (in: %s).', $name, implode(', ', $paths)));
72         }
73
74         return $filepaths;
75     }
76
77     /**
78      * Returns whether the file path is an absolute path.
79      *
80      * @param string $file A file path
81      *
82      * @return bool
83      */
84     private function isAbsolutePath($file)
85     {
86         if ($file[0] === '/' || $file[0] === '\\'
87             || (strlen($file) > 3 && ctype_alpha($file[0])
88                 && $file[1] === ':'
89                 && ($file[2] === '\\' || $file[2] === '/')
90             )
91             || null !== parse_url($file, PHP_URL_SCHEME)
92         ) {
93             return true;
94         }
95
96         return false;
97     }
98 }