1: <?php
  2: 
  3:   4:   5:   6:   7:   8:   9:  10: 
 11: 
 12: namespace Symfony\Component\Config;
 13: 
 14:  15:  16:  17:  18: 
 19: class FileLocator implements FileLocatorInterface
 20: {
 21:     protected $paths;
 22: 
 23:      24:  25:  26:  27: 
 28:     public function __construct($paths = array())
 29:     {
 30:         $this->paths = (array) $paths;
 31:     }
 32: 
 33:      34:  35:  36:  37:  38:  39:  40:  41:  42:  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:  80:  81:  82:  83:  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:             || null !== parse_url($file, PHP_URL_SCHEME)
 93:         ) {
 94:             return true;
 95:         }
 96: 
 97:         return false;
 98:     }
 99: }
100: