1
0
Fork 0
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:
epriestley 2017-06-26 09:05:26 -07:00
parent 03b6bdde19
commit 9d30d49cfc
10 changed files with 89 additions and 39 deletions

View file

@ -2755,6 +2755,7 @@ phutil_register_library_map(array(
'PhabricatorEmojiRemarkupRule' => 'applications/macro/markup/PhabricatorEmojiRemarkupRule.php', 'PhabricatorEmojiRemarkupRule' => 'applications/macro/markup/PhabricatorEmojiRemarkupRule.php',
'PhabricatorEmojiTranslation' => 'infrastructure/internationalization/translation/PhabricatorEmojiTranslation.php', 'PhabricatorEmojiTranslation' => 'infrastructure/internationalization/translation/PhabricatorEmojiTranslation.php',
'PhabricatorEmptyQueryException' => 'infrastructure/query/PhabricatorEmptyQueryException.php', 'PhabricatorEmptyQueryException' => 'infrastructure/query/PhabricatorEmptyQueryException.php',
'PhabricatorEnumConfigType' => 'applications/config/type/PhabricatorEnumConfigType.php',
'PhabricatorEnv' => 'infrastructure/env/PhabricatorEnv.php', 'PhabricatorEnv' => 'infrastructure/env/PhabricatorEnv.php',
'PhabricatorEnvTestCase' => 'infrastructure/env/__tests__/PhabricatorEnvTestCase.php', 'PhabricatorEnvTestCase' => 'infrastructure/env/__tests__/PhabricatorEnvTestCase.php',
'PhabricatorEpochEditField' => 'applications/transactions/editfield/PhabricatorEpochEditField.php', 'PhabricatorEpochEditField' => 'applications/transactions/editfield/PhabricatorEpochEditField.php',
@ -4069,6 +4070,7 @@ phutil_register_library_map(array(
'PhabricatorStoragePatch' => 'infrastructure/storage/management/PhabricatorStoragePatch.php', 'PhabricatorStoragePatch' => 'infrastructure/storage/management/PhabricatorStoragePatch.php',
'PhabricatorStorageSchemaSpec' => 'infrastructure/storage/schema/PhabricatorStorageSchemaSpec.php', 'PhabricatorStorageSchemaSpec' => 'infrastructure/storage/schema/PhabricatorStorageSchemaSpec.php',
'PhabricatorStorageSetupCheck' => 'applications/config/check/PhabricatorStorageSetupCheck.php', 'PhabricatorStorageSetupCheck' => 'applications/config/check/PhabricatorStorageSetupCheck.php',
'PhabricatorStringConfigType' => 'applications/config/type/PhabricatorStringConfigType.php',
'PhabricatorStringListEditField' => 'applications/transactions/editfield/PhabricatorStringListEditField.php', 'PhabricatorStringListEditField' => 'applications/transactions/editfield/PhabricatorStringListEditField.php',
'PhabricatorStringSetting' => 'applications/settings/setting/PhabricatorStringSetting.php', 'PhabricatorStringSetting' => 'applications/settings/setting/PhabricatorStringSetting.php',
'PhabricatorSubmitEditField' => 'applications/transactions/editfield/PhabricatorSubmitEditField.php', 'PhabricatorSubmitEditField' => 'applications/transactions/editfield/PhabricatorSubmitEditField.php',
@ -8044,6 +8046,7 @@ phutil_register_library_map(array(
'PhabricatorEmojiRemarkupRule' => 'PhutilRemarkupRule', 'PhabricatorEmojiRemarkupRule' => 'PhutilRemarkupRule',
'PhabricatorEmojiTranslation' => 'PhutilTranslation', 'PhabricatorEmojiTranslation' => 'PhutilTranslation',
'PhabricatorEmptyQueryException' => 'Exception', 'PhabricatorEmptyQueryException' => 'Exception',
'PhabricatorEnumConfigType' => 'PhabricatorTextConfigType',
'PhabricatorEnv' => 'Phobject', 'PhabricatorEnv' => 'Phobject',
'PhabricatorEnvTestCase' => 'PhabricatorTestCase', 'PhabricatorEnvTestCase' => 'PhabricatorTestCase',
'PhabricatorEpochEditField' => 'PhabricatorEditField', 'PhabricatorEpochEditField' => 'PhabricatorEditField',
@ -9605,6 +9608,7 @@ phutil_register_library_map(array(
'PhabricatorStoragePatch' => 'Phobject', 'PhabricatorStoragePatch' => 'Phobject',
'PhabricatorStorageSchemaSpec' => 'PhabricatorConfigSchemaSpec', 'PhabricatorStorageSchemaSpec' => 'PhabricatorConfigSchemaSpec',
'PhabricatorStorageSetupCheck' => 'PhabricatorSetupCheck', 'PhabricatorStorageSetupCheck' => 'PhabricatorSetupCheck',
'PhabricatorStringConfigType' => 'PhabricatorTextConfigType',
'PhabricatorStringListEditField' => 'PhabricatorEditField', 'PhabricatorStringListEditField' => 'PhabricatorEditField',
'PhabricatorStringSetting' => 'PhabricatorSetting', 'PhabricatorStringSetting' => 'PhabricatorSetting',
'PhabricatorSubmitEditField' => 'PhabricatorEditField', 'PhabricatorSubmitEditField' => 'PhabricatorEditField',

View file

@ -285,7 +285,7 @@ final class PhabricatorConfigEditController
$canonical_value = $type->newValueFromRequestValue( $canonical_value = $type->newValueFromRequestValue(
$option, $option,
$value); $value);
$type->validateStoredValue($canonical_value); $type->validateStoredValue($option, $canonical_value);
$xaction = $type->newTransaction($option, $canonical_value); $xaction = $type->newTransaction($option, $canonical_value);
} catch (PhabricatorConfigValidationException $ex) { } catch (PhabricatorConfigValidationException $ex) {
$errors[] = $ex->getMessage(); $errors[] = $ex->getMessage();
@ -347,10 +347,6 @@ final class PhabricatorConfigEditController
$set_value = null; $set_value = null;
switch ($type) { switch ($type) {
case 'string':
case 'enum':
$set_value = (string)$value;
break;
case 'list<string>': case 'list<string>':
case 'list<regex>': case 'list<regex>':
$set_value = phutil_split_lines( $set_value = phutil_split_lines(
@ -441,8 +437,6 @@ final class PhabricatorConfigEditController
} else { } else {
$type = $option->getType(); $type = $option->getType();
switch ($type) { switch ($type) {
case 'string':
case 'enum':
case 'class': case 'class':
return $value; return $value;
case 'bool': case 'bool':
@ -479,9 +473,6 @@ final class PhabricatorConfigEditController
} else { } else {
$type = $option->getType(); $type = $option->getType();
switch ($type) { switch ($type) {
case 'string':
$control = id(new AphrontFormTextControl());
break;
case 'bool': case 'bool':
$control = id(new AphrontFormSelectControl()) $control = id(new AphrontFormSelectControl())
->setOptions( ->setOptions(
@ -491,15 +482,6 @@ final class PhabricatorConfigEditController
'false' => idx($option->getBoolOptions(), 1), 'false' => idx($option->getBoolOptions(), 1),
)); ));
break; break;
case 'enum':
$options = array_mergev(
array(
array('' => pht('(Use Default)')),
$option->getEnumOptions(),
));
$control = id(new AphrontFormSelectControl())
->setOptions($options);
break;
case 'class': case 'class':
$symbols = id(new PhutilSymbolLoader()) $symbols = id(new PhutilSymbolLoader())
->setType('class') ->setType('class')

View file

@ -71,9 +71,7 @@ final class PhabricatorConfigManagementSetWorkflow
} else { } else {
$type = $option->getType(); $type = $option->getType();
switch ($type) { switch ($type) {
case 'string':
case 'class': case 'class':
case 'enum':
$value = (string)$value; $value = (string)$value;
break; break;
case 'bool': case 'bool':

View file

@ -52,14 +52,6 @@ abstract class PhabricatorApplicationConfigOptions extends Phobject {
$option->getKey())); $option->getKey()));
} }
break; 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': case 'class':
$symbols = id(new PhutilSymbolLoader()) $symbols = id(new PhutilSymbolLoader())
->setType('class') ->setType('class')

View file

@ -297,9 +297,9 @@ EODOC
$this->newOption('metamta.user-address-format', 'enum', 'full') $this->newOption('metamta.user-address-format', 'enum', 'full')
->setEnumOptions( ->setEnumOptions(
array( array(
'short' => 'short', 'short' => pht('Short'),
'real' => 'real', 'real' => pht('Real'),
'full' => 'full', 'full' => pht('Full'),
)) ))
->setSummary(pht('Control how Phabricator renders user names in mail.')) ->setSummary(pht('Control how Phabricator renders user names in mail.'))
->setDescription($address_description) ->setDescription($address_description)

View file

@ -21,12 +21,12 @@ final class PhabricatorUIConfigOptions
public function getOptions() { public function getOptions() {
$options = array( $options = array(
'blindigo' => 'blindigo', 'blindigo' => pht('Blindigo'),
'red' => 'red', 'red' => pht('Red'),
'blue' => 'blue', 'blue' => pht('Blue'),
'green' => 'green', 'green' => pht('Green'),
'indigo' => 'indigo', 'indigo' => pht('Indigo'),
'dark' => 'dark', 'dark' => pht('Dark'),
); );
$example = <<<EOJSON $example = <<<EOJSON

View 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);
}
}

View 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()));
}
}
}

View file

@ -10,6 +10,12 @@ abstract class PhabricatorTextConfigType
return (bool)strlen($value); return (bool)strlen($value);
} }
protected function newCanonicalValue(
PhabricatorConfigOption $option,
$value) {
return (string)$value;
}
protected function newHTTPParameterType() { protected function newHTTPParameterType() {
return new AphrontStringHTTPParameterType(); return new AphrontStringHTTPParameterType();
} }

View file

@ -260,7 +260,10 @@ EOHELP
->setDescription( ->setDescription(
pht('Format for inlined or attached patches.')) pht('Format for inlined or attached patches.'))
->setEnumOptions( ->setEnumOptions(
array('unified' => 'unified', 'git' => 'git')), array(
'unified' => pht('Unified'),
'git' => pht('Git'),
)),
); );
} }