1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Cli\Command; |
4
|
|
|
|
5
|
|
|
use Magium\Cli\ConfigurationPathInterface; |
6
|
|
|
use Magium\NotFoundException; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Zend\Config\Config; |
13
|
|
|
use Zend\Config\Writer\Json; |
14
|
|
|
|
15
|
|
|
class SetSetting extends Command implements ConfigurationPathInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
protected $path; |
19
|
|
|
|
20
|
|
|
public function setPath($path) |
21
|
|
|
{ |
22
|
|
|
$this->path = $path; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$this->setName('config:set'); |
28
|
|
|
$this->setDescription('Modifies a setting'); |
29
|
|
|
$this->addArgument('name', InputArgument::REQUIRED, 'The name of the setting'); |
30
|
|
|
$this->addArgument('value', InputArgument::REQUIRED, 'The value of the setting'); |
31
|
|
|
$this->addOption('json', null, InputOption::VALUE_NONE, 'If set, this will filter the value through json_decode().'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
35
|
|
|
{ |
36
|
|
|
if (!file_exists($this->path . '/magium.json')) { |
37
|
|
|
throw new NotFoundException('Configuration file not found. Please execute magium:init.'); |
38
|
|
|
} |
39
|
|
|
$reader = new \Zend\Config\Reader\Json(); |
40
|
|
|
$config = new Config($reader->fromFile($this->path . '/magium.json'), true); |
41
|
|
|
|
42
|
|
|
$name = $input->getArgument('name'); |
43
|
|
|
$value = $input->getArgument('value'); |
44
|
|
|
$output->writeln($value); |
45
|
|
|
|
46
|
|
|
if ($input->getOption('json')) { |
47
|
|
|
$value = json_decode($value); |
48
|
|
|
} |
49
|
|
|
if (!$config->config) { |
50
|
|
|
$config->config = []; |
51
|
|
|
} |
52
|
|
|
$config->config->$name = $value; |
53
|
|
|
|
54
|
|
|
$writer = new Json(); |
55
|
|
|
$writer->toFile($this->path . '/magium.json', $config); |
56
|
|
|
$output->writeln(sprintf('Wrote value for "%s" to %s/magium.json', $name, $this->path)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.