Skip to content

Commit

Permalink
Remove support for asset filters.
Browse files Browse the repository at this point in the history
Remove support for Asset.filter.css and Asset.filter.js.  Both of these
features were never fully implemented by CakePHP.  Asset filtering is
now easily accomplished using the Dispatcher filters/middleware system
as well.

Refs #3279
  • Loading branch information
markstory committed Oct 18, 2012
1 parent ac33e82 commit b8814ff
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 161 deletions.
41 changes: 0 additions & 41 deletions lib/Cake/Routing/Filter/AssetDispatcher.php
Expand Up @@ -48,11 +48,6 @@ public function beforeDispatch($event) {
return;
}

if ($result = $this->_filterAsset($event)) {
$event->stopPropagation();
return $result;
}

$assetFile = $this->_getAssetFile($url);
if ($assetFile === null || !file_exists($assetFile)) {
return null;
Expand All @@ -72,42 +67,6 @@ public function beforeDispatch($event) {
return $response;
}

/**
* Checks if the client is requesting a filtered asset and runs the corresponding
* filter if any is configured
*
* @param Cake\Event\Event $event containing the request and response object
* @return Cake\Network\Response if the client is requesting a recognized asset, null otherwise
*/
protected function _filterAsset($event) {
$url = $event->data['request']->url;
$response = $event->data['response'];
$filters = Configure::read('Asset.filter');
$isCss = (
strpos($url, 'ccss/') === 0 ||
preg_match('#^(theme/([^/]+)/ccss/)|(([^/]+)(?<!css)/ccss)/#i', $url)
);
$isJs = (
strpos($url, 'cjs/') === 0 ||
preg_match('#^/((theme/[^/]+)/cjs/)|(([^/]+)(?<!js)/cjs)/#i', $url)
);

if (($isCss && empty($filters['css'])) || ($isJs && empty($filters['js']))) {
$response->statusCode(404);
return $response;
}

if ($isCss) {
include WWW_ROOT . DS . $filters['css'];
return $response;
}

if ($isJs) {
include WWW_ROOT . DS . $filters['js'];
return $response;
}
}

/**
* Builds asset file path based off url
*
Expand Down
19 changes: 0 additions & 19 deletions lib/Cake/Test/TestCase/Routing/DispatcherTest.php
Expand Up @@ -913,25 +913,6 @@ public function testAsset($url, $file) {
$this->assertEquals($expected, $headers['Content-Length']);
}

/**
* test that missing asset processors trigger a 404 with no response body.
*
* @return void
*/
public function testMissingAssetProcessor404() {
$response = $this->getMock('Cake\Network\Response', array('send'));
$Dispatcher = new TestDispatcher();
Configure::write('Asset.filter', array(
'js' => '',
'css' => null
));
Configure::write('Dispatcher.filters', array('AssetDispatcher'));

$request = new Request('ccss/cake.generic.css');
$Dispatcher->dispatch($request, $response);
$this->assertEquals('404', $response->statusCode());
}

/**
* Data provider for cached actions.
*
Expand Down
52 changes: 0 additions & 52 deletions lib/Cake/Test/TestCase/Routing/Filter/AssetDispatcherTest.php
Expand Up @@ -33,54 +33,6 @@ public function tearDown() {
Configure::write('Dispatcher.filters', array());
}

/**
* test that asset filters work for theme and plugin assets
*
* @return void
*/
public function testAssetFilterForThemeAndPlugins() {
$filter = new AssetDispatcher();
$response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
Configure::write('Asset.filter', array(
'js' => '',
'css' => ''
));
App::build(array(
'Plugin' => array(CAKE . 'Test/TestApp/Plugin/'),
'View' => array(CAKE . 'Test/TestApp/View/')
), APP::RESET);

$request = new Request('theme/test_theme/ccss/cake.generic.css');
$event = new Event('DispatcherTest', $this, compact('request', 'response'));
$this->assertSame($response, $filter->beforeDispatch($event));
$this->assertTrue($event->isStopped());

$request = new Request('theme/test_theme/cjs/debug_kit.js');
$event = new Event('DispatcherTest', $this, compact('request', 'response'));
$this->assertSame($response, $filter->beforeDispatch($event));
$this->assertTrue($event->isStopped());

$request = new Request('test_plugin/ccss/cake.generic.css');
$event = new Event('DispatcherTest', $this, compact('request', 'response'));
$this->assertSame($response, $filter->beforeDispatch($event));
$this->assertTrue($event->isStopped());

$request = new Request('test_plugin/cjs/debug_kit.js');
$event = new Event('DispatcherTest', $this, compact('request', 'response'));
$this->assertSame($response, $filter->beforeDispatch($event));
$this->assertTrue($event->isStopped());

$request = new Request('css/ccss/debug_kit.css');
$event = new Event('DispatcherTest', $this, compact('request', 'response'));
$this->assertNull($filter->beforeDispatch($event));
$this->assertFalse($event->isStopped());

$request = new Request('js/cjs/debug_kit.js');
$event = new Event('DispatcherTest', $this, compact('request', 'response'));
$this->assertNull($filter->beforeDispatch($event));
$this->assertFalse($event->isStopped());
}

/**
* Tests that $response->checkNotModified() is called and bypasses
* file dispatching
Expand All @@ -89,10 +41,6 @@ public function testAssetFilterForThemeAndPlugins() {
*/
public function testNotModified() {
$filter = new AssetDispatcher();
Configure::write('Asset.filter', array(
'js' => '',
'css' => ''
));
App::build(array(
'Plugin' => array(CAKE . 'Test/TestApp/Plugin/'),
'View' => array(CAKE . 'Test/TestApp/View/')
Expand Down
38 changes: 0 additions & 38 deletions lib/Cake/Test/TestCase/View/Helper/HtmlHelperTest.php
Expand Up @@ -527,8 +527,6 @@ public function testStyle() {
* @return void
*/
public function testCssLink() {
Configure::write('Asset.filter.css', false);

$result = $this->Html->css('screen');
$expected = array(
'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/.*css\/screen\.css/')
Expand Down Expand Up @@ -560,17 +558,10 @@ public function testCssLink() {
$expected['link']['href'] = 'preg:/http:\/\/.*\/screen\.css\?1234/';
$this->assertTags($result, $expected);

Configure::write('Asset.filter.css', 'css.php');
$result = $this->Html->css('cake.generic');
$expected['link']['href'] = 'preg:/.*ccss\/cake\.generic\.css/';
$this->assertTags($result, $expected);

$result = $this->Html->css('//example.com/css/cake.generic.css');
$expected['link']['href'] = 'preg:/.*example\.com\/css\/cake\.generic\.css/';
$this->assertTags($result, $expected);

Configure::write('Asset.filter.css', false);

$result = explode("\n", trim($this->Html->css(array('cake.generic', 'vendor.generic'))));
$expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
$this->assertTags($result[0], $expected);
Expand Down Expand Up @@ -599,7 +590,6 @@ public function testCssLink() {
* @return void
*/
public function testPluginCssLink() {
Configure::write('Asset.filter.css', false);
Plugin::load('TestPlugin');

$result = $this->Html->css('TestPlugin.test_plugin_asset');
Expand All @@ -619,13 +609,6 @@ public function testPluginCssLink() {
$expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?1234/';
$this->assertTags($result, $expected);

Configure::write('Asset.filter.css', 'css.php');
$result = $this->Html->css('TestPlugin.test_plugin_asset');
$expected['link']['href'] = 'preg:/.*test_plugin\/ccss\/test_plugin_asset\.css/';
$this->assertTags($result, $expected);

Configure::write('Asset.filter.css', false);

$result = explode("\n", trim($this->Html->css(array('TestPlugin.test_plugin_asset', 'TestPlugin.vendor.generic'))));
$expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
$this->assertTags($result[0], $expected);
Expand Down Expand Up @@ -944,27 +927,6 @@ public function testScriptWithBlocks() {
$this->assertNull($result);
}

/**
* Test that Asset.filter.js works.
*
* @return void
*/
public function testScriptAssetFilter() {
Configure::write('Asset.filter.js', 'js.php');

$result = $this->Html->script('jquery-1.3');
$expected = array(
'script' => array('type' => 'text/javascript', 'src' => 'cjs/jquery-1.3.js')
);
$this->assertTags($result, $expected);

$result = $this->Html->script('//example.com/js/jquery-1.3.js');
$expected = array(
'script' => array('type' => 'text/javascript', 'src' => '//example.com/js/jquery-1.3.js')
);
$this->assertTags($result, $expected);
}

/**
* test a script file in the webroot/theme dir.
*
Expand Down
11 changes: 0 additions & 11 deletions lib/Cake/View/Helper/HtmlHelper.php
Expand Up @@ -435,13 +435,6 @@ public function css($path, $rel = null, $options = array()) {
$url = $path;
} else {
$url = $this->assetUrl($path, $options + array('pathPrefix' => CSS_URL, 'ext' => '.css'));

if (Configure::read('Asset.filter.css')) {
$pos = strpos($url, CSS_URL);
if ($pos !== false) {
$url = substr($url, 0, $pos) . 'ccss/' . substr($url, $pos + strlen(CSS_URL));
}
}
}

if ($rel == 'import') {
Expand Down Expand Up @@ -529,10 +522,6 @@ public function script($url, $options = array()) {

if (strpos($url, '//') === false) {
$url = $this->assetUrl($url, $options + array('pathPrefix' => JS_URL, 'ext' => '.js'));

if (Configure::read('Asset.filter.js')) {
$url = str_replace(JS_URL, 'cjs/', $url);
}
}
$attributes = $this->_parseAttributes($options, array('block', 'once'), ' ');
$out = sprintf($this->_tags['javascriptlink'], $url, $attributes);
Expand Down

0 comments on commit b8814ff

Please sign in to comment.