vendor/symfony/config/FileLocator.php line 33

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Config;
  11. use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
  12. /**
  13.  * FileLocator uses an array of pre-defined paths to find files.
  14.  *
  15.  * @author Fabien Potencier <fabien@symfony.com>
  16.  */
  17. class FileLocator implements FileLocatorInterface
  18. {
  19.     protected $paths;
  20.     /**
  21.      * @param string|string[] $paths A path or an array of paths where to look for resources
  22.      */
  23.     public function __construct(string|array $paths = [])
  24.     {
  25.         $this->paths = (array) $paths;
  26.     }
  27.     public function locate(string $namestring $currentPath nullbool $first true)
  28.     {
  29.         if ('' === $name) {
  30.             throw new \InvalidArgumentException('An empty file name is not valid to be located.');
  31.         }
  32.         if ($this->isAbsolutePath($name)) {
  33.             if (!file_exists($name)) {
  34.                 throw new FileLocatorFileNotFoundException(sprintf('The file "%s" does not exist.'$name), 0null, [$name]);
  35.             }
  36.             return $name;
  37.         }
  38.         $paths $this->paths;
  39.         if (null !== $currentPath) {
  40.             array_unshift($paths$currentPath);
  41.         }
  42.         $paths array_unique($paths);
  43.         $filepaths $notfound = [];
  44.         foreach ($paths as $path) {
  45.             if (@file_exists($file $path.\DIRECTORY_SEPARATOR.$name)) {
  46.                 if (true === $first) {
  47.                     return $file;
  48.                 }
  49.                 $filepaths[] = $file;
  50.             } else {
  51.                 $notfound[] = $file;
  52.             }
  53.         }
  54.         if (!$filepaths) {
  55.             throw new FileLocatorFileNotFoundException(sprintf('The file "%s" does not exist (in: "%s").'$nameimplode('", "'$paths)), 0null$notfound);
  56.         }
  57.         return $filepaths;
  58.     }
  59.     /**
  60.      * Returns whether the file path is an absolute path.
  61.      */
  62.     private function isAbsolutePath(string $file): bool
  63.     {
  64.         if ('/' === $file[0] || '\\' === $file[0]
  65.             || (\strlen($file) > && ctype_alpha($file[0])
  66.                 && ':' === $file[1]
  67.                 && ('\\' === $file[2] || '/' === $file[2])
  68.             )
  69.             || null !== parse_url($file\PHP_URL_SCHEME)
  70.         ) {
  71.             return true;
  72.         }
  73.         return false;
  74.     }
  75. }