T_BOOLEAN_AND, // && T_BOOLEAN_OR, // || T_IS_EQUAL, // == T_IS_NOT_EQUAL, // != or <> T_IS_SMALLER_OR_EQUAL, // <= T_IS_GREATER_OR_EQUAL, // >= T_INC, // ++ T_DEC, // -- T_PLUS_EQUAL, // += T_MINUS_EQUAL, // -= T_MUL_EQUAL, // *= T_DIV_EQUAL, // /= T_IS_IDENTICAL, // === T_IS_NOT_IDENTICAL, // !== T_DOUBLE_COLON, // :: T_PAAMAYIM_NEKUDOTAYIM, // :: T_OBJECT_OPERATOR, // -> T_DOLLAR_OPEN_CURLY_BRACES, // ${ T_AND_EQUAL, // &= T_MOD_EQUAL, // %= T_XOR_EQUAL, // ^= T_OR_EQUAL, // |= T_SL, // << T_SR, // >> T_SL_EQUAL, // <<= T_SR_EQUAL, // >>= ]; /** * @param string $source * @param array $options * @return mixed -- string - ok */ public static function minify($source, $options = []) { $min = new self($options); return $min->process($source); } /** * save options * @param array $opts */ public function __construct($opts) { if (isset($opts['min'])) { $this->min = $opts['min'] !== false; // compress yes/no } if (isset($opts['obn']) && !empty($opts['fle'])) { $this->obn = $opts['obn']; //obfuscator call $this->fle = $opts['fle']; //filename to save } } /** * compress code and register identifiers * @param string $php source * @return array */ private function process($php) { $flg = $this->obn ? false : null; //php identifiers' collection if (mb_stripos(pathinfo($this->fle, PATHINFO_EXTENSION), 'php') !== 0) {//not php file $php = $this->Fix($php); //04.2017 } $rlt = $this->Compress($php, $flg); if (!$this->min) { $rlt = $php; // return not-packed } if ($flg) { // source has php tags call_user_func($this->obn, -1, $this->fle); // save filename } return $rlt; } /** * add a space if missing after the tag * @param string $src * @return string */ private function Fix($src) { $t = 'obn, $i, $tokens); //save possible identifier $flg = !is_null($ot) || $flg; // mark php content } } } $ls = ""; } else { if (($token == ";" || $token == ":") && $ls == $token) { $new .= $this->Double($tokens, $i); //VR } else { $new .= $token; $ls = $token; } $iw = true; } } return $new; } /** * check to omit doubled character * @param array $tks tokens * @param int $i current token index * @return string append to output */ private function Double($tks, $i) { $r = ''; if ($tks[$i] == ';') { $j = $i - 1; while ($j > 2 && $tks[$j] != '(') { // find condition beginning $j--; } if ($j > 2) { if (is_array($tks[$j - 1]) && $tks[$j - 1][0] == T_WHITESPACE) { $j--; } if (is_array($tks[$j - 1]) && $tks[$j - 1][0] == T_FOR) { $r = $tks[$i]; // for (;;) } } } return $r; } }