Skip to content
This repository
Browse code

refactoring asset dispatcher filter

  • Loading branch information...
commit 3665a9599670037b52e205e741d916c0d33ff634 1 parent 2818ec6
ceeram ceeram authored

Showing 1 changed file with 44 additions and 34 deletions. Show diff stats Hide diff stats

  1. +44 34 lib/Cake/Routing/Filter/AssetDispatcher.php
78 lib/Cake/Routing/Filter/AssetDispatcher.php
@@ -42,8 +42,6 @@ class AssetDispatcher extends DispatcherFilter {
42 42 */
43 43 public function beforeDispatch($event) {
44 44 $url = $event->data['request']->url;
45   - $response = $event->data['response'];
46   -
47 45 if (strpos($url, '..') !== false || strpos($url, '.') === false) {
48 46 return;
49 47 }
@@ -53,43 +51,26 @@ public function beforeDispatch($event) {
53 51 return $result;
54 52 }
55 53
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;
79 57 }
80 58
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'])) {
87 64 return $response;
88 65 }
  66 +
  67 + $ext = array_pop(explode('.', $url));
  68 + $this->_deliverAsset($response, $assetFile, $ext);
  69 + return $response;
89 70 }
90 71
91 72 /**
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
93 74 * filter if any is configured
94 75 *
95 76 * @param CakeEvent $event containing the request and response object
@@ -111,16 +92,45 @@ protected function _filterAsset($event) {
111 92 if (($isCss && empty($filters['css'])) || ($isJs && empty($filters['js']))) {
112 93 $response->statusCode(404);
113 94 return $response;
114   - } elseif ($isCss) {
  95 + }
  96 +
  97 + if ($isCss) {
115 98 include WWW_ROOT . DS . $filters['css'];
116 99 return $response;
117   - } elseif ($isJs) {
  100 + }
  101 +
  102 + if ($isJs) {
118 103 include WWW_ROOT . DS . $filters['js'];
119 104 return $response;
120 105 }
121 106 }
122 107
123 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 +
  133 +/**
124 134 * Sends an asset file to the client
125 135 *
126 136 * @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.