Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactoring asset dispatcher filter

  • Loading branch information...
commit 3665a9599670037b52e205e741d916c0d33ff634 1 parent 2818ec6
@ceeram ceeram authored
Showing with 44 additions and 34 deletions.
  1. +44 −34 lib/Cake/Routing/Filter/AssetDispatcher.php
View
78 lib/Cake/Routing/Filter/AssetDispatcher.php
@@ -42,8 +42,6 @@ class AssetDispatcher extends DispatcherFilter {
*/
public function beforeDispatch($event) {
$url = $event->data['request']->url;
- $response = $event->data['response'];
-
if (strpos($url, '..') !== false || strpos($url, '.') === false) {
return;
}
@@ -53,43 +51,26 @@ public function beforeDispatch($event) {
return $result;
}
- $pathSegments = explode('.', $url);
- $ext = array_pop($pathSegments);
- $parts = explode('/', $url);
- $assetFile = null;
-
- if ($parts[0] === 'theme') {
- $themeName = $parts[1];
- unset($parts[0], $parts[1]);
- $fileFragment = urldecode(implode(DS, $parts));
- $path = App::themePath($themeName) . 'webroot' . DS;
- if (file_exists($path . $fileFragment)) {
- $assetFile = $path . $fileFragment;
- }
- } else {
- $plugin = Inflector::camelize($parts[0]);
- if (CakePlugin::loaded($plugin)) {
- unset($parts[0]);
- $fileFragment = urldecode(implode(DS, $parts));
- $pluginWebroot = CakePlugin::path($plugin) . 'webroot' . DS;
- if (file_exists($pluginWebroot . $fileFragment)) {
- $assetFile = $pluginWebroot . $fileFragment;
- }
- }
+ $assetFile = $this->_getAssetFile($url);
+ if ($assetFile === null || !file_exists($assetFile)) {
+ return null;
}
- if ($assetFile !== null) {
- $event->stopPropagation();
- $response->modified(filemtime($assetFile));
- if (!$response->checkNotModified($event->data['request'])) {
- $this->_deliverAsset($response, $assetFile, $ext);
- }
+ $response = $event->data['response'];
+ $event->stopPropagation();
+
+ $response->modified(filemtime($assetFile));
+ if ($response->checkNotModified($event->data['request'])) {
return $response;
}
+
+ $ext = array_pop(explode('.', $url));
+ $this->_deliverAsset($response, $assetFile, $ext);
+ return $response;
}
/**
- * Checks if the client is requeting a filtered asset and runs the corresponding
+ * Checks if the client is requesting a filtered asset and runs the corresponding
* filter if any is configured
*
* @param CakeEvent $event containing the request and response object
@@ -111,16 +92,45 @@ protected function _filterAsset($event) {
if (($isCss && empty($filters['css'])) || ($isJs && empty($filters['js']))) {
$response->statusCode(404);
return $response;
- } elseif ($isCss) {
+ }
+
+ if ($isCss) {
include WWW_ROOT . DS . $filters['css'];
return $response;
- } elseif ($isJs) {
+ }
+
+ if ($isJs) {
include WWW_ROOT . DS . $filters['js'];
return $response;
}
}
/**
+ * Builds asset file path based off url
+ *
+ * @param string $url
+ * @return string Absolute path for asset file
+ */
+ protected function _getAssetFile($url) {
+ $parts = explode('/', $url);
+ if ($parts[0] === 'theme') {
+ $themeName = $parts[1];
+ unset($parts[0], $parts[1]);
+ $fileFragment = urldecode(implode(DS, $parts));
+ $path = App::themePath($themeName) . 'webroot' . DS;
+ return $path . $fileFragment;
+ }
+
+ $plugin = Inflector::camelize($parts[0]);
+ if (CakePlugin::loaded($plugin)) {
+ unset($parts[0]);
+ $fileFragment = urldecode(implode(DS, $parts));
+ $pluginWebroot = CakePlugin::path($plugin) . 'webroot' . DS;
+ return $pluginWebroot . $fileFragment;
+ }
+ }
+
+/**
* Sends an asset file to the client
*
* @param CakeResponse $response The response object to use.

0 comments on commit 3665a95

Please sign in to comment.
Something went wrong with that request. Please try again.