2011-01-25 18:59:31 +01:00
|
|
|
<?php
|
|
|
|
|
2011-07-04 20:22:42 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @group celerity
|
|
|
|
*/
|
2011-01-25 18:59:31 +01:00
|
|
|
final class CelerityResourceMap {
|
|
|
|
|
|
|
|
private static $instance;
|
|
|
|
private $resourceMap;
|
2011-01-30 01:10:05 +01:00
|
|
|
private $packageMap;
|
Bring Javelin into Phabricator via git submodule, not copy-and-paste
Summary:
Javelin is currently embedded in Phabricator via copy-and-paste of prebuilt
packages. This is not so great.
Pull it in as a submodule instead and make all the Phabriator resources declare
proper dependency trees. Add Javelin linting.
Test Plan:
I tried to run through pretty much all the JS functionality on the site. This is
still a high-risk change, but I did a pretty thorough test
Differential: inline comments, revealing diffs, list tokenizers, comment
preview, editing/deleting comments, add review action.
Maniphest: list tokenizer, comment actions
Herald: rule editing, tokenizers, add/remove rows
Reviewed By: tomo
Reviewers: aran, tomo, mroch, jungejason, tuomaspelkonen
CC: aran, tomo, epriestley
Differential Revision: 223
2011-05-04 00:11:55 +02:00
|
|
|
private $reverseMap;
|
2011-01-25 18:59:31 +01:00
|
|
|
|
|
|
|
public static function getInstance() {
|
|
|
|
if (empty(self::$instance)) {
|
|
|
|
self::$instance = new CelerityResourceMap();
|
|
|
|
$root = phutil_get_library_root('phabricator');
|
2012-06-06 17:15:42 +02:00
|
|
|
|
2014-01-01 03:02:35 +01:00
|
|
|
$path = '__celerity_resource_map__.php';
|
2012-06-09 02:14:44 +02:00
|
|
|
$ok = include_once $root.'/'.$path;
|
2011-01-25 18:59:31 +01:00
|
|
|
if (!$ok) {
|
2012-06-06 17:15:42 +02:00
|
|
|
throw new Exception(
|
2014-01-01 03:02:35 +01:00
|
|
|
"Failed to load Celerity resource map!");
|
2011-01-25 18:59:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setResourceMap($resource_map) {
|
|
|
|
$this->resourceMap = $resource_map;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-01-01 03:03:24 +01:00
|
|
|
public function getPackagedNamesForSymbols(array $symbols) {
|
|
|
|
$resolved = $this->resolveResources($symbols);
|
|
|
|
return $this->packageResources($resolved);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function resolveResources(array $symbols) {
|
2011-01-25 18:59:31 +01:00
|
|
|
$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->resourceMap[$symbol])) {
|
|
|
|
throw new Exception(
|
2011-07-16 16:49:04 +02:00
|
|
|
"Attempting to resolve unknown Celerity resource, '{$symbol}'.");
|
2011-01-25 18:59:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$info = $this->resourceMap[$symbol];
|
|
|
|
foreach ($info['requires'] as $requires) {
|
|
|
|
if (!empty($map[$requires])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->resolveResource($map, $requires);
|
|
|
|
}
|
|
|
|
|
|
|
|
$map[$symbol] = $info;
|
|
|
|
}
|
2011-01-31 20:55:26 +01:00
|
|
|
|
2011-01-30 01:10:05 +01:00
|
|
|
public function setPackageMap($package_map) {
|
|
|
|
$this->packageMap = $package_map;
|
2012-04-03 03:35:09 +02:00
|
|
|
return $this;
|
2011-01-30 01:10:05 +01:00
|
|
|
}
|
2011-01-31 20:55:26 +01:00
|
|
|
|
2014-01-01 03:03:24 +01:00
|
|
|
private function packageResources(array $resolved_map) {
|
2011-01-30 01:10:05 +01:00
|
|
|
$packaged = array();
|
|
|
|
$handled = array();
|
|
|
|
foreach ($resolved_map as $symbol => $info) {
|
|
|
|
if (isset($handled[$symbol])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (empty($this->packageMap['reverse'][$symbol])) {
|
|
|
|
$packaged[$symbol] = $info;
|
|
|
|
} else {
|
|
|
|
$package = $this->packageMap['reverse'][$symbol];
|
|
|
|
$package_info = $this->packageMap['packages'][$package];
|
|
|
|
$packaged[$package_info['name']] = $package_info;
|
2011-07-16 16:49:04 +02:00
|
|
|
foreach ($package_info['symbols'] as $packaged_symbol) {
|
|
|
|
$handled[$packaged_symbol] = true;
|
2011-01-30 01:10:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-01 03:03:24 +01:00
|
|
|
|
|
|
|
$names = array();
|
|
|
|
foreach ($packaged as $key => $resource) {
|
|
|
|
if (isset($resource['disk'])) {
|
|
|
|
$names[] = $resource['disk'];
|
|
|
|
} else {
|
|
|
|
$names[] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $names;
|
2011-01-30 01:10:05 +01:00
|
|
|
}
|
2011-01-31 20:55:26 +01:00
|
|
|
|
2014-01-01 03:03:17 +01:00
|
|
|
public function getResourceDataForName($resource_name) {
|
|
|
|
$root = phutil_get_library_root('phabricator');
|
|
|
|
$root = dirname($root).'/webroot/';
|
|
|
|
return Filesystem::readFile($root.$resource_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getResourceNamesForPackageHash($package_hash) {
|
2011-01-30 01:10:05 +01:00
|
|
|
$package = idx($this->packageMap['packages'], $package_hash);
|
|
|
|
if (!$package) {
|
|
|
|
return null;
|
|
|
|
}
|
2011-01-31 20:55:26 +01:00
|
|
|
|
2011-01-30 01:10:05 +01:00
|
|
|
$paths = array();
|
|
|
|
foreach ($package['symbols'] as $symbol) {
|
|
|
|
$paths[] = $this->resourceMap[$symbol]['disk'];
|
|
|
|
}
|
2011-01-31 20:55:26 +01:00
|
|
|
|
2011-01-30 01:10:05 +01:00
|
|
|
return $paths;
|
|
|
|
}
|
2011-01-25 18:59:31 +01:00
|
|
|
|
2014-01-01 03:03:09 +01:00
|
|
|
private function lookupSymbolInformation($symbol) {
|
Bring Javelin into Phabricator via git submodule, not copy-and-paste
Summary:
Javelin is currently embedded in Phabricator via copy-and-paste of prebuilt
packages. This is not so great.
Pull it in as a submodule instead and make all the Phabriator resources declare
proper dependency trees. Add Javelin linting.
Test Plan:
I tried to run through pretty much all the JS functionality on the site. This is
still a high-risk change, but I did a pretty thorough test
Differential: inline comments, revealing diffs, list tokenizers, comment
preview, editing/deleting comments, add review action.
Maniphest: list tokenizer, comment actions
Herald: rule editing, tokenizers, add/remove rows
Reviewed By: tomo
Reviewers: aran, tomo, mroch, jungejason, tuomaspelkonen
CC: aran, tomo, epriestley
Differential Revision: 223
2011-05-04 00:11:55 +02:00
|
|
|
return idx($this->resourceMap, $symbol);
|
|
|
|
}
|
|
|
|
|
2014-01-01 03:02:56 +01:00
|
|
|
private function lookupFileInformation($path) {
|
Bring Javelin into Phabricator via git submodule, not copy-and-paste
Summary:
Javelin is currently embedded in Phabricator via copy-and-paste of prebuilt
packages. This is not so great.
Pull it in as a submodule instead and make all the Phabriator resources declare
proper dependency trees. Add Javelin linting.
Test Plan:
I tried to run through pretty much all the JS functionality on the site. This is
still a high-risk change, but I did a pretty thorough test
Differential: inline comments, revealing diffs, list tokenizers, comment
preview, editing/deleting comments, add review action.
Maniphest: list tokenizer, comment actions
Herald: rule editing, tokenizers, add/remove rows
Reviewed By: tomo
Reviewers: aran, tomo, mroch, jungejason, tuomaspelkonen
CC: aran, tomo, epriestley
Differential Revision: 223
2011-05-04 00:11:55 +02:00
|
|
|
if (empty($this->reverseMap)) {
|
|
|
|
$this->reverseMap = array();
|
|
|
|
foreach ($this->resourceMap as $symbol => $data) {
|
|
|
|
$data['provides'] = $symbol;
|
|
|
|
$this->reverseMap[$data['disk']] = $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return idx($this->reverseMap, $path);
|
|
|
|
}
|
|
|
|
|
2014-01-01 03:02:56 +01:00
|
|
|
|
2014-01-01 03:03:09 +01:00
|
|
|
/**
|
|
|
|
* Get the epoch timestamp of the last modification time of a symbol.
|
|
|
|
*
|
|
|
|
* @param string Resource symbol to lookup.
|
|
|
|
* @return int Epoch timestamp of last resource modification.
|
|
|
|
*/
|
2014-01-01 03:03:24 +01:00
|
|
|
public function getModifiedTimeForName($name) {
|
|
|
|
$package_hash = null;
|
|
|
|
foreach ($this->packageMap['packages'] as $hash => $package) {
|
|
|
|
if ($package['name'] == $name) {
|
|
|
|
$package_hash = $hash;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$root = dirname(phutil_get_library_root('phabricator')).'/webroot';
|
|
|
|
|
|
|
|
$mtime = 0;
|
|
|
|
|
|
|
|
if ($package_hash) {
|
|
|
|
$names = $this->getResourceNamesForPackageHash($package_hash);
|
|
|
|
foreach ($names as $component_name) {
|
|
|
|
$info = $this->lookupFileInformation($component_name);
|
|
|
|
if ($info) {
|
|
|
|
$mtime = max($mtime, (int)filemtime($root.$info['disk']));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$info = $this->lookupFileInformation($name);
|
|
|
|
if ($info) {
|
|
|
|
$root = dirname(phutil_get_library_root('phabricator')).'/webroot';
|
|
|
|
$mtime = (int)filemtime($root.$info['disk']);
|
|
|
|
}
|
2014-01-01 03:03:09 +01:00
|
|
|
}
|
2014-01-01 03:03:24 +01:00
|
|
|
|
|
|
|
return $mtime;
|
2014-01-01 03:03:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-01-01 03:03:24 +01:00
|
|
|
* Return the absolute URI for the resource associated with a symbol. This
|
|
|
|
* method is fairly low-level and ignores packaging.
|
2014-01-01 03:03:09 +01:00
|
|
|
*
|
|
|
|
* @param string Resource symbol to lookup.
|
|
|
|
* @return string|null Fully-qualified resource URI, or null if the symbol
|
|
|
|
* is unknown.
|
|
|
|
*/
|
2014-01-01 03:03:24 +01:00
|
|
|
public function getURIForSymbol($symbol) {
|
2014-01-01 03:03:09 +01:00
|
|
|
$info = $this->lookupSymbolInformation($symbol);
|
|
|
|
if ($info) {
|
|
|
|
return idx($info, 'uri');
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-01 03:02:56 +01:00
|
|
|
/**
|
2014-01-01 03:03:24 +01:00
|
|
|
* Return the absolute URI for the resource associated with a resource name.
|
|
|
|
* This method is fairly low-level and ignores packaging.
|
2014-01-01 03:02:56 +01:00
|
|
|
*
|
|
|
|
* @param string Resource name to lookup.
|
2014-01-01 03:03:09 +01:00
|
|
|
* @return string|null Fully-qualified resource URI, or null if the name
|
|
|
|
* is unknown.
|
2014-01-01 03:02:56 +01:00
|
|
|
*/
|
2014-01-01 03:03:24 +01:00
|
|
|
public function getURIForName($name) {
|
2014-01-01 03:02:56 +01:00
|
|
|
$info = $this->lookupFileInformation($name);
|
|
|
|
if ($info) {
|
|
|
|
return idx($info, 'uri');
|
|
|
|
}
|
2014-01-01 03:03:24 +01:00
|
|
|
|
|
|
|
foreach ($this->packageMap['packages'] as $hash => $package) {
|
|
|
|
if ($package['name'] == $name) {
|
|
|
|
return $package['uri'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-01 03:02:56 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the resource symbols required by a named resource.
|
|
|
|
*
|
|
|
|
* @param string Resource name to lookup.
|
2014-01-01 03:03:09 +01:00
|
|
|
* @return list<string>|null List of required symbols, or null if the name
|
|
|
|
* is unknown.
|
2014-01-01 03:02:56 +01:00
|
|
|
*/
|
|
|
|
public function getRequiredSymbolsForName($name) {
|
|
|
|
$info = $this->lookupFileInformation($name);
|
|
|
|
if ($info) {
|
|
|
|
return idx($info, 'requires', array());
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-01 03:03:09 +01:00
|
|
|
/**
|
|
|
|
* Return the resource name for a given symbol.
|
|
|
|
*
|
|
|
|
* @param string Resource symbol to lookup.
|
|
|
|
* @return string|null Resource name, or null if the symbol is unknown.
|
|
|
|
*/
|
|
|
|
public function getResourceNameForSymbol($symbol) {
|
|
|
|
$info = $this->lookupSymbolInformation($symbol);
|
|
|
|
if ($info) {
|
|
|
|
return idx($info, 'disk');
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-25 18:59:31 +01:00
|
|
|
}
|