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\Config;
14 use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
17 * FileLocator uses an array of pre-defined paths to find files.
19 * @author Fabien Potencier <fabien@symfony.com>
21 class FileLocator implements FileLocatorInterface
26 * @param string|array $paths A path or an array of paths where to look for resources
28 public function __construct($paths = array())
30 $this->paths = (array) $paths;
36 public function locate($name, $currentPath = null, $first = true)
39 throw new \InvalidArgumentException('An empty file name is not valid to be located.');
42 if ($this->isAbsolutePath($name)) {
43 if (!file_exists($name)) {
44 throw new FileLocatorFileNotFoundException(sprintf('The file "%s" does not exist.', $name), 0, null, array($name));
50 $paths = $this->paths;
52 if (null !== $currentPath) {
53 array_unshift($paths, $currentPath);
56 $paths = array_unique($paths);
57 $filepaths = $notfound = array();
59 foreach ($paths as $path) {
60 if (@file_exists($file = $path.\DIRECTORY_SEPARATOR.$name)) {
61 if (true === $first) {
71 throw new FileLocatorFileNotFoundException(sprintf('The file "%s" does not exist (in: %s).', $name, implode(', ', $paths)), 0, null, $notfound);
78 * Returns whether the file path is an absolute path.
80 * @param string $file A file path
84 private function isAbsolutePath($file)
86 if ('/' === $file[0] || '\\' === $file[0]
87 || (\strlen($file) > 3 && ctype_alpha($file[0])
89 && ('\\' === $file[2] || '/' === $file[2])
91 || null !== parse_url($file, PHP_URL_SCHEME)