[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/config/management/ -> PhabricatorConfigManagementSetWorkflow.php (source)

   1  <?php
   2  
   3  final class PhabricatorConfigManagementSetWorkflow
   4    extends PhabricatorConfigManagementWorkflow {
   5  
   6    protected function didConstruct() {
   7      $this
   8        ->setName('set')
   9        ->setExamples('**set** __key__ __value__')
  10        ->setSynopsis(pht('Set a local configuration value.'))
  11        ->setArguments(
  12          array(
  13            array(
  14              'name'  => 'database',
  15              'help'  => pht('Update configuration in the database instead of '.
  16              'in local configuration.'),
  17            ),
  18            array(
  19              'name'      => 'args',
  20              'wildcard'  => true,
  21            ),
  22          ));
  23    }
  24  
  25    public function execute(PhutilArgumentParser $args) {
  26      $console = PhutilConsole::getConsole();
  27      $argv = $args->getArg('args');
  28      if (count($argv) == 0) {
  29        throw new PhutilArgumentUsageException(pht(
  30          'Specify a configuration key and a value to set it to.'));
  31      }
  32  
  33      $key = $argv[0];
  34  
  35      if (count($argv) == 1) {
  36        throw new PhutilArgumentUsageException(pht(
  37          "Specify a value to set the key '%s' to.",
  38          $key));
  39      }
  40  
  41      $value = $argv[1];
  42  
  43      if (count($argv) > 2) {
  44        throw new PhutilArgumentUsageException(pht(
  45          'Too many arguments: expected one key and one value.'));
  46      }
  47  
  48      $options = PhabricatorApplicationConfigOptions::loadAllOptions();
  49      if (empty($options[$key])) {
  50        throw new PhutilArgumentUsageException(pht(
  51          "No such configuration key '%s'! Use `config list` to list all ".
  52          "keys.",
  53          $key));
  54      }
  55  
  56      $option = $options[$key];
  57  
  58      $type = $option->getType();
  59      switch ($type) {
  60        case 'string':
  61        case 'class':
  62        case 'enum':
  63          $value = (string)$value;
  64          break;
  65        case 'int':
  66          if (!ctype_digit($value)) {
  67            throw new PhutilArgumentUsageException(pht(
  68              "Config key '%s' is of type '%s'. Specify an integer.",
  69              $key,
  70              $type));
  71          }
  72          $value = (int)$value;
  73          break;
  74        case 'bool':
  75          if ($value == 'true') {
  76            $value = true;
  77          } else if ($value == 'false') {
  78            $value = false;
  79          } else {
  80            throw new PhutilArgumentUsageException(pht(
  81              "Config key '%s' is of type '%s'. ".
  82              "Specify 'true' or 'false'.",
  83              $key,
  84              $type));
  85          }
  86          break;
  87        default:
  88          $value = json_decode($value, true);
  89          if (!is_array($value)) {
  90            throw new PhutilArgumentUsageException(pht(
  91              "Config key '%s' is of type '%s'. Specify it in JSON.",
  92              $key,
  93              $type));
  94          }
  95          break;
  96      }
  97      $use_database = $args->getArg('database');
  98      if ($option->getLocked() && $use_database) {
  99        throw new PhutilArgumentUsageException(pht(
 100          "Config key '%s' is locked and can only be set in local ".
 101          'configuration.',
 102          $key));
 103      }
 104  
 105      try {
 106        $option->getGroup()->validateOption($option, $value);
 107      } catch (PhabricatorConfigValidationException $validation) {
 108        // Convert this into a usage exception so we don't dump a stack trace.
 109        throw new PhutilArgumentUsageException($validation->getMessage());
 110      }
 111  
 112      if ($use_database) {
 113        $config_type = 'database';
 114        PhabricatorConfigEditor::storeNewValue(
 115          $this->getViewer(),
 116          id(new PhabricatorConfigEntry())
 117            ->loadOneWhere('namespace = %s AND key = %s', 'default', $key),
 118          $value,
 119          PhabricatorContentSource::newConsoleSource());
 120      } else {
 121        $config_type = 'local';
 122        id(new PhabricatorConfigLocalSource())
 123          ->setKeys(array($key => $value));
 124      }
 125  
 126      $console->writeOut(
 127        pht("Set '%s' in %s configuration.", $key, $config_type)."\n");
 128    }
 129  
 130  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1