|
| 1 | +<?php |
| 2 | +// |
| 3 | +// Pot.js / PotLite.js |
| 4 | +// JavaScript code minifier |
| 5 | +// |
| 6 | +// Usage: |
| 7 | +// Run server. |
| 8 | +// Access http://localhost/{this-directory}/minify.php?type=full |
| 9 | +// Parameters: |
| 10 | +// - type: |
| 11 | +// "lite" or "full" |
| 12 | +// Minify "PotLite.js" if "type" is "lite". |
| 13 | +// Minify "Pot.js" if "type" is "full". |
| 14 | +// |
| 15 | + |
| 16 | +pot_minify(); |
| 17 | + |
| 18 | +class Pot_Minifier { |
| 19 | + |
| 20 | + const MINIFIER = '../../yuicompressor-2.4.7/build/yuicompressor-2.4.7.jar'; |
| 21 | + |
| 22 | + private $dir; |
| 23 | + private $fileName; |
| 24 | + private $trapName; |
| 25 | + |
| 26 | + private $parameters = array( |
| 27 | + 'line-break' => 160, |
| 28 | + 'type' => 'js', |
| 29 | + 'charset' => 'UTF-8' |
| 30 | + ); |
| 31 | + |
| 32 | + function __construct() { |
| 33 | + $this->init(); |
| 34 | + if (empty($_GET)) { |
| 35 | + die('Bad Request'); |
| 36 | + } |
| 37 | + $this->dir = dirname(__FILE__); |
| 38 | + $type = null; |
| 39 | + if (isset($_GET['type'])) { |
| 40 | + $type = $_GET['type']; |
| 41 | + } |
| 42 | + switch (strtolower(trim($type))) { |
| 43 | + case 'full': |
| 44 | + $this->fileName = 'pot.js'; |
| 45 | + $this->minName = 'pot.min.js'; |
| 46 | + $this->trapName = '@-trap-pot.js'; |
| 47 | + $this->trapMinName = '@-trap-pot.min.js'; |
| 48 | + break; |
| 49 | + case 'lite': |
| 50 | + $this->fileName = 'potlite.js'; |
| 51 | + $this->minName = 'potlite.min.js'; |
| 52 | + $this->trapName = '@-trap-potlite.js'; |
| 53 | + $this->trapMinName = '@-trap-potlite.min.js'; |
| 54 | + break; |
| 55 | + default: |
| 56 | + die('Invalid type'); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + function init() { |
| 61 | + error_reporting(E_ALL); |
| 62 | + @ini_set('display_errors', 1); |
| 63 | + @ini_set('log_errors', false); |
| 64 | + @ini_set('track_errors', false); |
| 65 | + @ini_set('default_charset', 'UTF-8'); |
| 66 | + if (function_exists('date_default_timezone_set') && |
| 67 | + function_exists('date_default_timezone_get')) { |
| 68 | + @date_default_timezone_set(@date_default_timezone_get()); |
| 69 | + } |
| 70 | + header('Content-Type: application/javascript'); |
| 71 | + } |
| 72 | + |
| 73 | + function minify() { |
| 74 | + $this->trap(); |
| 75 | + $command = sprintf('java -jar "%s"', $this->fullPath(self::MINIFIER)); |
| 76 | + $params = array(); |
| 77 | + foreach ($this->parameters as $name => $val) { |
| 78 | + $params[] = sprintf('--%s %s', $name, $val); |
| 79 | + } |
| 80 | + $parameter = sprintf( |
| 81 | + '%s -o "%s/%s" -v "%s/%s"', |
| 82 | + implode(' ', $params), |
| 83 | + $this->dir, |
| 84 | + $this->trapMinName, |
| 85 | + $this->dir, |
| 86 | + $this->trapName |
| 87 | + ); |
| 88 | + $cmd = strtr(sprintf('%s %s', $command, $parameter), '\\', '/'); |
| 89 | + $output = $this->runExternal($cmd, true); |
| 90 | + printf("%s\n", $output); |
| 91 | + sleep(5); |
| 92 | + $this->untrap(); |
| 93 | + printf("Minify SIZE: %s", number_format(filesize($this->minName))); |
| 94 | + } |
| 95 | + |
| 96 | + function trap() { |
| 97 | + $code = file_get_contents($this->dir . '/' . $this->fileName); |
| 98 | + $code = preg_replace('<^(.*?)([(]function\b\s*\w*\s*[(])>s', '$1(function(){$2', $code, 1); |
| 99 | + $code = preg_replace('<([)]\s*;?\s*)$>s', '$1})();', $code, 1); |
| 100 | + file_put_contents($this->dir . '/' . $this->trapName, $code); |
| 101 | + } |
| 102 | + |
| 103 | + function untrap() { |
| 104 | + $code = file_get_contents($this->dir . '/' . $this->trapMinName); |
| 105 | + $code = preg_replace('< |
| 106 | + ^ (.*?) |
| 107 | + [(]function[(][)][{] |
| 108 | + ([(]function\b\s*\w*\s*[(][^)]*[)]\s*[{]) |
| 109 | + >sx', '$1$2"use strict";', $code); |
| 110 | + $code = preg_replace('<([();])\s*[}][)][(][)]\s*;?\s*$>', '$1', $code); |
| 111 | + $code = rtrim(trim($code), ';') . ";\x0A"; |
| 112 | + file_put_contents($this->dir . '/' . $this->minName, $code); |
| 113 | + unlink($this->dir . '/' . $this->trapName); |
| 114 | + unlink($this->dir . '/' . $this->trapMinName); |
| 115 | + } |
| 116 | + |
| 117 | + function runExternal($command, $as_is = false) { |
| 118 | + $result = null; |
| 119 | + $specs = array( |
| 120 | + 0 => array('pipe', 'r'), |
| 121 | + 1 => array('pipe', 'w'), |
| 122 | + 2 => array('pipe', 'w') |
| 123 | + ); |
| 124 | + $process = proc_open($command, $specs, $pipes); |
| 125 | + if (is_resource($process)) { |
| 126 | + fclose($pipes[0]); |
| 127 | + if (function_exists('stream_set_blocking')) { |
| 128 | + stream_set_blocking($pipes[1], 0); |
| 129 | + stream_set_blocking($pipes[2], 0); |
| 130 | + } else if (function_exists('socket_set_blocking')) { |
| 131 | + socket_set_blocking($pipes[1], 0); |
| 132 | + socket_set_blocking($pipes[2], 0); |
| 133 | + } |
| 134 | + $outputs = array(); |
| 135 | + while (!feof($pipes[1])) { |
| 136 | + $outputs[] = fgets($pipes[1]); |
| 137 | + } |
| 138 | + $outputs[] = "\n"; |
| 139 | + while (!feof($pipes[2])) { |
| 140 | + $outputs[] = fgets($pipes[2]); |
| 141 | + } |
| 142 | + fclose($pipes[1]); |
| 143 | + fclose($pipes[2]); |
| 144 | + proc_close($process); |
| 145 | + if ($as_is) { |
| 146 | + $result = implode('', $outputs); |
| 147 | + } else { |
| 148 | + $outputs = array_filter(array_map('trim', $outputs), 'strlen'); |
| 149 | + $result = array_pop($outputs); |
| 150 | + } |
| 151 | + } |
| 152 | + return $result; |
| 153 | + } |
| 154 | + |
| 155 | + function fullPath($path, $check_exists = false) { |
| 156 | + static $backslash = '\\', $slash = '/', $colon = ':', $is_win = null; |
| 157 | + if ($is_win === null) { |
| 158 | + $is_win = (0 === strncasecmp(PHP_OS, 'win', 3)); |
| 159 | + } |
| 160 | + $result = ''; |
| 161 | + $fullpath = $path; |
| 162 | + $pre0 = substr($path, 0, 1); |
| 163 | + $pre1 = substr($path, 1, 1); |
| 164 | + if ((!$is_win && $pre0 !== $slash) |
| 165 | + || ($is_win && $pre1 !== $colon)) { |
| 166 | + $fullpath = getcwd() . $slash . $path; |
| 167 | + } |
| 168 | + $fullpath = strtr($fullpath, $backslash, $slash); |
| 169 | + $items = explode($slash, $fullpath); |
| 170 | + $new_items = array(); |
| 171 | + foreach ($items as $item) { |
| 172 | + if ($item == null || strpos($item, '.') === 0) { |
| 173 | + if ($item === '..') { |
| 174 | + array_pop($new_items); |
| 175 | + } |
| 176 | + continue; |
| 177 | + } |
| 178 | + $new_items[] = $item; |
| 179 | + } |
| 180 | + $fullpath = implode($slash, $new_items); |
| 181 | + if (!$is_win) { |
| 182 | + $fullpath = $slash . $fullpath; |
| 183 | + } |
| 184 | + $result = $fullpath; |
| 185 | + if ($check_exists) { |
| 186 | + clearstatcache(); |
| 187 | + if (!@file_exists($result)) { |
| 188 | + $result = false; |
| 189 | + } |
| 190 | + } |
| 191 | + return $result; |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +function pot_minify() { |
| 196 | + $pm = new Pot_Minifier(); |
| 197 | + $pm->minify(); |
| 198 | + $pm = null; |
| 199 | + unset($pm); |
| 200 | +} |
| 201 | + |
0 commit comments