- added the csspp library that automatically reduces the size of CSS files; it is activated by prefixing an CSS file URL with /ccss/ instead of /css/ and there's a switch in the /config/core.php to activate it in Controller::cssTag()

- added a new class, File, currently only used by the Log class and Compressed CSS (CCSS) caching; aims to support easy reading and writing of files

git-svn-id: https://svn.cakephp.org/repo/trunk/cake@252 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
pies 2005-06-19 03:35:19 +00:00
parent 7fadee449b
commit a8eed860b2
11 changed files with 1167 additions and 22 deletions

View file

@ -24,4 +24,11 @@
*/ */
define ('DEBUG', 1); define ('DEBUG', 1);
/**
* Compress output CSS (removing comments, whitespace, repeating tags etc.)
* This requires a /var/cache directory to be writable by the web server (caching).
* To use, prefix the CSS link URL with '/ccss/' instead of '/css/' or use Controller::cssTag().
*/
define ('COMPRESS_CSS', false);
?> ?>

View file

@ -88,6 +88,11 @@ define ('MODULES', ROOT.'modules'.DS);
*/ */
define ('PUBLIC', ROOT.'public'.DS); define ('PUBLIC', ROOT.'public'.DS);
/**
* Path to the public directory.
*/
define ('CSS', PUBLIC.'css'.DS);
/** /**
* Path to the scripts direcotry. * Path to the scripts direcotry.
*/ */
@ -118,6 +123,16 @@ define ('MODEL_TESTS', TESTS.'app'.DS.'models'.DS);
*/ */
define ('LIB_TESTS', TESTS.'libs'.DS); define ('LIB_TESTS', TESTS.'libs'.DS);
/**
* Path to the temporary files directory.
*/
define ('TMP', ROOT.'tmp'.DS);
/**
* Path to the cache files directory. It can be shared between hosts in a multi-server setup.
*/
define('CACHE', TMP.'cache'.DS);
/** /**
* Path to the vendors directory. * Path to the vendors directory.
*/ */
@ -130,9 +145,20 @@ define ('VENDORS', ROOT.'vendors'.DS);
*/ */
define ('PEAR', VENDORS.'Pear'.DS); define ('PEAR', VENDORS.'Pear'.DS);
/**
* Full url prefix
*/
define('FULL_BASE_URL', 'http://'.$_SERVER['HTTP_HOST']);
/** /**
* Web path to the public images directory. * Web path to the public images directory.
*/ */
define ('IMAGES', '/img/'); define ('IMAGES_URL', '/img/');
/**
* Web path to the CSS files directory.
*/
define ('CSS_URL', '/css/');
?> ?>

View file

@ -460,7 +460,7 @@ class Controller extends Template
* @return string Formatted IMG tag * @return string Formatted IMG tag
*/ */
function imageTag ($path, $alt=null, $html_options=null) { function imageTag ($path, $alt=null, $html_options=null) {
$url = $this->base.IMAGES.$path; $url = $this->base.IMAGES_URL.$path;
return sprintf(TAG_IMAGE, $url, $alt, $this->parseHtmlOptions($html_options, null, '', ' ')); return sprintf(TAG_IMAGE, $url, $alt, $this->parseHtmlOptions($html_options, null, '', ' '));
} }
@ -516,7 +516,7 @@ class Controller extends Template
* @return string Formatted LINK element. * @return string Formatted LINK element.
*/ */
function cssTag ($path, $rel='stylesheet', $html_options=null) { function cssTag ($path, $rel='stylesheet', $html_options=null) {
$url = "{$this->base}/css/{$path}.css"; $url = "{$this->base}/".(COMPRESS_CSS? 'c': '')."css/{$path}.css";
return sprintf(TAG_CSS, $rel, $url, $this->parseHtmlOptions($html_options, null, '', ' ')); return sprintf(TAG_CSS, $rel, $url, $this->parseHtmlOptions($html_options, null, '', ' '));
} }

40
libs/file.php Normal file
View file

@ -0,0 +1,40 @@
<?php // $Id$
class File
{
var $path = null;
function File ($path)
{
$this->path = $path;
}
function read ()
{
return file_get_contents($this->path);
}
function append ($data)
{
return $this->write($data, 'a');
}
function write ($data, $mode = 'w')
{
if (!($handle = fopen($this->path, $mode)))
{
print ("[File] Could not open {$this->path} with mode $mode!");
return false;
}
if (!fwrite($handle, $data))
return false;
if (!fclose($handle))
return false;
return true;
}
}
?>

View file

@ -29,26 +29,17 @@
* @lastmodified $Date$ * @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License * @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/ */
uses('file');
class Log class Log
{ {
function write($type, $msg) function write($type, $msg)
{ {
$out = date('y-m-d H:i:s').' '.ucfirst($type).': '.$msg."\n"; $filename = LOGS.$type.'.log';
$fn = LOGS.$type.'.log'; $output = date('y-m-d H:i:s').' '.ucfirst($type).': '.$msg."\n";
if (!($log = fopen($fn, 'a'))) $log = new File($filename);
{ return $log->append($output);
print ("[Log] Could not open {$fn}!");
return false;
}
if (!fwrite($log, $out))
return false;
if (!fclose($log))
return false;
return true;
} }
} }

View file

@ -30,6 +30,8 @@
* @license http://www.opensource.org/licenses/mit-license.php The MIT License * @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/ */
uses('log');
/** /**
* Enter description here... * Enter description here...
* *

70
public/css.php Normal file
View file

@ -0,0 +1,70 @@
<?PHP
require(ROOT.'config'.DS.'paths.php');
require(LIBS.'file.php');
require(LIBS.'legacy.php');
function make_clean_css ($path, $name)
{
require_once(VENDORS.'csspp'.DS.'csspp.php');
$data = file_get_contents($path);
$csspp = new csspp();
$output = $csspp->compress($data);
$ratio = 100-(round(strlen($output)/strlen($data), 3)*100);
$output = " /* file: $name, ratio: $ratio% */ " . $output;
return $output;
}
function write_css_cache ($path, $content)
{
if (!is_dir(dirname($path)))
mkdir(dirname($path));
$cache = new File($path);
return $cache->write($content);
}
if (!preg_match('|^ccss/([a-z0-9\/\-\\\]+\.[a-z0-9]+)$|i', $url, $regs))
die('Wrong file name.');
$filename = 'css/'.$regs[1];
$filepath = CSS.$regs[1];
$cachepath = CACHE.'css'.DS.str_replace(array('/','\\'), '-', $regs[1]);
if (!file_exists($filepath))
die('Wrong file name.');
if (file_exists($cachepath))
{
$templateModified = filemtime($filepath);
$cacheModified = filemtime($cachepath);
if ($templateModified > $cacheModified)
{
$output = make_clean_css ($filepath, $filename);
write_css_cache ($cachepath, $output);
}
else
{
$output = file_get_contents($cachepath);
}
}
else
{
$output = make_clean_css ($filepath, $filename);
write_css_cache ($cachepath, $output);
}
header("Date: ".date("D, j M Y G:i:s ", $templateModified).'GMT');
header("Content-Type: text/css");
header("Expires: ".date("D, j M Y G:i:s T", time()+24*60*60));
header("Cache-Control: cache"); // HTTP/1.1
header("Pragma: cache"); // HTTP/1.0
print $output;
?>

View file

@ -30,6 +30,8 @@
* @license http://www.opensource.org/licenses/mit-license.php The MIT License * @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/ */
$url = empty($_GET['url'])? null: $_GET['url'];
session_start(); session_start();
/** /**
@ -41,16 +43,22 @@ if (!defined('DS'))
if (!defined('ROOT')) if (!defined('ROOT'))
define ('ROOT', dirname(dirname(__FILE__)).DS); define ('ROOT', dirname(dirname(__FILE__)).DS);
if (strpos($url, 'ccss/') === 0)
{
include(ROOT.'public'.DS.'css.php');
die;
}
/** /**
* Configuration, directory layout and standard libraries * Configuration, directory layout and standard libraries
*/ */
require_once (ROOT.'config/core.php'); require_once (ROOT.'config/core.php');
require_once (ROOT.'config/paths.php'); require_once (ROOT.'config/paths.php');
require_once (ROOT.'libs/basics.php');
require_once (ROOT.'libs/log.php'); require_once (ROOT.'libs/log.php');
require_once (ROOT.'libs/object.php'); require_once (ROOT.'libs/object.php');
require_once (ROOT.'libs/neat_array.php'); require_once (ROOT.'libs/neat_array.php');
require_once (ROOT.'libs/inflector.php'); require_once (ROOT.'libs/inflector.php');
require_once (ROOT.'libs/basics.php');
DEBUG? error_reporting(E_ALL): error_reporting(0); DEBUG? error_reporting(E_ALL): error_reporting(0);
@ -69,7 +77,6 @@ if (class_exists('DATABASE_CONFIG'))
} }
## RUN THE SCRIPT ## RUN THE SCRIPT
$url = empty($_GET['url'])? null: $_GET['url'];
$DISPATCHER = new Dispatcher (); $DISPATCHER = new Dispatcher ();
$DISPATCHER->dispatch($url); $DISPATCHER->dispatch($url);

1
scripts/test.bat Normal file
View file

@ -0,0 +1 @@
@php -q scripts\test.php --human-readable"

View file

@ -239,11 +239,11 @@ class ControllerTest extends UnitTestCase
function testImageTag() function testImageTag()
{ {
$result = $this->controller->imageTag('foo.gif'); $result = $this->controller->imageTag('foo.gif');
$expected = '<img src="/ease'.IMAGES.'foo.gif" alt="" />'; $expected = '<img src="/ease'.IMAGES_URL.'foo.gif" alt="" />';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->controller->imageTag('bar/baz.gif', 'Foobar', array('class'=>'Zet')); $result = $this->controller->imageTag('bar/baz.gif', 'Foobar', array('class'=>'Zet'));
$expected = '<img src="/ease'.IMAGES.'bar/baz.gif" alt="Foobar" class="Zet" />'; $expected = '<img src="/ease'.IMAGES_URL.'bar/baz.gif" alt="Foobar" class="Zet" />';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }

1001
vendors/csspp/csspp.php vendored Normal file

File diff suppressed because it is too large Load diff