Jump to content
php.lv forumi

nestrada combine scripts


TheQn

Recommended Posts

kr4 nesaprotu kur kluda combine.php negrib .css faili apvienoties viena faila, bet .js apvienojas viena faila un compressojas, kur var but kluda.

combine.php

<?php
header("Vary: Accept-Encoding");
error_reporting(0);
$cache = true;
$cachedir = dirname(__FILE__) . '/cache';
$cssdir = dirname(__FILE__) . '../../css/';
$jsdir = dirname(__FILE__) . '../../javascript/';
if (!isset($_GET['files']) or strlen($_GET['files']) == 0) {
header('Status: 404 Not Found');
exit();
}
if (!file_exists($cachedir)) {
mkdir($cachedir);
}
$type = $_GET['type'];
switch ($type) {
case 'css':
   	$base = realpath($cssdir);
   	break;
case 'javascript':
   	$base = realpath($jsdir);
   	break;
default:
   	header("HTTP/1.0 503 Not Implemented");
   	exit;
};
$files = explode(',', $_GET['files']);
$types = array_fill(0, sizeof($files), $type);
function addExtension($file, $type)
{
if ($type == 'javascript' && substr($file, -3) !== '.js')
{
   	$file .= '.js';
}
elseif ($type == 'css' && substr($path, -4) !== '.css')
{
   	$file .= '.css';
}
return $file;
}
$files = array_map('addExtension', $files, $types);
$lastmodified = 0;
foreach ($files as $file) {
$path = realpath($base.'/'.$file);
if (!file_exists($path)) {
   	header("HTTP/1.0 404 Not Found");
   	exit;
}
$lastmodified = max($lastmodified, filemtime($path));
}
$md5 = md5($_GET['files']);
$hash = $lastmodified . '-' . $md5;
header("ETag: \"" . $hash . "\"");
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) ==
'"' . $hash . '"') {
header("HTTP/1.0 304 Not Modified");
header('Content-Length: 0');
exit;
}
else
{
$gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
$deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');
$encoding = $gzip ? 'gzip' : ($deflate ? 'deflate' : 'none');
if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') && preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i',
   	$_SERVER['HTTP_USER_AGENT'], $matches)) {
   	$version = floatval($matches[1]);
   	if ($version < 6)
       	$encoding = 'none';
   	if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1'))
       	$encoding = 'none';
}

if ($cache)
{
   	$cachefile = 'cache-' . $md5 . '.' . (($type === 'css') ? 'css' : 'js') . (($encoding !=
       	'none') ? '.' . $encoding : '');
   	if (file_exists($cachedir . '/' . $cachefile) && $lastmodified < filemtime($cachedir . '/' . $cachefile))
   	{
       	if ($encoding != 'none') {
           	header("Content-Encoding: " . $encoding);
       	}
       	header("Content-Type: text/" . $type);
       	header("Content-Length: " . filesize($cachedir . '/' . $cachefile));
       	readfile($cachedir . '/' . $cachefile);
       	exit;
   	}
}
$contents = '';
foreach ($files as $file) {
   	$path = realpath($base.'/'.$file);
   	$contents .= file_get_contents($path);
}
if ($type === 'css')
{
$contents = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $contents);
 $contents = str_replace(array("\r", "\n", "\t", '  ', '   '), '', $contents);
}
else
{
   	require ('classes/jsmin.php');
   	$contents = JSMin::minify($contents);
}
header("Content-Type: text/" . $type);
if (isset($encoding) && $encoding != 'none')
{
   	$contents = gzencode($contents, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
   	header("Content-Encoding: " . $encoding);
   	header('Content-Length: ' . strlen($contents));
   	echo $contents;
}
else
{
   	header('Content-Length: ' . strlen($contents));
   	echo $contents;
}
if ($cache)
   	file_put_contents($cachedir . '/' . $cachefile, $contents);
}
?>

 

.htaccess



RewriteEngine On
RewriteRule ^css/(.*\.css)$ /engine/combine.php?type=css&files=$1
RewriteRule ^javascript/(.*\.js)$ /engine/combine.php?type=javascript&files=$1

 

 

Index.php

<link rel="stylesheet" type="text/css" href="css/reset.css,global.css" />
<script type="text/javascript"> (function() {
var ssync = document.createElement('script');
ssync.type = 'text/javascript';
ssync.async = true;
ssync.src = 'javascript/jquery.js,common.js';
var xsync = document.getElementsByTagName('script')[0];
xsync.parentNode.insertBefore(ssync,xsync); } )();
</script>

Edited by TheQn
Link to comment
Share on other sites

Manuprāt šis jau saknē nav pareizs domu gājiens. Mēs sen sen atpakaļ mēģinājām šādu sistēmu izmantot, bet tā beigu beigās pārvērtās par mērenu murgu ar visām kešotajām versijām, versiju pacelšanām, utt.

 

Daudz labāks risinājums ir publiski likt jau vienu sapakotu failu un atsevišķus specifiskos failus lādēt atsevišķi.

 

Iedomājies situāciju, kad tev ir teiksim 20 dažādi CSS faili, tu viņus atkarībā no vajadzības kombinē teiksim 400 kombinācijās. Pamaini vienu CSS failu un tev kaut kādā brīdī lietotājiem ir atkal jānovelk 400 dažādas failu kombinācijas, kā rezultātā viss ir slikti. Labāk sapako vienu universālu CSS failu jau pirms likšanas publiskajā vidē un paļaujies uz to, ka pārlūks to iekešos. Būs stipri labāk.

 

Es arī nevaru iedomāties iemeslu, kāpēc reset.css saturs nevarētu atrasties global.css iekšienē.

Link to comment
Share on other sites

Sis combine scripts comprese css,js failus tikai vienu reizi, un nem no cache folderi kam lietotajam piemeram cache-fwqfqw432423423f4f2f4.js.gzip nu nelade katram lietotajom compress css strada tika ta <link rel="stylesheet" type="text/css" href="/engine/combine.php?type=css&files=reset,global" /> nezinu kapec vins negrib stradat caur .htaccess sada veida RewriteRule ^css/(.*\.css)$ /engine/combine.php?type=css&files=$1

jo es taisu savu cms man vajag tikai so combine.php lai stradatu.

 

man liekas ta problema ir combine.php scripta jo es nevaru atrast so kludu scripta. busu pateicigs kurs atradis kludu un izlabos.

Edited by TheQn
Link to comment
Share on other sites

vo ko uzrada access

ip - - [11/Aug/2011:14:18:32 +0300] "GET /css/reset.css,global.css HTTP/1.1" 404 20

ip - - [11/Aug/2011:14:18:47 +0300] "GET /css/reset.css,global.css HTTP/1.1" 404 20

ip - - [11/Aug/2011:14:18:50 +0300] "GET /css/reset.css,global.css HTTP/1.1" 404 20

bet nezinu kapec rada 404 ja es ieliku pareizu celu lids css folderim pasa combine.php, un .htaccess bet kludu nevaru atrast.

Link to comment
Share on other sites

si koda dala urzrada Here!

elseif ($type == 'css' && substr($path, -4) !== '.css')
{
   	$file .= '.css';
   	echo "Here";
}

 

si koda dala neko neuzrada

if ($type === 'css')
{
$contents = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $contents);
 $contents = str_replace(array("\r", "\n", "\t", '  ', '   '), '', $contents);
   	$contents = str_replace('{ ','{', $contents);
   	$contents = str_replace(' }','}', $contents);
   	$contents = str_replace('; ',';', $contents);
   	$contents = str_replace(', ',',', $contents);
   	$contents = str_replace(' {','{', $contents);
   	$contents = str_replace('} ','}', $contents);
   	$contents = str_replace(': ',':', $contents);
   	$contents = str_replace(' ,',',', $contents);
   	$contents = str_replace(' ;',';', $contents);
   	echo "here";
}

 

lieku echo kur ir apstrada ar css faili.

 

taisiju vel ta var_dump($cssdir);, var_dump($files);, var_dump($file); vispar neko neuzrada.

Link to comment
Share on other sites

kods ir 100% pareizi uzrakstits, es pats nesaprotu kur kluda, es vel meginaju error_reporting(E_ALL) un error_reporting(8191) kludas koda neradas.

<pre id="line1">string(18) "C:\AppServ\www/css" </pre>

var_dump($types); null uzrada

<pre id="line1">string(20) "reset.css,global.css" vis pareizi</pre>

Edited by TheQn
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...