@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator

Make resolveResources() and packageResources() private on CelerityResourceMap

Summary: Ref T4222. These are the last two "return a big ball of mud" methods. Make the API stronger so I can swap out the implementations.

Test Plan: Reloaded pages.

Reviewers: btrahan, hach-que

Reviewed By: hach-que

CC: aran

Maniphest Tasks: T4222

Differential Revision: https://secure.phabricator.com/D7871

+85 -46
+2 -2
src/applications/phame/skins/PhameBasicTemplateBlogSkin.php
··· 31 31 32 32 $map = CelerityResourceMap::getInstance(); 33 33 $resource_symbol = 'syntax-highlighting-css'; 34 - $resource_uri = $map->getFullyQualifiedURIForSymbol($resource_symbol); 34 + $resource_uri = $map->getURIForSymbol($resource_symbol); 35 35 36 36 $this->cssResources[] = phutil_tag( 37 37 'link', 38 38 array( 39 39 'rel' => 'stylesheet', 40 40 'type' => 'text/css', 41 - 'href' => $resource_uri, 41 + 'href' => PhabricatorEnv::getCDNURI($resource_uri), 42 42 )); 43 43 44 44 $this->cssResources = phutil_implode_html("\n", $this->cssResources);
+60 -15
src/infrastructure/celerity/CelerityResourceMap.php
··· 35 35 return $this; 36 36 } 37 37 38 - public function resolveResources(array $symbols) { 38 + public function getPackagedNamesForSymbols(array $symbols) { 39 + $resolved = $this->resolveResources($symbols); 40 + return $this->packageResources($resolved); 41 + } 42 + 43 + private function resolveResources(array $symbols) { 39 44 $map = array(); 40 45 foreach ($symbols as $symbol) { 41 46 if (!empty($map[$symbol])) { ··· 69 74 return $this; 70 75 } 71 76 72 - public function packageResources(array $resolved_map) { 77 + private function packageResources(array $resolved_map) { 73 78 $packaged = array(); 74 79 $handled = array(); 75 80 foreach ($resolved_map as $symbol => $info) { ··· 87 92 } 88 93 } 89 94 } 90 - return $packaged; 95 + 96 + $names = array(); 97 + foreach ($packaged as $key => $resource) { 98 + if (isset($resource['disk'])) { 99 + $names[] = $resource['disk']; 100 + } else { 101 + $names[] = $key; 102 + } 103 + } 104 + 105 + return $names; 91 106 } 92 107 93 108 public function getResourceDataForName($resource_name) { ··· 132 147 * @param string Resource symbol to lookup. 133 148 * @return int Epoch timestamp of last resource modification. 134 149 */ 135 - public function getModifiedTimeForSymbol($symbol) { 136 - $info = $this->lookupSymbolInformation($symbol); 137 - if ($info) { 138 - $root = dirname(phutil_get_library_root('phabricator')).'/webroot'; 139 - return (int)filemtime($root.$info['disk']); 150 + public function getModifiedTimeForName($name) { 151 + $package_hash = null; 152 + foreach ($this->packageMap['packages'] as $hash => $package) { 153 + if ($package['name'] == $name) { 154 + $package_hash = $hash; 155 + break; 156 + } 157 + } 158 + 159 + $root = dirname(phutil_get_library_root('phabricator')).'/webroot'; 160 + 161 + $mtime = 0; 162 + 163 + if ($package_hash) { 164 + $names = $this->getResourceNamesForPackageHash($package_hash); 165 + foreach ($names as $component_name) { 166 + $info = $this->lookupFileInformation($component_name); 167 + if ($info) { 168 + $mtime = max($mtime, (int)filemtime($root.$info['disk'])); 169 + } 170 + } 171 + } else { 172 + $info = $this->lookupFileInformation($name); 173 + if ($info) { 174 + $root = dirname(phutil_get_library_root('phabricator')).'/webroot'; 175 + $mtime = (int)filemtime($root.$info['disk']); 176 + } 140 177 } 141 - return 0; 178 + 179 + return $mtime; 142 180 } 143 181 144 182 145 183 /** 146 - * Return the fully-qualified, absolute URI for the resource associated with 147 - * a symbol. This method is fairly low-level and ignores packaging. 184 + * Return the absolute URI for the resource associated with a symbol. This 185 + * method is fairly low-level and ignores packaging. 148 186 * 149 187 * @param string Resource symbol to lookup. 150 188 * @return string|null Fully-qualified resource URI, or null if the symbol 151 189 * is unknown. 152 190 */ 153 - public function getFullyQualifiedURIForSymbol($symbol) { 191 + public function getURIForSymbol($symbol) { 154 192 $info = $this->lookupSymbolInformation($symbol); 155 193 if ($info) { 156 194 return idx($info, 'uri'); ··· 160 198 161 199 162 200 /** 163 - * Return the fully-qualified, absolute URI for the resource associated with 164 - * a resource name. This method is fairly low-level and ignores packaging. 201 + * Return the absolute URI for the resource associated with a resource name. 202 + * This method is fairly low-level and ignores packaging. 165 203 * 166 204 * @param string Resource name to lookup. 167 205 * @return string|null Fully-qualified resource URI, or null if the name 168 206 * is unknown. 169 207 */ 170 - public function getFullyQualifiedURIForName($name) { 208 + public function getURIForName($name) { 171 209 $info = $this->lookupFileInformation($name); 172 210 if ($info) { 173 211 return idx($info, 'uri'); 174 212 } 213 + 214 + foreach ($this->packageMap['packages'] as $hash => $package) { 215 + if ($package['name'] == $name) { 216 + return $package['uri']; 217 + } 218 + } 219 + 175 220 return null; 176 221 } 177 222
+1 -1
src/infrastructure/celerity/CelerityResourceTransformer.php
··· 127 127 $uri = $this->rawResourceMap[$uri]['uri']; 128 128 } 129 129 } else if ($this->celerityMap) { 130 - $resource_uri = $this->celerityMap->getFullyQualifiedURIForName($uri); 130 + $resource_uri = $this->celerityMap->getURIForName($uri); 131 131 if ($resource_uri) { 132 132 $uri = $resource_uri; 133 133 }
+21 -27
src/infrastructure/celerity/CelerityStaticResourceResponse.php
··· 62 62 private function resolveResources() { 63 63 if ($this->needsResolve) { 64 64 $map = CelerityResourceMap::getInstance(); 65 - $this->resolved = $map->resolveResources(array_keys($this->symbols)); 66 - $this->packaged = $map->packageResources($this->resolved); 65 + 66 + $symbols = array_keys($this->symbols); 67 + $this->packaged = $map->getPackagedNamesForSymbols($symbols); 68 + 67 69 $this->needsResolve = false; 68 70 } 69 71 return $this; ··· 71 73 72 74 public function renderSingleResource($symbol) { 73 75 $map = CelerityResourceMap::getInstance(); 74 - $resolved = $map->resolveResources(array($symbol)); 75 - $packaged = $map->packageResources($resolved); 76 + $packaged = $map->getPackagedNamesForSymbols(array($symbol)); 76 77 return $this->renderPackagedResources($packaged); 77 78 } 78 79 ··· 80 81 $this->resolveResources(); 81 82 82 83 $resources = array(); 83 - foreach ($this->packaged as $resource) { 84 - if ($resource['type'] == $type) { 85 - $resources[] = $resource; 84 + foreach ($this->packaged as $name) { 85 + $resource_type = CelerityResourceTransformer::getResourceType($name); 86 + if ($resource_type == $type) { 87 + $resources[] = $name; 86 88 } 87 89 } 88 90 ··· 91 93 92 94 private function renderPackagedResources(array $resources) { 93 95 $output = array(); 94 - foreach ($resources as $resource) { 95 - if (isset($this->hasRendered[$resource['uri']])) { 96 + foreach ($resources as $name) { 97 + if (isset($this->hasRendered[$name])) { 96 98 continue; 97 99 } 98 - $this->hasRendered[$resource['uri']] = true; 100 + $this->hasRendered[$name] = true; 99 101 100 - $output[] = $this->renderResource($resource); 102 + $output[] = $this->renderResource($name); 101 103 $output[] = "\n"; 102 104 } 103 105 return phutil_implode_html('', $output); 104 106 } 105 107 106 - private function renderResource(array $resource) { 107 - $uri = $this->getURI($resource); 108 - switch ($resource['type']) { 108 + private function renderResource($name) { 109 + $uri = $this->getURI($name); 110 + $type = CelerityResourceTransformer::getResourceType($name); 111 + switch ($type) { 109 112 case 'css': 110 113 return phutil_tag( 111 114 'link', ··· 232 235 return $response; 233 236 } 234 237 235 - private function getURI($resource) { 236 - $uri = $resource['uri']; 238 + private function getURI($name) { 239 + $map = CelerityResourceMap::getInstance(); 240 + $uri = $map->getURIForName($name); 237 241 238 242 // In developer mode, we dump file modification times into the URI. When a 239 243 // page is reloaded in the browser, any resources brought in by Ajax calls ··· 242 246 // the map script). In production, we can assume the map script gets run 243 247 // after changes, and safely skip this. 244 248 if (PhabricatorEnv::getEnvConfig('phabricator.developer-mode')) { 245 - $root = dirname(phutil_get_library_root('phabricator')).'/webroot'; 246 - if (isset($resource['disk'])) { 247 - $mtime = (int)filemtime($root.$resource['disk']); 248 - } else { 249 - $mtime = 0; 250 - foreach ($resource['symbols'] as $symbol) { 251 - $map = CelerityResourceMap::getInstance(); 252 - $mtime = max($mtime, $map->getModifiedTimeForSymbol($symbol)); 253 - } 254 - } 255 - 249 + $mtime = $map->getModifiedTimeForName($name); 256 250 $uri = preg_replace('@^/res/@', '/res/'.$mtime.'T/', $uri); 257 251 } 258 252
+1 -1
src/infrastructure/celerity/api.php
··· 52 52 function celerity_get_resource_uri($resource) { 53 53 $map = CelerityResourceMap::getInstance(); 54 54 55 - $uri = $map->getFullyQualifiedURIForName($resource); 55 + $uri = $map->getURIForName($resource); 56 56 if ($uri) { 57 57 return $uri; 58 58 }