1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Config; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
|
|
class DeleteCommand extends AbstractConfigCommand |
11
|
|
|
{ |
12
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
$this |
15
|
|
|
->setName('config:delete') |
16
|
|
|
->setDescription('Deletes a store config item') |
17
|
|
|
->addArgument('path', InputArgument::REQUIRED, 'The config path') |
18
|
|
|
->addOption( |
19
|
|
|
'scope', |
20
|
|
|
null, |
21
|
|
|
InputOption::VALUE_OPTIONAL, |
22
|
|
|
'The config value\'s scope (default, websites, stores)', |
23
|
|
|
'default' |
24
|
|
|
) |
25
|
|
|
->addOption('scope-id', null, InputOption::VALUE_OPTIONAL, 'The config value\'s scope ID', '0') |
26
|
|
|
->addOption('all', null, InputOption::VALUE_NONE, 'Delete all entries by path') |
27
|
|
|
; |
28
|
|
|
|
29
|
|
|
$help = <<<HELP |
30
|
|
|
To delete all entries if a path you can set the option --all. |
31
|
|
|
HELP; |
32
|
|
|
$this->setHelp($help); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param InputInterface $input |
37
|
|
|
* @param OutputInterface $output |
38
|
|
|
* |
39
|
|
|
* @return int|void |
40
|
|
|
*/ |
41
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
42
|
|
|
{ |
43
|
|
|
$this->detectMagento($output, true); |
44
|
|
|
|
45
|
|
|
if (!$this->initMagento()) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$deleted = array(); |
50
|
|
|
|
51
|
|
|
$scope = $this->_validateScopeParam($input->getOption('scope')); |
52
|
|
|
$scopeId = $this->_convertScopeIdParam($scope, $input->getOption('scope-id')); |
53
|
|
|
|
54
|
|
|
$path = $input->getArgument('path'); |
55
|
|
|
|
56
|
|
|
if (false !== strstr($path, '*')) { |
57
|
|
|
$paths = $this->expandPathPattern($input, $path); |
58
|
|
|
} else { |
59
|
|
|
$paths = array($path); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
foreach ($paths as $path) { |
63
|
|
|
$deleted = array_merge($deleted, $this->_deletePath($input, $path, $scopeId)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (count($deleted) > 0) { |
67
|
|
|
$this->getHelper('table') |
68
|
|
|
->setHeaders(array('deleted path', 'scope', 'id')) |
69
|
|
|
->setRows($deleted) |
70
|
|
|
->render($output); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param InputInterface $input |
76
|
|
|
* @param string $path |
77
|
|
|
* @param string $scopeId |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
protected function _deletePath(InputInterface $input, $path, $scopeId) |
82
|
|
|
{ |
83
|
|
|
$deleted = array(); |
84
|
|
|
if ($input->getOption('all')) { |
85
|
|
|
// Default |
86
|
|
|
$deleted[] = $this->deleteConfigEntry($path, 'default', 0); |
87
|
|
|
|
88
|
|
|
// Delete websites |
89
|
|
|
foreach (\Mage::app()->getWebsites() as $website) { |
90
|
|
|
$deleted[] = $this->deleteConfigEntry($path, 'websites', $website->getId()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Delete stores |
94
|
|
|
foreach (\Mage::app()->getStores() as $store) { |
95
|
|
|
$deleted[] = $this->deleteConfigEntry($path, 'stores', $store->getId()); |
96
|
|
|
} |
97
|
|
|
} else { |
98
|
|
|
$deleted[] = $this->deleteConfigEntry($path, $input->getOption('scope'), $scopeId); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $deleted; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $pattern |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
private function expandPathPattern($input, $pattern) |
109
|
|
|
{ |
110
|
|
|
$paths = array(); |
111
|
|
|
|
112
|
|
|
/* @var $collection \Mage_Core_Model_Resource_Db_Collection_Abstract */ |
113
|
|
|
$collection = $this->_getConfigDataModel()->getCollection(); |
114
|
|
|
|
115
|
|
|
$likePattern = str_replace('*', '%', $pattern); |
116
|
|
|
$collection->addFieldToFilter('path', array('like' => $likePattern)); |
117
|
|
|
|
118
|
|
|
if ($scope = $input->getOption('scope')) { |
119
|
|
|
$collection->addFieldToFilter('scope', array('eq' => $scope)); |
120
|
|
|
} |
121
|
|
|
$collection->addOrder('path', 'ASC'); |
122
|
|
|
|
123
|
|
|
foreach ($collection as $item) { |
124
|
|
|
$paths[] = $item->getPath(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $paths; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Delete concrete entry from config table specified by path, scope and scope-id |
132
|
|
|
* |
133
|
|
|
* @param string $path |
134
|
|
|
* @param string $scope |
135
|
|
|
* @param int $scopeId |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
private function deleteConfigEntry($path, $scope, $scopeId) |
140
|
|
|
{ |
141
|
|
|
$config = $this->_getConfigModel(); |
142
|
|
|
|
143
|
|
|
$config->deleteConfig( |
144
|
|
|
$path, |
145
|
|
|
$scope, |
146
|
|
|
$scopeId |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
return array( |
150
|
|
|
'path' => $path, |
151
|
|
|
'scope' => $scope, |
152
|
|
|
'scopeId' => $scopeId, |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
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.