PHP Classes

File: classes/autoinclude/compress.php

Recommend this page to a friend!
  Classes of bassam alessawi   lezaz   classes/autoinclude/compress.php   Download  
File: classes/autoinclude/compress.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: lezaz
Template engine that compiles tags into PHP code
Author: By
Last change:
Date: 8 years ago
Size: 4,524 bytes
 

Contents

Class file image Download
<?php

function compressJS($dir, $compress = 0, $sort = 0) {
    global
$lezaz;
   
$pattern = "*.js";
   
$vars_dir_path = $lezaz->lezaz_path($dir) . '/';
   
$vars_dir_link = $lezaz->lezaz_path($dir, 1) . '/';

   
$glop = glob($vars_dir_path . $pattern);
    if (
$sort)
       
arsort($glop);
    else
       
asort($glop);
    foreach (
$glop as $filename) {
       
$fn = basename($filename);
        if (
strpos('XXX' . $filename, 'Xall.js.'))
            continue;
        if (
$compress == 1 || $compress == 2) {
           
$Xfile.= hash_file('md5', $filename);
        } else {
           
$return.=" <script type=\"text/javascript\" src=\"{$vars_dir_link}{$fn}\"></script> \n";
        }
    }
    if (
file_exists($vars_dir_path . 'all.js.' . md5($Xfile) . $compress . '.js'))
        return
" <script type=\"text/javascript\" src=\"" . $vars_dir_link . 'all.js.' . md5($Xfile) . $compress . '.js' . "\"></script> \n";
    if (
$return)
        return
$return;



   
array_map('unlink', glob($vars_dir_path . "all.js.*.js")); // delete all mini files created by lezaz

   
foreach (glob($vars_dir_path . $pattern) as $filename) {
       
$fn = basename($filename);
        if (
$compress == '1') {
           
$jsall.= "\n\n\n/*$fn*/\n========================\n" . file_get_contents($filename);
        } else if (
$compress == '2') {
           
$jsau = addslashes(file_get_contents($filename));
           
$packer = new JavaScriptPacker($jsau);
           
$jsau = $packer->pack();
           
$jsall.= "\n\n\n/*$fn*/\n========================\n" . $jsau;
        }
    }
   
$lezaz->file->write($vars_dir_path . 'all.js.' . md5($Xfile) . $compress . '.js', $jsall);
    return
" <script type=\"text/javascript\" src=\"" . $vars_dir_link . 'all.js.' . md5($Xfile) . $compress . '.js' . "\"></script> \n";
}

function
compressCSS($dir, $compress = 0, $sort = 0) {
    global
$lezaz;
   
$pattern = "*.css";
   
$vars_dir_path = $lezaz->lezaz_path($dir) . '/';
   
$vars_dir_link = $lezaz->lezaz_path($dir, 1) . '/';

   
$glop = glob($vars_dir_path . $pattern);
    if (
$sort)
       
arsort($glop);
    else
       
asort($glop);
    foreach (
$glop as $filename) {
       
$fn = basename($filename);
        if (
strpos('XXX' . $filename, 'Xall.css.'))
            continue;
        if (
$compress == 1 || $compress == 2) {
           
$Xfile.= hash_file('md5', $filename);
        } else {
           
$return.=" <link rel=\"stylesheet\" type=\"text/css\" href=\"{$vars_dir_link}{$fn}\"/>\n";
        }
    }
    if (
file_exists($vars_dir_path . 'all.css.' . md5($Xfile) . $compress . '.css'))
        return
" <link rel=\"stylesheet\" type=\"text/css\" href=\"" . $vars_dir_link . 'all.css.' . md5($Xfile) . $compress . '.css' . "\"/>\n";
    if (
$return)
        return
$return;



   
array_map('unlink', glob($vars_dir_path . "all.css.*.css")); // delete all mini files created by lezaz

   
foreach (glob($vars_dir_path . $pattern) as $filename) {
       
$fn = basename($filename);
        if (
$compress == '1') {
           
$cssall.= "\n\n\n/*$fn*/\n========================\n" . file_get_contents($filename);
        } else if (
$compress == '2') {
           
$cssau = compress_css($filename);
           
$cssall.= "\n\n\n/*$fn*/\n========================\n" . $cssau;
        }
    }
   
$lezaz->file->write($vars_dir_path . 'all.css.' . md5($Xfile) . $compress . '.css', get_import($cssall));
    return
"<link rel=\"stylesheet\" type=\"text/css\" href=\"" . $vars_dir_link . 'all.css.' . md5($Xfile) . $compress . '.css' . "\"/> \n";
   
// <link rel=\"stylesheet\" type=\"text/css\" href=\"' . $vars_dir_link . '$it\"/>";'));
   
asort($files);
   
arsort($files);
}

function
compress_css($file_name) {
   
$cssx = file_get_contents($file_name);
   
$cssx = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', ' ', $cssx);
    return
str_ireplace('{}', '{ }', str_ireplace(array('; ', ' }', '{ ', ': ', ' {', ' '), array(';', '}', '{', ':', '{', ' '), str_ireplace(array("\r\n", "\r", "\n", "\t", ' '), ' ', $cssx)));
}

function
get_import($css) {
   
preg_match_all('/\@font\-face[^\}]*\}/', $css, $p1);
   
$css = preg_replace('/\@font\-face[^\}]*\}/', '', $css);
   
$css = implode("\n", $p1[0]) . "\n" . $css;

   
preg_match_all('/\@import[^\;]*\;/', $css, $p);
   
$css = preg_replace('/\@import[^\;]*\;/', '', $css);
   
$css = implode("\n", $p[0]) . "\n" . $css;

    return
$css;
}