mirror of
https://we.phorge.it/source/phorge.git
synced 2025-01-09 14:21:02 +01:00
Convert "enum" and "string" config options to new modular option types
Summary: Ref T12845. This moves the "enum" and "string" types to the new code. Test Plan: Set, deleted, and tried to set invalid values for various enum and string config values (header color, mail prefixes, etc) from the CLI and web. Reviewers: chad, amckinley Reviewed By: amckinley Maniphest Tasks: T12845 Differential Revision: https://secure.phabricator.com/D18156
This commit is contained in:
parent
03b6bdde19
commit
9d30d49cfc
10 changed files with 89 additions and 39 deletions
|
@ -2755,6 +2755,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorEmojiRemarkupRule' => 'applications/macro/markup/PhabricatorEmojiRemarkupRule.php',
|
||||
'PhabricatorEmojiTranslation' => 'infrastructure/internationalization/translation/PhabricatorEmojiTranslation.php',
|
||||
'PhabricatorEmptyQueryException' => 'infrastructure/query/PhabricatorEmptyQueryException.php',
|
||||
'PhabricatorEnumConfigType' => 'applications/config/type/PhabricatorEnumConfigType.php',
|
||||
'PhabricatorEnv' => 'infrastructure/env/PhabricatorEnv.php',
|
||||
'PhabricatorEnvTestCase' => 'infrastructure/env/__tests__/PhabricatorEnvTestCase.php',
|
||||
'PhabricatorEpochEditField' => 'applications/transactions/editfield/PhabricatorEpochEditField.php',
|
||||
|
@ -4069,6 +4070,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorStoragePatch' => 'infrastructure/storage/management/PhabricatorStoragePatch.php',
|
||||
'PhabricatorStorageSchemaSpec' => 'infrastructure/storage/schema/PhabricatorStorageSchemaSpec.php',
|
||||
'PhabricatorStorageSetupCheck' => 'applications/config/check/PhabricatorStorageSetupCheck.php',
|
||||
'PhabricatorStringConfigType' => 'applications/config/type/PhabricatorStringConfigType.php',
|
||||
'PhabricatorStringListEditField' => 'applications/transactions/editfield/PhabricatorStringListEditField.php',
|
||||
'PhabricatorStringSetting' => 'applications/settings/setting/PhabricatorStringSetting.php',
|
||||
'PhabricatorSubmitEditField' => 'applications/transactions/editfield/PhabricatorSubmitEditField.php',
|
||||
|
@ -8044,6 +8046,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorEmojiRemarkupRule' => 'PhutilRemarkupRule',
|
||||
'PhabricatorEmojiTranslation' => 'PhutilTranslation',
|
||||
'PhabricatorEmptyQueryException' => 'Exception',
|
||||
'PhabricatorEnumConfigType' => 'PhabricatorTextConfigType',
|
||||
'PhabricatorEnv' => 'Phobject',
|
||||
'PhabricatorEnvTestCase' => 'PhabricatorTestCase',
|
||||
'PhabricatorEpochEditField' => 'PhabricatorEditField',
|
||||
|
@ -9605,6 +9608,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorStoragePatch' => 'Phobject',
|
||||
'PhabricatorStorageSchemaSpec' => 'PhabricatorConfigSchemaSpec',
|
||||
'PhabricatorStorageSetupCheck' => 'PhabricatorSetupCheck',
|
||||
'PhabricatorStringConfigType' => 'PhabricatorTextConfigType',
|
||||
'PhabricatorStringListEditField' => 'PhabricatorEditField',
|
||||
'PhabricatorStringSetting' => 'PhabricatorSetting',
|
||||
'PhabricatorSubmitEditField' => 'PhabricatorEditField',
|
||||
|
|
|
@ -285,7 +285,7 @@ final class PhabricatorConfigEditController
|
|||
$canonical_value = $type->newValueFromRequestValue(
|
||||
$option,
|
||||
$value);
|
||||
$type->validateStoredValue($canonical_value);
|
||||
$type->validateStoredValue($option, $canonical_value);
|
||||
$xaction = $type->newTransaction($option, $canonical_value);
|
||||
} catch (PhabricatorConfigValidationException $ex) {
|
||||
$errors[] = $ex->getMessage();
|
||||
|
@ -347,10 +347,6 @@ final class PhabricatorConfigEditController
|
|||
$set_value = null;
|
||||
|
||||
switch ($type) {
|
||||
case 'string':
|
||||
case 'enum':
|
||||
$set_value = (string)$value;
|
||||
break;
|
||||
case 'list<string>':
|
||||
case 'list<regex>':
|
||||
$set_value = phutil_split_lines(
|
||||
|
@ -441,8 +437,6 @@ final class PhabricatorConfigEditController
|
|||
} else {
|
||||
$type = $option->getType();
|
||||
switch ($type) {
|
||||
case 'string':
|
||||
case 'enum':
|
||||
case 'class':
|
||||
return $value;
|
||||
case 'bool':
|
||||
|
@ -479,9 +473,6 @@ final class PhabricatorConfigEditController
|
|||
} else {
|
||||
$type = $option->getType();
|
||||
switch ($type) {
|
||||
case 'string':
|
||||
$control = id(new AphrontFormTextControl());
|
||||
break;
|
||||
case 'bool':
|
||||
$control = id(new AphrontFormSelectControl())
|
||||
->setOptions(
|
||||
|
@ -491,15 +482,6 @@ final class PhabricatorConfigEditController
|
|||
'false' => idx($option->getBoolOptions(), 1),
|
||||
));
|
||||
break;
|
||||
case 'enum':
|
||||
$options = array_mergev(
|
||||
array(
|
||||
array('' => pht('(Use Default)')),
|
||||
$option->getEnumOptions(),
|
||||
));
|
||||
$control = id(new AphrontFormSelectControl())
|
||||
->setOptions($options);
|
||||
break;
|
||||
case 'class':
|
||||
$symbols = id(new PhutilSymbolLoader())
|
||||
->setType('class')
|
||||
|
|
|
@ -71,9 +71,7 @@ final class PhabricatorConfigManagementSetWorkflow
|
|||
} else {
|
||||
$type = $option->getType();
|
||||
switch ($type) {
|
||||
case 'string':
|
||||
case 'class':
|
||||
case 'enum':
|
||||
$value = (string)$value;
|
||||
break;
|
||||
case 'bool':
|
||||
|
|
|
@ -52,14 +52,6 @@ abstract class PhabricatorApplicationConfigOptions extends Phobject {
|
|||
$option->getKey()));
|
||||
}
|
||||
break;
|
||||
case 'string':
|
||||
if (!is_string($value)) {
|
||||
throw new PhabricatorConfigValidationException(
|
||||
pht(
|
||||
"Option '%s' is of type string, but value is not a string.",
|
||||
$option->getKey()));
|
||||
}
|
||||
break;
|
||||
case 'class':
|
||||
$symbols = id(new PhutilSymbolLoader())
|
||||
->setType('class')
|
||||
|
|
|
@ -297,9 +297,9 @@ EODOC
|
|||
$this->newOption('metamta.user-address-format', 'enum', 'full')
|
||||
->setEnumOptions(
|
||||
array(
|
||||
'short' => 'short',
|
||||
'real' => 'real',
|
||||
'full' => 'full',
|
||||
'short' => pht('Short'),
|
||||
'real' => pht('Real'),
|
||||
'full' => pht('Full'),
|
||||
))
|
||||
->setSummary(pht('Control how Phabricator renders user names in mail.'))
|
||||
->setDescription($address_description)
|
||||
|
|
|
@ -21,12 +21,12 @@ final class PhabricatorUIConfigOptions
|
|||
|
||||
public function getOptions() {
|
||||
$options = array(
|
||||
'blindigo' => 'blindigo',
|
||||
'red' => 'red',
|
||||
'blue' => 'blue',
|
||||
'green' => 'green',
|
||||
'indigo' => 'indigo',
|
||||
'dark' => 'dark',
|
||||
'blindigo' => pht('Blindigo'),
|
||||
'red' => pht('Red'),
|
||||
'blue' => pht('Blue'),
|
||||
'green' => pht('Green'),
|
||||
'indigo' => pht('Indigo'),
|
||||
'dark' => pht('Dark'),
|
||||
);
|
||||
|
||||
$example = <<<EOJSON
|
||||
|
|
43
src/applications/config/type/PhabricatorEnumConfigType.php
Normal file
43
src/applications/config/type/PhabricatorEnumConfigType.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
final class PhabricatorEnumConfigType
|
||||
extends PhabricatorTextConfigType {
|
||||
|
||||
const TYPEKEY = 'enum';
|
||||
|
||||
public function validateStoredValue(
|
||||
PhabricatorConfigOption $option,
|
||||
$value) {
|
||||
|
||||
if (!is_string($value)) {
|
||||
throw $this->newException(
|
||||
pht(
|
||||
'Option "%s" is of type "%s", but the configured value is not '.
|
||||
'a string.',
|
||||
$option->getKey(),
|
||||
$this->getTypeKey()));
|
||||
}
|
||||
|
||||
$map = $option->getEnumOptions();
|
||||
if (!isset($map[$value])) {
|
||||
throw $this->newException(
|
||||
pht(
|
||||
'Option "%s" is of type "%s", but the current value ("%s") is not '.
|
||||
'among the set of valid values: %s.',
|
||||
$option->getKey(),
|
||||
$this->getTypeKey(),
|
||||
$value,
|
||||
implode(', ', array_keys($map))));
|
||||
}
|
||||
}
|
||||
|
||||
protected function newControl(PhabricatorConfigOption $option) {
|
||||
$map = array(
|
||||
'' => pht('(Use Default)'),
|
||||
) + $option->getEnumOptions();
|
||||
|
||||
return id(new AphrontFormSelectControl())
|
||||
->setOptions($map);
|
||||
}
|
||||
|
||||
}
|
22
src/applications/config/type/PhabricatorStringConfigType.php
Normal file
22
src/applications/config/type/PhabricatorStringConfigType.php
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
final class PhabricatorStringConfigType
|
||||
extends PhabricatorTextConfigType {
|
||||
|
||||
const TYPEKEY = 'string';
|
||||
|
||||
public function validateStoredValue(
|
||||
PhabricatorConfigOption $option,
|
||||
$value) {
|
||||
|
||||
if (!is_string($value)) {
|
||||
throw $this->newException(
|
||||
pht(
|
||||
'Option "%s" is of type "%s", but the configured value is not '.
|
||||
'a string.',
|
||||
$option->getKey(),
|
||||
$this->getTypeKey()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -10,6 +10,12 @@ abstract class PhabricatorTextConfigType
|
|||
return (bool)strlen($value);
|
||||
}
|
||||
|
||||
protected function newCanonicalValue(
|
||||
PhabricatorConfigOption $option,
|
||||
$value) {
|
||||
return (string)$value;
|
||||
}
|
||||
|
||||
protected function newHTTPParameterType() {
|
||||
return new AphrontStringHTTPParameterType();
|
||||
}
|
||||
|
|
|
@ -260,7 +260,10 @@ EOHELP
|
|||
->setDescription(
|
||||
pht('Format for inlined or attached patches.'))
|
||||
->setEnumOptions(
|
||||
array('unified' => 'unified', 'git' => 'git')),
|
||||
array(
|
||||
'unified' => pht('Unified'),
|
||||
'git' => pht('Git'),
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue