From 368b085c94922627d357283a0a68cc6d5b57ed1c Mon Sep 17 00:00:00 2001 From: nate Date: Mon, 31 Dec 2007 05:19:00 +0000 Subject: [PATCH] Adding asset filters for CSS and JS, set using 'Asset.filter.css' and 'Asset.filter.js', fixes #2233 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6287 3807eeeb-6ff5-0310-8944-8be069107fe0 --- app/config/core.php | 9 ++++++++- cake/console/libs/templates/skel/config/core.php | 9 ++++++++- cake/dispatcher.php | 5 ++++- cake/libs/view/helpers/html.php | 5 ++--- cake/libs/view/helpers/javascript.php | 4 ++++ cake/tests/cases/libs/view/helpers/html.test.php | 5 +++++ cake/tests/cases/libs/view/helpers/javascript.test.php | 5 +++++ 7 files changed, 36 insertions(+), 6 deletions(-) diff --git a/app/config/core.php b/app/config/core.php index 516b59ad6..93600ad1b 100644 --- a/app/config/core.php +++ b/app/config/core.php @@ -158,7 +158,14 @@ * * To use, prefix the CSS link URL with '/ccss/' instead of '/css/' or use HtmlHelper::css(). */ - define('COMPRESS_CSS', false); + //Configure::write('Asset.filter.css', 'css.php'); +/** + * Plug in your own custom JavaScript compressor by dropping a script in your webroot to handle the + * output, and setting the config below to the name of the script. + * + * To use, prefix your JavaScript link URLs with '/cjs/' instead of '/js/' or use JavaScriptHelper::link(). + */ + //Configure::write('Asset.filter.js', 'custom_javascript_output_filter.php'); /** * The classname and database used in CakePHP's * access control lists. diff --git a/cake/console/libs/templates/skel/config/core.php b/cake/console/libs/templates/skel/config/core.php index 7e3e40a07..5bd2e9206 100644 --- a/cake/console/libs/templates/skel/config/core.php +++ b/cake/console/libs/templates/skel/config/core.php @@ -154,7 +154,14 @@ * * To use, prefix the CSS link URL with '/ccss/' instead of '/css/' or use HtmlHelper::css(). */ - define('COMPRESS_CSS', false); + //Configure::write('Asset.filter.css', 'css.php'); +/** + * Plug in your own custom JavaScript compressor by dropping a script in your webroot to handle the + * output, and setting the config below to the name of the script. + * + * To use, prefix your JavaScript link URLs with '/cjs/' instead of '/js/' or use JavaScriptHelper::link(). + */ + //Configure::write('Asset.filter.js', 'custom_javascript_output_filter.php'); /** * The classname and database used in CakePHP's * access control lists. diff --git a/cake/dispatcher.php b/cake/dispatcher.php index 7c79317ed..46038ac1b 100644 --- a/cake/dispatcher.php +++ b/cake/dispatcher.php @@ -636,7 +636,10 @@ class Dispatcher extends Object { */ function cached($url) { if (strpos($url, 'ccss/') === 0) { - include WWW_ROOT . DS . 'css.php'; + include WWW_ROOT . DS . Configure::read('Asset.filter.css'); + exit(); + } elseif (strpos($url, 'cjs/') === 0) { + include WWW_ROOT . DS . Configure::read('Asset.filter.js'); exit(); } diff --git a/cake/libs/view/helpers/html.php b/cake/libs/view/helpers/html.php index 47477e1b1..b06194905 100644 --- a/cake/libs/view/helpers/html.php +++ b/cake/libs/view/helpers/html.php @@ -355,10 +355,9 @@ class HtmlHelper extends AppHelper { } } - if (COMPRESS_CSS) { - $path = str_replace('css/', 'ccss/', $path); + if (Configure::read('Asset.filter.css')) { + $path = str_replace(CSS_URL, 'ccss/', $path); } - $url = $this->webroot($path); } diff --git a/cake/libs/view/helpers/javascript.php b/cake/libs/view/helpers/javascript.php index ccff81ef1..a2cfa2698 100644 --- a/cake/libs/view/helpers/javascript.php +++ b/cake/libs/view/helpers/javascript.php @@ -163,6 +163,10 @@ class JavascriptHelper extends AppHelper { if (strpos($url, '://') === false) { $url = $this->webroot(JS_URL . $url); + + if (Configure::read('Asset.filter.js')) { + $url = str_replace(JS_URL, 'cjs/', $url); + } } $out = $this->output(sprintf($this->tags['javascriptlink'], $url)); diff --git a/cake/tests/cases/libs/view/helpers/html.test.php b/cake/tests/cases/libs/view/helpers/html.test.php index 0f9e82f75..76cd899dd 100644 --- a/cake/tests/cases/libs/view/helpers/html.test.php +++ b/cake/tests/cases/libs/view/helpers/html.test.php @@ -123,6 +123,11 @@ class HtmlHelperTest extends UnitTestCase { $result = $this->Html->css('cake.generic'); $this->assertPattern('/^]+href=".*css\/cake\.generic\.css\?[0-9]+"[^<>]+\/>$/', $result); Configure::write('Asset.timestamp', false); + + Configure::write('Asset.filter.css', 'css.php'); + $result = $this->Html->css('cake.generic'); + $this->assertPattern('/^]+href=".*ccss\/cake\.generic\.css"[^<>]+\/>$/', $result); + Configure::write('Asset.filter.css', false); } function testBreadcrumb() { diff --git a/cake/tests/cases/libs/view/helpers/javascript.test.php b/cake/tests/cases/libs/view/helpers/javascript.test.php index 3e3138d8b..c062a4bf8 100644 --- a/cake/tests/cases/libs/view/helpers/javascript.test.php +++ b/cake/tests/cases/libs/view/helpers/javascript.test.php @@ -75,6 +75,11 @@ class JavascriptTest extends UnitTestCase { $result = $this->Javascript->link('jquery-1.1.2'); $this->assertPattern('/^]+src=".*js\/jquery-1\.1\.2\.js\?"[^<>]*>/', $result); Configure::write('Asset.timestamp', false); + + Configure::write('Asset.filter.js', 'js.php'); + $result = $this->Javascript->link('jquery-1.1.2'); + $this->assertPattern('/^]+src=".*cjs\/jquery-1\.1\.2\.js"[^<>]*>/', $result); + Configure::write('Asset.filter.js', false); } function testObjectGeneration() {