Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
MapperState
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 6
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addSourceFile
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 addOutput
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getSourceMapData
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
42
 getRawSourceMap
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 getSourceMap
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4/**
5 * Copyright 2022 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
26namespace Wikimedia\Minify;
27
28/**
29 * The base class for stateful minifying with source map fetching
30 */
31abstract class MapperState extends MinifierState {
32    /** @var MappingsGenerator|null */
33    protected $mappingsGenerator;
34
35    public function __construct() {
36        $this->mappingsGenerator = new MappingsGenerator;
37    }
38
39    /**
40     * Minify a source file and collect the output and mappings data.
41     *
42     * @param string $url The name of the input file. Possibly a URL relative
43     *   to the source root.
44     * @param string $source The input source text.
45     * @param bool $bundle Whether to add the source text to sourcesContent
46     * @return $this
47     */
48    public function addSourceFile( string $url, string $source, bool $bundle = false ) {
49        $this->sources[] = $url;
50        if ( $bundle ) {
51            $this->sourcesContent[] = $source;
52        } else {
53            $this->sourcesContent[] = null;
54        }
55        $this->mappingsGenerator->nextSourceFile( $source );
56        return parent::addSourceFile( $url, $source, $bundle );
57    }
58
59    /**
60     * Add a string to the output without any minification or source mapping.
61     *
62     * @param string $output
63     * @return $this
64     */
65    public function addOutput( string $output ) {
66        $this->mappingsGenerator->outputSpace( $output );
67        return parent::addOutput( $output );
68    }
69
70    /**
71     * Get the source map data to be JSON encoded.
72     *
73     * @return array
74     */
75    public function getSourceMapData() {
76        $data = [ 'version' => 3 ];
77        if ( $this->outputFile !== null ) {
78            $data['file'] = $this->outputFile;
79        }
80        if ( $this->sourceRoot !== null ) {
81            $data['sourceRoot'] = $this->sourceRoot;
82        }
83        $data['sources'] = $this->sources;
84
85        $needSourcesContent = false;
86        foreach ( $this->sourcesContent as $content ) {
87            if ( $content !== null ) {
88                $needSourcesContent = true;
89            }
90        }
91        if ( $needSourcesContent ) {
92            $data['sourcesContent'] = $this->sourcesContent;
93        }
94        $data['names'] = [];
95        $data['mappings'] = $this->mappingsGenerator->getMap();
96        return $data;
97    }
98
99    /**
100     * Get the JSON-encoded source map. Take care to avoid leaking private data
101     * due to an XSSI attack.
102     *
103     * @return string
104     */
105    public function getRawSourceMap() {
106        $data = $this->getSourceMapData();
107        $out = "{\n";
108        $first = true;
109        foreach ( $data as $key => $value ) {
110            if ( $first ) {
111                $first = false;
112            } else {
113                $out .= ",\n";
114            }
115            $out .= json_encode( $key ) . ': ' .
116                json_encode( $value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
117        }
118        $out .= "\n}\n";
119        return $out;
120    }
121
122    /**
123     * Get the JSON-encoded source map including XSSI protection prefix.
124     *
125     * @return string
126     */
127    public function getSourceMap() {
128        return ")]}\n" . $this->getRawSourceMap();
129    }
130}