Skip to content

Commit

Permalink
refactoring asset dispatcher filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ceeram committed Sep 24, 2012
1 parent 2818ec6 commit 3665a95
Showing 1 changed file with 44 additions and 34 deletions.
78 changes: 44 additions & 34 deletions lib/Cake/Routing/Filter/AssetDispatcher.php
Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -111,15 +92,44 @@ 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
*
Expand Down

0 comments on commit 3665a95

Please sign in to comment.