Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| MinifierState | |
0.00% |
0 / 14 |
|
0.00% |
0 / 7 |
90 | |
0.00% |
0 / 1 |
| outputFile | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| sourceRoot | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| addSourceFile | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setErrorHandler | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| minify | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| addOutput | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| ensureNewline | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| getMinifiedOutput | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( 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 | |
| 26 | namespace Wikimedia\Minify; |
| 27 | |
| 28 | /** |
| 29 | * The base class for stateful minifying without source map support. |
| 30 | * |
| 31 | * Some stub mutator methods for source map support are provided for |
| 32 | * the convenience of callers switching between source map and plain mode. |
| 33 | */ |
| 34 | abstract class MinifierState { |
| 35 | /** @var string[] The accumulated data for the source map sources line */ |
| 36 | protected $sources = []; |
| 37 | |
| 38 | /** @var array The accumulated data for the source map sourcesContent line */ |
| 39 | protected $sourcesContent = []; |
| 40 | |
| 41 | /** @var string The accumulated minified output */ |
| 42 | protected $minifiedOutput = ''; |
| 43 | |
| 44 | /** @var string|null The value for the "file" key in the source map */ |
| 45 | protected $outputFile; |
| 46 | |
| 47 | /** @var string|null The value for the "sourceRoot" key in the source map */ |
| 48 | protected $sourceRoot; |
| 49 | |
| 50 | /** @var callable|null */ |
| 51 | protected $onError; |
| 52 | |
| 53 | /** |
| 54 | * Set the name of the output file, to be given as the "file" key. |
| 55 | * |
| 56 | * @param string $file |
| 57 | * @return $this |
| 58 | */ |
| 59 | public function outputFile( string $file ) { |
| 60 | $this->outputFile = $file; |
| 61 | return $this; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Set the source root. The spec says this will be merely "prepended" to |
| 66 | * the source names, not resolved as a relative URL, so it should probably |
| 67 | * have a trailing slash. |
| 68 | * |
| 69 | * @param string $url |
| 70 | * @return $this |
| 71 | */ |
| 72 | public function sourceRoot( string $url ) { |
| 73 | $this->sourceRoot = $url; |
| 74 | return $this; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Minify a source file and collect the output and mappings data. |
| 79 | * |
| 80 | * @param string $url The name of the input file. Possibly a URL relative |
| 81 | * to the source root. |
| 82 | * @param string $source The input source text. |
| 83 | * @param bool $bundle Whether to add the source text to sourcesContent |
| 84 | * @return $this |
| 85 | */ |
| 86 | public function addSourceFile( string $url, string $source, bool $bundle = false ) { |
| 87 | $this->minifiedOutput .= $this->minify( $source ); |
| 88 | return $this; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Register an error callback |
| 93 | * |
| 94 | * JavaScriptMinifier assumes all input has previously been validated |
| 95 | * by a linter (such as Peast or ESLint) and not attempt to validate |
| 96 | * the syntax. Any new or unknown syntax is generally silently preserved |
| 97 | * in the output. |
| 98 | * |
| 99 | * Certain kinds of syntax error are nonetheless detected and can |
| 100 | * be logged via this error callback to aid in debugging. |
| 101 | * |
| 102 | * The error callback may be invoked during MinifierState::addSourceFile |
| 103 | * (via JavaScriptMinifier::minify), and is passed a ParseError object |
| 104 | * as its only parameter. |
| 105 | * |
| 106 | * Example: |
| 107 | * |
| 108 | * JavaScriptMinifier::createMinifier() |
| 109 | * ->setErrorHandler( static function ( $error ) { |
| 110 | * trigger_error( $error->getMessage() ); |
| 111 | * } ) |
| 112 | * ->addSourceFile( 'file.js', $source ) |
| 113 | * ->getMinifiedOutput(); |
| 114 | * |
| 115 | * @param callable $onError |
| 116 | * @return $this |
| 117 | */ |
| 118 | public function setErrorHandler( $onError ) { |
| 119 | $this->onError = $onError; |
| 120 | return $this; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Minify a string |
| 125 | * |
| 126 | * @param string $source |
| 127 | * @return string |
| 128 | */ |
| 129 | abstract protected function minify( string $source ): string; |
| 130 | |
| 131 | /** |
| 132 | * Add a string to the output without any minification or source mapping. |
| 133 | * |
| 134 | * @param string $output |
| 135 | * @return $this |
| 136 | */ |
| 137 | public function addOutput( string $output ) { |
| 138 | $this->minifiedOutput .= $output; |
| 139 | return $this; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Add a line break to the output if the output is non-empty and does not |
| 144 | * end in a line break. |
| 145 | * |
| 146 | * @return $this |
| 147 | */ |
| 148 | public function ensureNewline() { |
| 149 | if ( $this->minifiedOutput !== '' && !str_ends_with( $this->minifiedOutput, "\n" ) ) { |
| 150 | $this->addOutput( "\n" ); |
| 151 | } |
| 152 | return $this; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get the minified output. |
| 157 | * |
| 158 | * @return string |
| 159 | */ |
| 160 | public function getMinifiedOutput() { |
| 161 | return $this->minifiedOutput; |
| 162 | } |
| 163 | } |