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