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