Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

refactoring asset dispatcher filter #867

Merged
merged 1 commit into from

2 participants

@ceeram
Collaborator

No description provided.

@lorenzo lorenzo merged commit a355a84 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 24, 2012
  1. @ceeram
This page is out of date. Refresh to see the latest.
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.
Something went wrong with that request. Please try again.