Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
77.78% |
14 / 18 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| FilePath | |
77.78% |
14 / 18 |
|
85.71% |
6 / 7 |
13.58 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getLocalPath | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getRemotePath | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| getLocalBasePath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRemoteBasePath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| initBasePaths | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\ResourceLoader; |
| 8 | |
| 9 | use RuntimeException; |
| 10 | |
| 11 | /** |
| 12 | * A path to a bundled file (such as JavaScript or CSS), along with a remote and local base path. |
| 13 | * |
| 14 | * This is for use with FileModule. Base path may be `null`, which indicates that the |
| 15 | * path is expanded relative to the corresponding base path of the FileModule object instead. |
| 16 | * |
| 17 | * @ingroup ResourceLoader |
| 18 | * @since 1.17 |
| 19 | */ |
| 20 | class FilePath { |
| 21 | /** @var string|null Local base path */ |
| 22 | protected ?string $localBasePath; |
| 23 | |
| 24 | /** @var string|null Remote base path */ |
| 25 | protected ?string $remoteBasePath; |
| 26 | |
| 27 | /** @var string Path to the file */ |
| 28 | protected string $path; |
| 29 | |
| 30 | /** |
| 31 | * @param string $path Relative path to the file, no leading slash. |
| 32 | * @param string|null $localBasePath Base path to prepend when generating a local path. |
| 33 | * @param string|null $remoteBasePath Base path to prepend when generating a remote path. |
| 34 | * Should not have a trailing slash unless at web document root. |
| 35 | */ |
| 36 | public function __construct( string $path, ?string $localBasePath = null, ?string $remoteBasePath = null ) { |
| 37 | $this->path = $path; |
| 38 | $this->localBasePath = $localBasePath; |
| 39 | $this->remoteBasePath = $remoteBasePath; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @return string |
| 44 | * @throws RuntimeException If the base path was not provided. You must either provide the base |
| 45 | * path in the constructor, or use getPath() instead and add the base path from a FileModule. |
| 46 | */ |
| 47 | public function getLocalPath(): string { |
| 48 | if ( $this->localBasePath === null ) { |
| 49 | throw new RuntimeException( 'Base path was not provided' ); |
| 50 | } |
| 51 | return "{$this->localBasePath}/{$this->path}"; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @return string |
| 56 | * @throws RuntimeException If the base path was not provided. You must either provide the base |
| 57 | * path in the constructor, or use getPath() instead and add the base path from a FileModule. |
| 58 | */ |
| 59 | public function getRemotePath(): string { |
| 60 | if ( $this->remoteBasePath === null ) { |
| 61 | throw new RuntimeException( 'Base path was not provided' ); |
| 62 | } |
| 63 | if ( $this->remoteBasePath === '/' ) { |
| 64 | // In document root |
| 65 | // Don't insert another slash (T284391). |
| 66 | return $this->remoteBasePath . $this->path; |
| 67 | } |
| 68 | return "{$this->remoteBasePath}/{$this->path}"; |
| 69 | } |
| 70 | |
| 71 | public function getLocalBasePath(): ?string { |
| 72 | return $this->localBasePath; |
| 73 | } |
| 74 | |
| 75 | public function getRemoteBasePath(): ?string { |
| 76 | return $this->remoteBasePath; |
| 77 | } |
| 78 | |
| 79 | public function getPath(): string { |
| 80 | return $this->path; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Set the base path if it has not already been set. |
| 85 | * |
| 86 | * @param string $localBasePath |
| 87 | * @param string $remoteBasePath |
| 88 | */ |
| 89 | public function initBasePaths( string $localBasePath, string $remoteBasePath ) { |
| 90 | if ( $this->localBasePath === null ) { |
| 91 | $this->localBasePath = $localBasePath; |
| 92 | } |
| 93 | if ( $this->remoteBasePath === null ) { |
| 94 | $this->remoteBasePath = $remoteBasePath; |
| 95 | } |
| 96 | } |
| 97 | } |