2013-02-13 53 views
2

我正在使用此代碼將css文件緩存到中間自己的CMS中。ob_start緩存CSS

<?php 
ob_start("ob_gzhandler"); 
ob_start("compress"); 
header("Content-type: text/css; charset: UTF-8"); 
header("Cache-Control: must-revalidate"); 
$off = 3600; 
$exp = "Expires: " . gmdate("D, d M Y H:i:s", time() + $off) . " GMT"; 
header($exp); 

function compress($buffer) { 
    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); // remove comments 
    $buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer); // remove tabs, spaces, newlines, etc. 
    return $buffer; 
} 

require_once('style1.css'); 
require_once('style2.css'); 

?> 

這段代碼的一個很大的侷限是我無法將參數傳遞給我的「壓縮」函數。 例如,如果我有一個css文件到另一個目錄中,圖像的相對路徑不會被替換。

當我打電話給我的壓縮函數並使用例如類似的東西時,您是否知道添加參數的方法?

$buffer = str_replace('url("', 'url("'.$directory, $buffer); 

任何建議都非常感謝!


編輯 - >最終的解決方案

@Jack建議後,我來到這個源。

用法:在HTML頁面的標題中加入這一行: <link href="cacheCSS.php" rel="stylesheet" type="text/css" />

ob_start("ob_gzhandler"); 

class CWD { 
    private $path; 

    public function __construct($path=NULL){ 
     if(!isset($path)) $path = ''; 
     $this->setPath($path); 
    } 

    public function setPath($path){ 
     $this->path = $path; 
    } 

    public function getPath() { 
     return $this->path; 
    } 
} 

$directory = new CWD(); 
$compress = function($buffer) use ($directory) { 
    $buffer = str_replace('url("', 'url("'.$directory->getPath(), $buffer); 
    $buffer = str_replace('url(\'', 'url(\''.$directory->getPath(), $buffer); 
    $buffer = preg_replace('#^\s*//.+$#m', "", $buffer); 
    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); 
    $buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer); 
    return $buffer; 
}; 

ob_start($compress); 

header("Content-type: text/css; charset: UTF-8"); 
header("Cache-Control: must-revalidate"); 
$off = 0; # Set to a reaonable value later, say 3600 (1 hr); 
$exp = "Expires: " . gmdate("D, d M Y H:i:s", time() + $off) . " GMT"; 
header($exp); 

/* List of CSS files*/ 
$directory->setPath('path1/'); 
include('path1/style1.css'); 

ob_flush(); 
$directory->setPath('path2/'); 
include('path2/style2.css'); 

ob_flush(); 
$directory->setPath('path3/'); 
include('path3/style3.css'); 

回答

3

由於PHP 5.3,你可以使用閉包來實現:

$directory = 'whatever'; 

$compress = function($buffer) use ($directory) { 
    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); // remove comments 
    $buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer); // remove tabs, spaces, newlines, etc. 

    $buffer = str_replace('url("', 'url("'.$directory, $buffer); 

    return $buffer; 
} 

ob_start($compress); 

更新

如果您的目錄可以更改,您可能需要使用對象:

class CWD 
{ 
    private $path; 

    public function __construct($path) 
    { 
     $this->setPath($path); 
    } 

    public function setPath($path) 
    { 
     $this->path = $path; 
    } 

    public function getPath() 
    { 
     return $this->path; 
    } 
} 

$directory = new CWD('/path/to/directory'); 

$compress = function($buffer) use ($directory) { 
    // ... 
    $buffer = str_replace('url("', 'url("'.$directory->getPath(), $buffer); 
}; 

然後某處你的代碼中你會:

$directory->setPath(); 
echo "whatever css"; 
+0

這可能是一個很好的提示,但路徑的目錄是不固定的,並取決於需要指令。例如: require_once('dir1/style.css'); - > $ directory ='dir1 /'; require_once('dir2/style.css'); - > $ directory ='dir2 /'; 謝謝你的耐心。 – Danilo 2013-02-13 11:28:52

+0

如果路徑要爲每個文件保持固定,您可以在include之上指定它們。 $目錄= 'DIR1 /'; require_once( 'DIR1/style1.css');然後將「use($ directory)」更改爲「use(&$ directory)」以通過引用傳遞該變量。 – Levi 2013-02-13 14:06:39

+0

@Danilo如果它改變了,你可以改爲一個對象。 – 2013-02-13 14:14:26