Skip to content

Commit 3665a95

Browse files
committed
refactoring asset dispatcher filter
1 parent 2818ec6 commit 3665a95

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

lib/Cake/Routing/Filter/AssetDispatcher.php

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ class AssetDispatcher extends DispatcherFilter {
4242
*/
4343
public function beforeDispatch($event) {
4444
$url = $event->data['request']->url;
45-
$response = $event->data['response'];
46-
4745
if (strpos($url, '..') !== false || strpos($url, '.') === false) {
4846
return;
4947
}
@@ -53,43 +51,26 @@ public function beforeDispatch($event) {
5351
return $result;
5452
}
5553

56-
$pathSegments = explode('.', $url);
57-
$ext = array_pop($pathSegments);
58-
$parts = explode('/', $url);
59-
$assetFile = null;
60-
61-
if ($parts[0] === 'theme') {
62-
$themeName = $parts[1];
63-
unset($parts[0], $parts[1]);
64-
$fileFragment = urldecode(implode(DS, $parts));
65-
$path = App::themePath($themeName) . 'webroot' . DS;
66-
if (file_exists($path . $fileFragment)) {
67-
$assetFile = $path . $fileFragment;
68-
}
69-
} else {
70-
$plugin = Inflector::camelize($parts[0]);
71-
if (CakePlugin::loaded($plugin)) {
72-
unset($parts[0]);
73-
$fileFragment = urldecode(implode(DS, $parts));
74-
$pluginWebroot = CakePlugin::path($plugin) . 'webroot' . DS;
75-
if (file_exists($pluginWebroot . $fileFragment)) {
76-
$assetFile = $pluginWebroot . $fileFragment;
77-
}
78-
}
54+
$assetFile = $this->_getAssetFile($url);
55+
if ($assetFile === null || !file_exists($assetFile)) {
56+
return null;
7957
}
8058

81-
if ($assetFile !== null) {
82-
$event->stopPropagation();
83-
$response->modified(filemtime($assetFile));
84-
if (!$response->checkNotModified($event->data['request'])) {
85-
$this->_deliverAsset($response, $assetFile, $ext);
86-
}
59+
$response = $event->data['response'];
60+
$event->stopPropagation();
61+
62+
$response->modified(filemtime($assetFile));
63+
if ($response->checkNotModified($event->data['request'])) {
8764
return $response;
8865
}
66+
67+
$ext = array_pop(explode('.', $url));
68+
$this->_deliverAsset($response, $assetFile, $ext);
69+
return $response;
8970
}
9071

9172
/**
92-
* Checks if the client is requeting a filtered asset and runs the corresponding
73+
* Checks if the client is requesting a filtered asset and runs the corresponding
9374
* filter if any is configured
9475
*
9576
* @param CakeEvent $event containing the request and response object
@@ -111,15 +92,44 @@ protected function _filterAsset($event) {
11192
if (($isCss && empty($filters['css'])) || ($isJs && empty($filters['js']))) {
11293
$response->statusCode(404);
11394
return $response;
114-
} elseif ($isCss) {
95+
}
96+
97+
if ($isCss) {
11598
include WWW_ROOT . DS . $filters['css'];
11699
return $response;
117-
} elseif ($isJs) {
100+
}
101+
102+
if ($isJs) {
118103
include WWW_ROOT . DS . $filters['js'];
119104
return $response;
120105
}
121106
}
122107

108+
/**
109+
* Builds asset file path based off url
110+
*
111+
* @param string $url
112+
* @return string Absolute path for asset file
113+
*/
114+
protected function _getAssetFile($url) {
115+
$parts = explode('/', $url);
116+
if ($parts[0] === 'theme') {
117+
$themeName = $parts[1];
118+
unset($parts[0], $parts[1]);
119+
$fileFragment = urldecode(implode(DS, $parts));
120+
$path = App::themePath($themeName) . 'webroot' . DS;
121+
return $path . $fileFragment;
122+
}
123+
124+
$plugin = Inflector::camelize($parts[0]);
125+
if (CakePlugin::loaded($plugin)) {
126+
unset($parts[0]);
127+
$fileFragment = urldecode(implode(DS, $parts));
128+
$pluginWebroot = CakePlugin::path($plugin) . 'webroot' . DS;
129+
return $pluginWebroot . $fileFragment;
130+
}
131+
}
132+
123133
/**
124134
* Sends an asset file to the client
125135
*

0 commit comments

Comments
 (0)