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