| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | namespace MediaWiki\ResourceLoader; |
| 22 | |
| 23 | use RuntimeException; |
| 24 | |
| 25 | /** |
| 26 | * A path to a bundled file (such as JavaScript or CSS), along with a remote and local base path. |
| 27 | * |
| 28 | * This is for use with FileModule. Base path may be `null`, which indicates that the |
| 29 | * path is expanded relative to the corresponding base path of the FileModule object instead. |
| 30 | * |
| 31 | * @ingroup ResourceLoader |
| 32 | * @since 1.17 |
| 33 | */ |
| 34 | class FilePath { |
| 35 | /** @var string|null Local base path */ |
| 36 | protected ?string $localBasePath; |
| 37 | |
| 38 | /** @var string|null Remote base path */ |
| 39 | protected ?string $remoteBasePath; |
| 40 | |
| 41 | /** @var string Path to the file */ |
| 42 | protected string $path; |
| 43 | |
| 44 | /** |
| 45 | * @param string $path Relative path to the file, no leading slash. |
| 46 | * @param string|null $localBasePath Base path to prepend when generating a local path. |
| 47 | * @param string|null $remoteBasePath Base path to prepend when generating a remote path. |
| 48 | * Should not have a trailing slash unless at web document root. |
| 49 | */ |
| 50 | public function __construct( string $path, ?string $localBasePath = null, ?string $remoteBasePath = null ) { |
| 51 | $this->path = $path; |
| 52 | $this->localBasePath = $localBasePath; |
| 53 | $this->remoteBasePath = $remoteBasePath; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @return string |
| 58 | * @throws RuntimeException If the base path was not provided. You must either provide the base |
| 59 | * path in the constructor, or use getPath() instead and add the base path from a FileModule. |
| 60 | */ |
| 61 | public function getLocalPath(): string { |
| 62 | if ( $this->localBasePath === null ) { |
| 63 | throw new RuntimeException( 'Base path was not provided' ); |
| 64 | } |
| 65 | return "{$this->localBasePath}/{$this->path}"; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @return string |
| 70 | * @throws RuntimeException If the base path was not provided. You must either provide the base |
| 71 | * path in the constructor, or use getPath() instead and add the base path from a FileModule. |
| 72 | */ |
| 73 | public function getRemotePath(): string { |
| 74 | if ( $this->remoteBasePath === null ) { |
| 75 | throw new RuntimeException( 'Base path was not provided' ); |
| 76 | } |
| 77 | if ( $this->remoteBasePath === '/' ) { |
| 78 | // In document root |
| 79 | // Don't insert another slash (T284391). |
| 80 | return $this->remoteBasePath . $this->path; |
| 81 | } |
| 82 | return "{$this->remoteBasePath}/{$this->path}"; |
| 83 | } |
| 84 | |
| 85 | /** @return string|null */ |
| 86 | public function getLocalBasePath(): ?string { |
| 87 | return $this->localBasePath; |
| 88 | } |
| 89 | |
| 90 | /** @return string|null */ |
| 91 | public function getRemoteBasePath(): ?string { |
| 92 | return $this->remoteBasePath; |
| 93 | } |
| 94 | |
| 95 | /** @return string */ |
| 96 | public function getPath(): string { |
| 97 | return $this->path; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Set the base path if it has not already been set. |
| 102 | * |
| 103 | * @param string $localBasePath |
| 104 | * @param string $remoteBasePath |
| 105 | */ |
| 106 | public function initBasePaths( string $localBasePath, string $remoteBasePath ) { |
| 107 | if ( $this->localBasePath === null ) { |
| 108 | $this->localBasePath = $localBasePath; |
| 109 | } |
| 110 | if ( $this->remoteBasePath === null ) { |
| 111 | $this->remoteBasePath = $remoteBasePath; |
| 112 | } |
| 113 | } |
| 114 | } |