|
| 1 | +<?php |
| 2 | + |
| 3 | +final class PhabricatorClassConfigType |
| 4 | + extends PhabricatorTextConfigType { |
| 5 | + |
| 6 | + const TYPEKEY = 'class'; |
| 7 | + |
| 8 | + public function validateStoredValue( |
| 9 | + PhabricatorConfigOption $option, |
| 10 | + $value) { |
| 11 | + |
| 12 | + if (!is_string($value)) { |
| 13 | + throw $this->newException( |
| 14 | + pht( |
| 15 | + 'Option "%s" is of type "%s", but the configured value is not '. |
| 16 | + 'a string.', |
| 17 | + $option->getKey(), |
| 18 | + $this->getTypeKey())); |
| 19 | + } |
| 20 | + |
| 21 | + $base = $option->getBaseClass(); |
| 22 | + $map = $this->getClassOptions($option); |
| 23 | + |
| 24 | + try { |
| 25 | + $ok = class_exists($value); |
| 26 | + } catch (Exception $ex) { |
| 27 | + $ok = false; |
| 28 | + } |
| 29 | + |
| 30 | + if (!$ok) { |
| 31 | + throw $this->newException( |
| 32 | + pht( |
| 33 | + 'Option "%s" is of type "%s", but the configured value is not the '. |
| 34 | + 'name of a known class. Valid selections are: %s.', |
| 35 | + $option->getKey(), |
| 36 | + $this->getTypeKey(), |
| 37 | + implode(', ', array_keys($map)))); |
| 38 | + } |
| 39 | + |
| 40 | + if (!isset($map[$value])) { |
| 41 | + throw $this->newException( |
| 42 | + pht( |
| 43 | + 'Option "%s" is of type "%s", but the current value ("%s") is not '. |
| 44 | + 'a known, concrete subclass of base class "%s". Valid selections '. |
| 45 | + 'are: %s.', |
| 46 | + $option->getKey(), |
| 47 | + $this->getTypeKey(), |
| 48 | + $value, |
| 49 | + $base, |
| 50 | + implode(', ', array_keys($map)))); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + protected function newControl(PhabricatorConfigOption $option) { |
| 55 | + $map = array( |
| 56 | + '' => pht('(Use Default)'), |
| 57 | + ) + $this->getClassOptions($option); |
| 58 | + |
| 59 | + return id(new AphrontFormSelectControl()) |
| 60 | + ->setOptions($map); |
| 61 | + } |
| 62 | + |
| 63 | + private function getClassOptions(PhabricatorConfigOption $option) { |
| 64 | + $symbols = id(new PhutilSymbolLoader()) |
| 65 | + ->setType('class') |
| 66 | + ->setAncestorClass($option->getBaseClass()) |
| 67 | + ->setConcreteOnly(true) |
| 68 | + ->selectSymbolsWithoutLoading(); |
| 69 | + |
| 70 | + $map = ipull($symbols, 'name', 'name'); |
| 71 | + asort($map); |
| 72 | + |
| 73 | + return $map; |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments