Path: blob/master/src/applications/config/option/PhabricatorApplicationConfigOptions.php
12256 views
<?php12abstract class PhabricatorApplicationConfigOptions extends Phobject {34abstract public function getName();5abstract public function getDescription();6abstract public function getGroup();7abstract public function getOptions();89public function getIcon() {10return 'fa-sliders';11}1213public function validateOption(PhabricatorConfigOption $option, $value) {14if ($value === $option->getDefault()) {15return;16}1718if ($value === null) {19return;20}2122$type = $option->newOptionType();23if ($type) {24try {25$type->validateStoredValue($option, $value);26$this->didValidateOption($option, $value);27} catch (PhabricatorConfigValidationException $ex) {28throw $ex;29} catch (Exception $ex) {30// If custom validators threw exceptions other than validation31// exceptions, convert them to validation exceptions so we repair the32// configuration and raise an error.33throw new PhabricatorConfigValidationException($ex->getMessage());34}3536return;37}3839if ($option->isCustomType()) {40try {41return $option->getCustomObject()->validateOption($option, $value);42} catch (Exception $ex) {43throw new PhabricatorConfigValidationException($ex->getMessage());44}45} else {46throw new Exception(47pht(48'Unknown configuration option type "%s".',49$option->getType()));50}5152$this->didValidateOption($option, $value);53}5455protected function didValidateOption(56PhabricatorConfigOption $option,57$value) {58// Hook for subclasses to do complex validation.59return;60}6162/**63* Hook to render additional hints based on, e.g., the viewing user, request,64* or other context. For example, this is used to show workspace IDs when65* configuring `asana.workspace-id`.66*67* @param PhabricatorConfigOption Option being rendered.68* @param AphrontRequest Active request.69* @return wild Additional contextual description70* information.71*/72public function renderContextualDescription(73PhabricatorConfigOption $option,74AphrontRequest $request) {75return null;76}7778public function getKey() {79$class = get_class($this);80$matches = null;81if (preg_match('/^Phabricator(.*)ConfigOptions$/', $class, $matches)) {82return strtolower($matches[1]);83}84return strtolower(get_class($this));85}8687final protected function newOption($key, $type, $default) {88return id(new PhabricatorConfigOption())89->setKey($key)90->setType($type)91->setDefault($default)92->setGroup($this);93}9495final public static function loadAll($external_only = false) {96$symbols = id(new PhutilSymbolLoader())97->setAncestorClass(__CLASS__)98->setConcreteOnly(true)99->selectAndLoadSymbols();100101$groups = array();102foreach ($symbols as $symbol) {103if ($external_only && $symbol['library'] == 'phabricator') {104continue;105}106107$obj = newv($symbol['name'], array());108$key = $obj->getKey();109if (isset($groups[$key])) {110$pclass = get_class($groups[$key]);111$nclass = $symbol['name'];112113throw new Exception(114pht(115"Multiple %s subclasses have the same key ('%s'): %s, %s.",116__CLASS__,117$key,118$pclass,119$nclass));120}121$groups[$key] = $obj;122}123124return $groups;125}126127final public static function loadAllOptions($external_only = false) {128$groups = self::loadAll($external_only);129130$options = array();131foreach ($groups as $group) {132foreach ($group->getOptions() as $option) {133$key = $option->getKey();134if (isset($options[$key])) {135throw new Exception(136pht(137"Multiple %s subclasses contain an option named '%s'!",138__CLASS__,139$key));140}141$options[$key] = $option;142}143}144145return $options;146}147148/**149* Deformat a HEREDOC for use in remarkup by converting line breaks to150* spaces.151*/152final protected function deformat($string) {153return preg_replace('/(?<=\S)\n(?=\S)/', ' ', $string);154}155156}157158159