add( $path ); } } /** * @param string normalized absolute path * @return Loco_fs_Locations */ public function add( $path ){ foreach( $this->expand($path) as $path ){ // path must have trailing slash, otherwise "/plugins/foobar" would match "/plugins/foo/" $this[$path] = strlen($path); } return $this; } /** * Check if a given path begins with any of the registered ones * @param string absolute path * @return bool whether path matched */ public function check( $path ){ foreach( $this->expand($path) as $path ){ foreach( $this as $prefix => $length ){ if( $prefix === $path || substr($path,0,$length) === $prefix ){ return true; } } } return false; } /** * Match location and return the relative subpath. * Note that exact match is returned as "." indicating self * @param string * @return string | null */ public function rel( $path ){ foreach( $this->expand($path) as $path ){ foreach( $this as $prefix => $length ){ if( $prefix === $path ){ return '.'; } if( substr($path,0,$length) === $prefix ){ return untrailingslashit( substr($path,$length) ); } } } return null; } /** * @param string * @return string[] */ private function expand( $path ){ $path = Loco_fs_File::abs($path); if( ! $path ){ throw new InvalidArgumentException('Expected absolute path'); } $paths = array( trailingslashit($path) ); // add real path if differs $real = realpath($path); if( $real && $real !== $path ){ $paths[] = trailingslashit($real); } return $paths; } }