add( $path ); } } /** * @param string normalized absolute path * @return Loco_fs_Locations */ public function add( $path ){ $path = Loco_fs_File::abs($path); if( ! $path ){ throw new InvalidArgumentException('Location must be absolute path'); } // path must have trailing slash, otherwise "/plugins/foobar" would match "/plugins/foo/" $path = trailingslashit($path); $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 ){ $path = trailingslashit( Loco_fs_File::abs($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 * @return string | null */ public function rel( $path ){ $path = trailingslashit( Loco_fs_File::abs($path) ); foreach( $this as $prefix => $length ){ if( $prefix === $path ){ return '.'; } if( substr($path,0,$length) === $prefix ){ return untrailingslashit( substr($path,$length) ); } } } }