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 SetElementValue 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('element:set'); |
28
|
|
|
$this->setDescription('Modifies a property value for a configurable element'); |
29
|
|
|
$this->addArgument('class', InputArgument::REQUIRED, 'Need the full name of the class, including namespace'); |
30
|
|
|
$this->addArgument('property', InputArgument::REQUIRED, 'The name of the propery to set (this is not validated and is case sensitive)'); |
31
|
|
|
$this->addArgument('value', InputArgument::REQUIRED, 'Need the value of the setting'); |
32
|
|
|
$this->addOption('json', null, InputOption::VALUE_NONE, 'If set, this will filter the value through json_decode().'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
36
|
|
|
{ |
37
|
|
|
if (!file_exists($this->path . '/magium.json')) { |
38
|
|
|
throw new NotFoundException('Configuration file not found. Please execute magium:init.'); |
39
|
|
|
} |
40
|
|
|
$reader = new \Zend\Config\Reader\Json(); |
41
|
|
|
$config = new Config($reader->fromFile($this->path . '/magium.json'), true); |
42
|
|
|
|
43
|
|
|
$class = $input->getArgument('class'); |
44
|
|
|
$property = $input->getArgument('property'); |
45
|
|
|
$value = $input->getArgument('value'); |
46
|
|
|
|
47
|
|
|
if ($input->getOption('json')) { |
48
|
|
|
$value = json_decode($value); |
49
|
|
|
} |
50
|
|
|
if (!$config->magium) { |
51
|
|
|
$config->magium = []; |
52
|
|
|
} |
53
|
|
|
if (!class_exists($class)) { |
54
|
|
|
throw new NotFoundException('Could not find class: ' . $class . '. You might need to escape blackslashes (\\\\)'); |
55
|
|
|
} |
56
|
|
|
$class = strtolower($class); |
57
|
|
|
$s = $config->magium; |
58
|
|
|
if (!isset($s[$class])) { |
59
|
|
|
$s[$class] = []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$s[$class]->$property = $value; |
63
|
|
|
|
64
|
|
|
$writer = new Json(); |
65
|
|
|
$writer->toFile($this->path . '/magium.json', $config); |
66
|
|
|
$output->writeln(sprintf('Wrote value %s for "%s:%s" to %s/magium.json', $value, $class, $property, $this->path)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
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.