File modules/util/ui/minifier/AbstractMinifier.class.php

Last commit: Sun Oct 13 13:17:32 2024 +0200	Jan Dankert	New Minifier for CSS and JS: Leave JS Linebreaks as they are (for better debugging); Exploded CSS files (instead of a combined one), LESS is necessary but should be avoided in the future.
1 <?php 2 3 namespace util\ui\minifier; 4 5 use util\json\JSON; 6 7 /** 8 * An minifier superclass with support for sourcemaps. 9 * @property string $compressedContent 10 */ 11 abstract class AbstractMinifier 12 { 13 private $mappings = []; 14 private $sourceFiles; 15 16 const FLAG_CREATE_SOURCEMAP = 256; 17 const FLAG_ALL = PHP_INT_MAX; 18 19 /** 20 * Flags. 21 * @var int 22 */ 23 private $config = self::FLAG_ALL; 24 25 protected function hasFlag( $flag ) 26 { 27 return boolval( $this->config & $flag ); 28 } 29 30 /** 31 * Source file content. 32 * 33 * @var string 34 */ 35 private $content; 36 37 public function setNextSourceFile( $filename ) { 38 $this->content = file_get_contents($filename); 39 $this->addSourceFile( basename($filename),$filename ); 40 $this->compress( $this->getAllChars() ); 41 } 42 43 public function getNextChar() 44 { 45 if ( strlen($this->content) == 0 ) 46 return null; 47 48 $firstChar = substr( $this->content,0,1); 49 $this->content = substr( $this->content,1); 50 return $firstChar; 51 } 52 53 public function getAllChars() 54 { 55 return str_split( $this->content); 56 } 57 58 public function addSourceFile( $filename,$index=null ) { 59 if ( !$index ) 60 $index = basename($filename); 61 $this->sourceFiles[ $index ] = $filename; 62 } 63 64 65 public function getSourcemapLink() 66 { 67 68 } 69 70 /** 71 * Creates the JSON sourcemap. 72 * 73 * @return void 74 */ 75 public function generateSourcemap() 76 { 77 $sourcemap = [ 78 "version" => 3, 79 "file" => $this->outFile, 80 "sourceRoot" => "", 81 "sources" => $this->sourceFiles, 82 "names" => [], 83 "mappings" => $this->generateMappings() 84 ]; 85 return JSON::encode( $sourcemap ); 86 } 87 88 89 public function addMapping($destLine, $destColumn, $srcIndex, $srcLine, $srcColumn) 90 { 91 $this->mappings[] = array( 92 'dest_line' => $destLine, // Line in the compiled file 93 'dest_col' => $destColumn, // Column in the compiled file 94 'src_index' => $srcIndex, // Index of the source file 95 'src_line' => $srcLine, // Line in the source file 96 'src_col' => $srcColumn, // Column in the source file 97 ); 98 } 99 100 101 102 public function __construct( $flags = null ) 103 { 104 if ( $flags ) 105 $this->config = $flags; 106 } 107 108 109 110 111 /** 112 * Create the mappings. 113 * 114 * @return string 115 */ 116 public function generateMappings() { 117 118 // Group mappings by dest line number. 119 $grouped_map = array(); 120 foreach ($this->mappings as $m) { 121 $grouped_map[$m['dest_line']][] = $m; 122 } 123 124 ksort($grouped_map); 125 126 $grouped_map_enc = array(); 127 128 $last_dest_line = 0; 129 $last_src_index = 0; 130 $last_src_line = 0; 131 $last_src_col = 0; 132 foreach ($grouped_map as $dest_line => $line_map) { 133 while (++$last_dest_line < $dest_line) { 134 $grouped_map_enc[] = ";"; 135 } 136 137 $line_map_enc = array(); 138 $last_dest_col = 0; 139 140 foreach ($line_map as $m) { 141 $m_enc = Base64VLQ::encode($m['dest_col'] - $last_dest_col); 142 $last_dest_col = $m['dest_col']; 143 if (isset($m['src_index'])) { 144 $m_enc .= Base64VLQ::encode($m['src_index'] - $last_src_index); 145 $last_src_index = $m['src_index']; 146 147 $m_enc .= Base64VLQ::encode($m['src_line'] - $last_src_line); 148 $last_src_line = $m['src_line']; 149 150 $m_enc .= Base64VLQ::encode($m['src_col'] - $last_src_col); 151 $last_src_col = $m['src_col']; 152 } 153 $line_map_enc[] = $m_enc; 154 } 155 156 $grouped_map_enc[] = implode(",", $line_map_enc) . ";"; 157 } 158 159 $grouped_map_enc = implode($grouped_map_enc); 160 161 return $grouped_map_enc; 162 } 163 164 165 public function addCompressedContent( $add ) 166 { 167 $this->compressedContent .= $add; 168 } 169 170 171 public function getCompressedContent() 172 { 173 $this->compress( str_split(file_get_contents( array_shift($this->sourceFiles)))); 174 return $this->compressedContent . ($this->hasFlag(self::FLAG_CREATE_SOURCEMAP) ? $this->linkToSourcemap( "" ) : ''); 175 } 176 177 178 abstract public function linkToSourcemap( $sourcemap ); 179 180 abstract protected function compress( $chars ); 181 }
Download modules/util/ui/minifier/AbstractMinifier.class.php
History Sun, 13 Oct 2024 13:17:32 +0200 Jan Dankert New Minifier for CSS and JS: Leave JS Linebreaks as they are (for better debugging); Exploded CSS files (instead of a combined one), LESS is necessary but should be avoided in the future.