Something went wrong. Try again.
@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
Something went wrong. Try again.
7.1 kB · 266 lines
PHP
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267<?php
/** * Interface to the static resource map, which is a graph of available * resources, resource dependencies, and packaging information. You generally do * not need to invoke it directly; instead, you call higher-level Celerity APIs * and it uses the resource map to satisfy your requests. */final class CelerityResourceMap extends Phobject {
private static $instances = array();
private $resources; private $symbolMap; private $requiresMap; private $packageMap; private $nameMap; private $hashMap; private $componentMap;
public function __construct(CelerityResources $resources) { $this->resources = $resources;
$map = $resources->loadMap(); $this->symbolMap = idx($map, 'symbols', array()); $this->requiresMap = idx($map, 'requires', array()); $this->packageMap = idx($map, 'packages', array()); $this->nameMap = idx($map, 'names', array());
// We derive these reverse maps at runtime.
$this->hashMap = array_flip($this->nameMap); $this->componentMap = array(); foreach ($this->packageMap as $package_name => $symbols) { foreach ($symbols as $symbol) { $this->componentMap[$symbol] = $package_name; } } }
public static function getNamedInstance($name) { if (empty(self::$instances[$name])) { $resources_list = CelerityPhysicalResources::getAll(); if (empty($resources_list[$name])) { throw new Exception( pht( 'No resource source exists with name "%s"!', $name)); }
$instance = new CelerityResourceMap($resources_list[$name]); self::$instances[$name] = $instance; }
return self::$instances[$name]; }
public function getNameMap() { return $this->nameMap; }
public function getSymbolMap() { return $this->symbolMap; }
public function getRequiresMap() { return $this->requiresMap; }
public function getPackageMap() { return $this->packageMap; }
public function getPackagedNamesForSymbols(array $symbols) { $resolved = $this->resolveResources($symbols); return $this->packageResources($resolved); }
private function resolveResources(array $symbols) { $map = array(); foreach ($symbols as $symbol) { if (!empty($map[$symbol])) { continue; } $this->resolveResource($map, $symbol); }
return $map; }
private function resolveResource(array &$map, $symbol) { if (empty($this->symbolMap[$symbol])) { throw new Exception( pht( 'Attempting to resolve unknown resource, "%s".', $symbol)); }
$hash = $this->symbolMap[$symbol];
$map[$symbol] = $hash;
if (isset($this->requiresMap[$hash])) { $requires = $this->requiresMap[$hash]; } else { $requires = array(); }
foreach ($requires as $required_symbol) { if (!empty($map[$required_symbol])) { continue; } $this->resolveResource($map, $required_symbol); } }
private function packageResources(array $resolved_map) { $packaged = array(); $handled = array(); foreach ($resolved_map as $symbol => $hash) { if (isset($handled[$symbol])) { continue; }
if (empty($this->componentMap[$symbol])) { $packaged[] = $this->hashMap[$hash]; } else { $package_name = $this->componentMap[$symbol]; $packaged[] = $package_name;
$package_symbols = $this->packageMap[$package_name]; foreach ($package_symbols as $package_symbol) { $handled[$package_symbol] = true; } } }
return $packaged; }
public function getResourceDataForName($resource_name) { return $this->resources->getResourceData($resource_name); }
public function getResourceNamesForPackageName($package_name) { $package_symbols = idx($this->packageMap, $package_name); if (!$package_symbols) { return null; }
$resource_names = array(); foreach ($package_symbols as $symbol) { $resource_names[] = $this->hashMap[$this->symbolMap[$symbol]]; }
return $resource_names; }
/** * Get the epoch timestamp of the last modification time of a symbol. * * @param string $name Resource symbol to lookup. * @return int Epoch timestamp of last resource modification. */ public function getModifiedTimeForName($name) { if ($this->isPackageResource($name)) { $names = array(); foreach ($this->packageMap[$name] as $symbol) { $names[] = $this->getResourceNameForSymbol($symbol); } } else { $names = array($name); }
$mtime = 0; foreach ($names as $name) { $mtime = max($mtime, $this->resources->getResourceModifiedTime($name)); }
return $mtime; }
/** * Return the absolute URI for the resource associated with a symbol. This * method is fairly low-level and ignores packaging. * * @param string $symbol Resource symbol to lookup. * @return string|null Resource URI, or null if the symbol is unknown. */ public function getURIForSymbol($symbol) { $hash = idx($this->symbolMap, $symbol); return $this->getURIForHash($hash); }
/** * Return the absolute URI for the resource associated with a resource name. * This method is fairly low-level and ignores packaging. * * @param string $name Resource name to lookup. * @return string|null Resource URI, or null if the name is unknown. */ public function getURIForName($name) { $hash = idx($this->nameMap, $name); return $this->getURIForHash($hash); }
public function getHashForName($name) { return idx($this->nameMap, $name); }
/** * Return the absolute URI for a resource, identified by hash. * This method is fairly low-level and ignores packaging. * * @param string $hash Resource hash to lookup. * @return string|null Resource URI, or null if the hash is unknown. */ private function getURIForHash($hash) { if ($hash === null) { return null; } return $this->resources->getResourceURI($hash, $this->hashMap[$hash]); }
/** * Return the resource symbols required by a named resource. * * @param string $name Resource name to lookup. * @return list<string>|null List of required symbols, or null if the name * is unknown. */ public function getRequiredSymbolsForName($name) { $hash = idx($this->nameMap, $name); if ($hash === null) { return null; } return idx($this->requiresMap, $hash, array()); }
/** * Return the resource name for a given symbol. * * @param string $symbol Resource symbol to lookup. * @return string|null Resource name, or null if the symbol is unknown. */ public function getResourceNameForSymbol($symbol) { $hash = idx($this->symbolMap, $symbol); return idx($this->hashMap, $hash); }
public function isPackageResource($name) { return isset($this->packageMap[$name]); }
public function getResourceTypeForName($name) { return $this->resources->getResourceType($name); }
}