Debian Codegraph @

vendor/wikimedia/minify/src/MapperState.php

raw
1 <?php
2 /**
3 * Copyright 2022 Wikimedia Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * @file
18 * @license Apache-2.0
19 * @license MIT
20 * @license GPL-2.0-or-later
21 * @license LGPL-2.1-or-later
22 */
23
24 namespace Wikimedia\Minify;
25
26 /**
27 * The base class for stateful minifying with source map fetching
28 */
29 abstract class MapperState extends MinifierState {
30 /** @var MappingsGenerator|null */
31 protected $mappingsGenerator;
32
33 public function __construct() {
34 $this->mappingsGenerator = new MappingsGenerator;
35 }
36
37 /**
38 * Minify a source file and collect the output and mappings data.
39 *
40 * @param string $url The name of the input file. Possibly a URL relative
41 * to the source root.
42 * @param string $source The input source text.
43 * @param bool $bundle Whether to add the source text to sourcesContent
44 * @return $this
45 */
46 public function addSourceFile( string $url, string $source, bool $bundle = false ) {
47 $this->sources[] = $url;
48 if ( $bundle ) {
49 $this->sourcesContent[] = $source;
50 } else {
51 $this->sourcesContent[] = null;
52 }
53 $this->mappingsGenerator->nextSourceFile( $source );
54 return parent::addSourceFile( $url, $source, $bundle );
55 }
56
57 /**
58 * Add a string to the output without any minification or source mapping.
59 *
60 * @param string $output
61 * @return $this
62 */
63 public function addOutput( string $output ) {
64 $this->mappingsGenerator->outputSpace( $output );
65 return parent::addOutput( $output );
66 }
67
68 /**
69 * Get the source map data to be JSON encoded.
70 *
71 * @return array
72 */
73 public function getSourceMapData() {
74 $data = [ 'version' => 3 ];
75 if ( $this->outputFile !== null ) {
76 $data['file'] = $this->outputFile;
77 }
78 if ( $this->sourceRoot !== null ) {
79 $data['sourceRoot'] = $this->sourceRoot;
80 }
81 $data['sources'] = $this->sources;
82
83 $needSourcesContent = false;
84 foreach ( $this->sourcesContent as $content ) {
85 if ( $content !== null ) {
86 $needSourcesContent = true;
87 }
88 }
89 if ( $needSourcesContent ) {
90 $data['sourcesContent'] = $this->sourcesContent;
91 }
92 $data['names'] = [];
93 $data['mappings'] = $this->mappingsGenerator->getMap();
94 return $data;
95 }
96
97 /**
98 * Get the JSON-encoded source map. Take care to avoid leaking private data
99 * due to an XSSI attack.
100 *
101 * @return string
102 */
103 public function getRawSourceMap() {
104 $data = $this->getSourceMapData();
105 $out = "{\n";
106 $first = true;
107 foreach ( $data as $key => $value ) {
108 if ( $first ) {
109 $first = false;
110 } else {
111 $out .= ",\n";
112 }
113 $out .= json_encode( $key ) . ': ' .
114 json_encode( $value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
115 }
116 $out .= "\n}\n";
117 return $out;
118 }
119
120 /**
121 * Get the JSON-encoded source map including XSSI protection prefix.
122 *
123 * @return string
124 */
125 public function getSourceMap() {
126 return ")]}\n" . $this->getRawSourceMap();
127 }
128 }