File: FileManager.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (65 lines) | stat: -rw-r--r-- 2,011 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
 * @private
 */
class Less_FileManager {
	/**
	 * Get the full absolute path and uri of the import
	 * @see less-node/FileManager.getPath https://github.com/less/less.js/blob/v2.5.3/lib/less-node/file-manager.js#L70
	 * @param string $filename
	 * @param null|array $currentFileInfo
	 * @return null|array{0:string,1:string}
	 */
	public static function getFilePath( $filename, $currentFileInfo ) {
		if ( !$filename ) {
			return;
		}

			$import_dirs = [];

		if ( Less_Environment::isPathRelative( $filename ) ) {
			// if the path is relative, the file should be in the current directory
			if ( $currentFileInfo ) {
				$import_dirs[ $currentFileInfo['currentDirectory'] ] = $currentFileInfo['uri_root'];
			}

		} else {
			// otherwise, the file should be relative to the server root
			if ( $currentFileInfo ) {
				$import_dirs[ $currentFileInfo['entryPath'] ] = $currentFileInfo['entryUri'];
			}
			// if the user supplied entryPath isn't the actual root
			$import_dirs[ $_SERVER['DOCUMENT_ROOT'] ] = '';

		}

			// always look in user supplied import directories
			$import_dirs = array_merge( $import_dirs, Less_Parser::$options['import_dirs'] );

		foreach ( $import_dirs as $rootpath => $rooturi ) {
			if ( is_callable( $rooturi ) ) {
				$res = $rooturi( $filename );
				if ( $res && is_string( $res[0] ) ) {
					return [
						Less_Environment::normalizePath( $res[0] ),
						Less_Environment::normalizePath( $res[1] ?? dirname( $filename ) )
					];
				}
			} elseif ( !empty( $rootpath ) ) {
				$path = rtrim( $rootpath, '/\\' ) . '/' . ltrim( $filename, '/\\' );
				if ( file_exists( $path ) ) {
					return [
						Less_Environment::normalizePath( $path ),
						Less_Environment::normalizePath( dirname( $rooturi . $filename ) )
					];
				}
				if ( file_exists( $path . '.less' ) ) {
					return [
						Less_Environment::normalizePath( $path . '.less' ),
						Less_Environment::normalizePath( dirname( $rooturi . $filename . '.less' ) )
					];
				}
			}
		}
	}
}