ob_start缓存CSS

时间:2013-02-13 02:40:56

标签: php css caching ob-start

我正在使用这个php代码将css文件缓存到中间自己的CMS中。

<?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');

?>

此代码的一大限制是我无法将参数传递给我的“compress”函数。 例如,如果我将css文件放入另一个目录中,则不会替换图像的相对路径。

当我调用我的压缩函数并使用例如类似的东西时,你知道一种添加参数的方法吗?

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

非常感谢任何建议!


编辑 - &gt;最终解决方案

在@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');

1 个答案:

答案 0 :(得分: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";