Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
24 / 24 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| IndexMap | |
100.00% |
24 / 24 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| outputFile | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addEncodedMap | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| getMap | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | /** |
| 5 | * Copyright 2023 Wikimedia Foundation |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * @file |
| 20 | * @license Apache-2.0 |
| 21 | * @license MIT |
| 22 | * @license GPL-2.0-or-later |
| 23 | * @license LGPL-2.1-or-later |
| 24 | */ |
| 25 | |
| 26 | namespace Wikimedia\Minify; |
| 27 | |
| 28 | /** |
| 29 | * A class representing an index map, as defined by the source map |
| 30 | * specification. This allows several mapped sources to be combined into a |
| 31 | * single file. |
| 32 | */ |
| 33 | class IndexMap { |
| 34 | /** @var string|null */ |
| 35 | private $file; |
| 36 | /** @var IndexMapOffset */ |
| 37 | private $offset; |
| 38 | /** @var array */ |
| 39 | private $sections; |
| 40 | |
| 41 | /** |
| 42 | * Create an empty index map |
| 43 | */ |
| 44 | public function __construct() { |
| 45 | $this->offset = new IndexMapOffset( 0, 0 ); |
| 46 | $this->sections = []; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Set the name of the output file, to be given as the "file" key. |
| 51 | * |
| 52 | * @param string $file |
| 53 | * @return $this |
| 54 | */ |
| 55 | public function outputFile( string $file ) { |
| 56 | $this->file = $file; |
| 57 | return $this; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Add a section with a source map which was encoded in the "raw" JSON format. |
| 62 | * |
| 63 | * @param string $mapJson The JSON-encoded source map. |
| 64 | * @param IndexMapOffset $generatedSize The size of the generated output |
| 65 | * corresponding to $mapJson. This is used to advance the current offset |
| 66 | * and will be used to calculate the offset of the next section, if there |
| 67 | * is one. |
| 68 | * @return $this |
| 69 | */ |
| 70 | public function addEncodedMap( string $mapJson, IndexMapOffset $generatedSize ) { |
| 71 | $this->sections[] = |
| 72 | '{"offset":' . |
| 73 | json_encode( [ |
| 74 | 'line' => $this->offset->line, |
| 75 | 'column' => $this->offset->column, |
| 76 | ] ) . |
| 77 | ',"map":' . |
| 78 | $mapJson . |
| 79 | '}'; |
| 80 | $this->offset->add( $generatedSize ); |
| 81 | return $this; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get the index map, encoded as JSON |
| 86 | * |
| 87 | * @return string |
| 88 | */ |
| 89 | public function getMap(): string { |
| 90 | $map = "{\n" . |
| 91 | "\"version\": 3,\n"; |
| 92 | if ( $this->file !== null ) { |
| 93 | $map .= '"file": ' . json_encode( $this->file ) . ",\n"; |
| 94 | } |
| 95 | $map .= "\"sections\": [\n"; |
| 96 | if ( $this->sections ) { |
| 97 | $map .= implode( ",\n", $this->sections ) . "\n"; |
| 98 | } |
| 99 | $map .= "]\n}"; |
| 100 | return $map; |
| 101 | } |
| 102 | } |