Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.67% |
29 / 30 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| Less_FileManager | |
96.67% |
29 / 30 |
|
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 1 |
| getFilePath | |
96.67% |
29 / 30 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | /** |
| 5 | * @private |
| 6 | */ |
| 7 | class Less_FileManager { |
| 8 | /** |
| 9 | * Get the full absolute path and uri of the import |
| 10 | * @see less-node/FileManager.getPath https://github.com/less/less.js/blob/v2.5.3/lib/less-node/file-manager.js#L70 |
| 11 | * @param string $filename |
| 12 | * @param null|array $currentFileInfo |
| 13 | * @return null|array{0:string,1:string} |
| 14 | */ |
| 15 | public static function getFilePath( $filename, $currentFileInfo ) { |
| 16 | if ( !$filename ) { |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | $import_dirs = []; |
| 21 | |
| 22 | if ( Less_Environment::isPathRelative( $filename ) ) { |
| 23 | // if the path is relative, the file should be in the current directory |
| 24 | if ( $currentFileInfo ) { |
| 25 | $import_dirs[ $currentFileInfo['currentDirectory'] ] = $currentFileInfo['uri_root']; |
| 26 | } |
| 27 | |
| 28 | } else { |
| 29 | // otherwise, the file should be relative to the server root |
| 30 | if ( $currentFileInfo ) { |
| 31 | $import_dirs[ $currentFileInfo['entryPath'] ] = $currentFileInfo['entryUri']; |
| 32 | } |
| 33 | // if the user supplied entryPath isn't the actual root |
| 34 | $import_dirs[ $_SERVER['DOCUMENT_ROOT'] ] = ''; |
| 35 | |
| 36 | } |
| 37 | |
| 38 | // always look in user supplied import directories |
| 39 | $import_dirs = array_merge( $import_dirs, Less_Parser::$options['import_dirs'] ); |
| 40 | |
| 41 | foreach ( $import_dirs as $rootpath => $rooturi ) { |
| 42 | if ( is_callable( $rooturi ) ) { |
| 43 | $res = $rooturi( $filename ); |
| 44 | if ( $res && is_string( $res[0] ) ) { |
| 45 | return [ |
| 46 | Less_Environment::normalizePath( $res[0] ), |
| 47 | Less_Environment::normalizePath( $res[1] ?? dirname( $filename ) ) |
| 48 | ]; |
| 49 | } |
| 50 | } elseif ( !empty( $rootpath ) ) { |
| 51 | $path = rtrim( $rootpath, '/\\' ) . '/' . ltrim( $filename, '/\\' ); |
| 52 | if ( file_exists( $path ) ) { |
| 53 | return [ |
| 54 | Less_Environment::normalizePath( $path ), |
| 55 | Less_Environment::normalizePath( dirname( $rooturi . $filename ) ) |
| 56 | ]; |
| 57 | } |
| 58 | if ( file_exists( $path . '.less' ) ) { |
| 59 | return [ |
| 60 | Less_Environment::normalizePath( $path . '.less' ), |
| 61 | Less_Environment::normalizePath( dirname( $rooturi . $filename . '.less' ) ) |
| 62 | ]; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |