1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slack-cli package. |
5
|
|
|
* |
6
|
|
|
* (c) Cas Leentfaar <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CL\SlackCli\Command; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Cas Leentfaar <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class ConfigSetCommand extends AbstractCommand |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritDoc} |
25
|
|
|
*/ |
26
|
1 |
|
protected function configure() |
27
|
|
|
{ |
28
|
1 |
|
parent::configure(); |
29
|
|
|
|
30
|
1 |
|
$this->setName('config:set'); |
31
|
1 |
|
$this->setDescription('Stores the given setting and value in the global configuration'); |
32
|
1 |
|
$this->addArgument('setting', InputArgument::REQUIRED, 'The setting to use'); |
33
|
1 |
|
$this->addArgument('value', InputArgument::REQUIRED, 'The value to set for this setting'); |
34
|
1 |
|
$this->setHelp(<<<EOT |
35
|
|
|
The <info>config:set</info> command lets you store a given setting and value in the global configuration. |
36
|
|
|
|
37
|
|
|
To list all stored settings and values, use the <info>config.list</info> command. |
38
|
|
|
EOT |
39
|
1 |
|
); |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
1 |
|
public function execute(InputInterface $input, OutputInterface $output) |
46
|
|
|
{ |
47
|
1 |
|
$setting = $this->input->getArgument('setting'); |
48
|
1 |
|
$value = $this->input->getArgument('value'); |
49
|
|
|
|
50
|
1 |
|
return $this->setConfig($setting, $value); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function setConfig($setting, $value) |
54
|
|
|
{ |
55
|
|
|
// handle config values |
56
|
|
|
$uniqueConfigValues = [ |
57
|
1 |
|
'default_token' => ['is_string', function ($val) { |
58
|
1 |
|
return $val; |
59
|
1 |
|
}], |
60
|
1 |
|
]; |
61
|
|
|
|
62
|
1 |
|
foreach ($uniqueConfigValues as $name => $callbacks) { |
63
|
1 |
|
if ($setting === $name) { |
64
|
1 |
|
list($validator, $normalizer) = $callbacks; |
65
|
|
|
|
66
|
1 |
|
if (true !== $validation = $validator($value)) { |
67
|
|
|
throw new \RuntimeException(sprintf( |
68
|
|
|
'"%s" is an invalid value' . ($validation ? ' (' . $validation . ')' : ''), |
69
|
|
|
$value |
70
|
|
|
)); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
$this->getConfigSource()->addConfigSetting($setting, $normalizer($value)); |
74
|
|
|
|
75
|
1 |
|
$this->writeOk(sprintf( |
76
|
1 |
|
'Successfully changed value of <info>`%s`</info> to <comment>`%s`</comment>!', |
77
|
1 |
|
$setting, |
78
|
|
|
$value |
79
|
1 |
|
)); |
80
|
|
|
|
81
|
1 |
|
return 0; |
82
|
|
|
} |
83
|
1 |
|
} |
84
|
|
|
|
85
|
1 |
|
$this->writeError(sprintf('There is no setting with that name in the configuration: `%s`', $setting)); |
86
|
|
|
|
87
|
1 |
|
return 1; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|