Changeset View
Changeset View
Standalone View
Standalone View
src/config/ArcanistConfigurationSourceList.php
| Show All 9 Lines | public function addSource(ArcanistConfigurationSource $source) { | ||||
| $this->sources[] = $source; | $this->sources[] = $source; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getSources() { | public function getSources() { | ||||
| return $this->sources; | return $this->sources; | ||||
| } | } | ||||
| private function getSourcesWithScopes($scopes) { | |||||
| if ($scopes !== null) { | |||||
| $scopes = array_fuse($scopes); | |||||
| } | |||||
| $results = array(); | |||||
| foreach ($this->getSources() as $source) { | |||||
| if ($scopes !== null) { | |||||
| $scope = $source->getConfigurationSourceScope(); | |||||
| if ($scope === null) { | |||||
| continue; | |||||
| } | |||||
| if (!isset($scopes[$scope])) { | |||||
| continue; | |||||
| } | |||||
| } | |||||
| $results[] = $source; | |||||
| } | |||||
| return $results; | |||||
| } | |||||
| public function getWritableSourceFromScope($scope) { | |||||
| $sources = $this->getSourcesWithScopes(array($scope)); | |||||
| $writable = array(); | |||||
| foreach ($sources as $source) { | |||||
| if (!$source->isWritableConfigurationSource()) { | |||||
| continue; | |||||
| } | |||||
| $writable[] = $source; | |||||
| } | |||||
| if (!$writable) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Unable to write configuration: there is no writable configuration '. | |||||
| 'source in the "%s" scope.', | |||||
| $scope)); | |||||
| } | |||||
| if (count($writable) > 1) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Unable to write configuration: more than one writable source '. | |||||
| 'exists in the "%s" scope.', | |||||
| $scope)); | |||||
| } | |||||
amckinley: When could this happen? | |||||
Done Inline ActionsToday, it can't. You can have multiple sources of the same type with --config-file x --config-file y, but they won't be writable. Soon, you can have multiple writable sources (user, local) but they won't have the same scope. So this is mostly just future-proofing. I think the two most likely ways to get here in the future are:
epriestley: Today, it can't.
You can have multiple sources of the same type with `--config-file x --config… | |||||
| return head($writable); | |||||
| } | |||||
| public function getConfig($key) { | public function getConfig($key) { | ||||
| $option = $this->getConfigOption($key); | $option = $this->getConfigOption($key); | ||||
| $values = $this->getStorageValueList($key); | $values = $this->getStorageValueList($key); | ||||
| return $option->getValueFromStorageValueList($values); | return $option->getValueFromStorageValueList($values); | ||||
| } | } | ||||
| public function getConfigFromScopes($key, array $scopes) { | |||||
| $option = $this->getConfigOption($key); | |||||
| $values = $this->getStorageValueListFromScopes($key, $scopes); | |||||
| return $option->getValueFromStorageValueList($values); | |||||
| } | |||||
| public function getStorageValueList($key) { | public function getStorageValueList($key) { | ||||
| return $this->getStorageValueListFromScopes($key, null); | |||||
| } | |||||
| private function getStorageValueListFromScopes($key, $scopes) { | |||||
| $values = array(); | $values = array(); | ||||
| foreach ($this->getSources() as $source) { | foreach ($this->getSourcesWithScopes($scopes) as $source) { | ||||
| if ($source->hasValueForKey($key)) { | if ($source->hasValueForKey($key)) { | ||||
| $value = $source->getValueForKey($key); | $value = $source->getValueForKey($key); | ||||
| $values[] = new ArcanistConfigurationSourceValue( | $values[] = new ArcanistConfigurationSourceValue( | ||||
| $source, | $source, | ||||
| $source->getValueForKey($key)); | $source->getValueForKey($key)); | ||||
| } | } | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | foreach ($this->getSources() as $source) { | ||||
| $value = $option->getStorageValueFromStringValue($value); | $value = $option->getStorageValueFromStringValue($value); | ||||
| } | } | ||||
| $option->getValueFromStorageValue($value); | $option->getValueFromStorageValue($value); | ||||
| $value_lists[$resolved_key][] = new ArcanistConfigurationSourceValue( | $value_lists[$resolved_key][] = new ArcanistConfigurationSourceValue( | ||||
| $source, | $source, | ||||
| $raw_value); | $raw_value); | ||||
| } catch (Exception $ex) { | } catch (Exception $ex) { | ||||
| throw $ex; | throw new PhutilProxyException( | ||||
| pht( | |||||
| 'Configuration value ("%s") defined in source "%s" is not '. | |||||
| 'valid.', | |||||
| $key, | |||||
| $source->getSourceDisplayName()), | |||||
| $ex); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| // Make sure each value list can be merged. | // Make sure each value list can be merged. | ||||
| foreach ($value_lists as $key => $value_list) { | foreach ($value_lists as $key => $value_list) { | ||||
| try { | try { | ||||
| $options[$key]->getValueFromStorageValueList($value_list); | $options[$key]->getValueFromStorageValueList($value_list); | ||||
| } catch (Exception $ex) { | } catch (Exception $ex) { | ||||
| throw $ex; | throw $ex; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| No newline at end of file | No newline at end of file | ||||
When could this happen?