Permalink
Browse files

Added parameter for css url rewriting and removed buggy etag generation

  • Loading branch information...
ilmich committed Jan 11, 2012
1 parent bc77d32 commit 3ee5666e04343037f866edfd8f0e4e3170179174
Showing with 64 additions and 25 deletions.
  1. +2 −11 classes/class.cms.php
  2. +3 −2 conf/css.php
  3. +59 −12 css.php
View
@@ -167,17 +167,8 @@ public static function getHttpRequest() {
* @param HttpResponse $res the response to send
*/
public static function sendHttpResponse($res) {
- EventManager::getInstance()->getEvent('processResponse')->raise(self::getHttpRequest(),$res);
- $md5 = md5($res->getBody());
- $etag = self::getHttpRequest()->getEtag();
- if ($md5 === $etag) {
- $res = new HttpResponse();
- $res->setStatus(304);
- }else {
- $res->setEtag($md5);
- }
- $res->send();
-
+ EventManager::getInstance()->getEvent('processResponse')->raise(self::getHttpRequest(),$res);
+ $res->send();
}
/**
View
@@ -1,7 +1,8 @@
<?php
- $css['minify'] = true;
+ $css['minify'] = false;
+ $css['etag'] = false;
$css['style'] = array("files" => array(getTemplateDir()."winterplain/style.css"),
- "prependRelativePath" => getTemplateWebRoot()."winterplain/");
+ "prependRelativePath" => getTemplateWebRoot('winterplain'));
return $css;
View
71 css.php
@@ -1,9 +1,11 @@
<?php
require_once 'includes/master.inc.php';
- require_once CLYDEPHP_VENDOR.'minify/CSS.php';
+ require_once CLYDEPHP_VENDOR.'minify/CSS.php';
+ require_once CLYDEPHP_VENDOR.'minify/Minify/CSS/UriRewriter.php';
$req = Cms::getHttpRequest();//get request
+ $resource = null; //empty css object
if ($req->isGet()) { //component accept only get request
$resp = new HttpResponse('text/css'); //new empty response
$filename = String::slugify($req->getParam('css')); //get css name
@@ -17,8 +19,8 @@
$conf = getCmsConfig(null,'css');
- $css = Cms::getCachedObject($filename,'css');
- if (is_null($css)) {
+ $resource = Cms::getCachedObject($filename,'css');
+ if (is_null($resource)) {
if (!isset($conf[$filename])) {
$resp->setStatus(400)
->setBody('Css '.$filename.' not configured')
@@ -32,20 +34,38 @@
->send();
exit(-1);
}
+ $resource = new CssResource();
+ $css = "";
//load all css files
foreach ($conf[$filename]['files'] as $file) {
$css .= @file_get_contents($file);
}
- //minify css
- if (isset($conf['minify']) && $conf['minify'])
- $css = minifyCss($css,$conf[$filename]);
-
- Cms::putObjectInCache($filename, $css,'css');
- }
+
+ //fix css url
+ if (isset($conf[$filename]['prependRelativePath']))
+ $css = Minify_CSS_UriRewriter::prepend($css, $conf[$filename]['prependRelativePath']);
+ //minify css
+ if (isset($conf['minify']) && $conf['minify']) {
+ var_dump($conf[$filename]);
+ $resource->setBody(minifyCss($css));
+ }
+ else {
+ $resource->setBody($css);
+ }
+ $resource->generateEtag();
+
+ Cms::putObjectInCache($filename, $resource,'css');
+ }
+ if (isset($conf['etag']) && $conf['etag']) {
+ if ($resource->getEtag() === $req->getEtag()) {
+ $resp->setStatus(304)->send();
+ exit;
+ }
+ $resp->setEtag($resource->getEtag());
+ }
- $resp->addHeader('Content-Length', strlen($css))
- ->setEtag($cssEtag)
- ->setBody($css);
+ $resp->addHeader('Content-Length', $resource->getBodyLenght())
+ ->setBody($resource->getBody());
Cms::sendHttpResponse($resp);
} else {
@@ -56,4 +76,31 @@
->send();
exit(-1);
}
+
+ class CssResource {
+
+ public $_body = '';
+ public $_etag = '';
+
+ public function getBody() {
+ return String::nullToEmpty($this->_body);
+ }
+
+ public function getEtag() {
+ return String::nullToEmpty($this->_etag);
+ }
+
+ public function setBody($body) {
+ $this->_body = $body;
+ }
+
+ public function generateEtag() {
+ $this->_etag = md5($this->_body);
+ return $this->_etag;
+ }
+
+ public function getBodyLenght() {
+ return strlen($this->_body);
+ }
+ }

0 comments on commit 3ee5666

Please sign in to comment.