1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Translation\Console\Command;
use Magento\Framework\App\Cache;
use Magento\Framework\Composer\ComposerInformation;
use Magento\Framework\Composer\DependencyChecker;
use Magento\Framework\Composer\Remove;
use Magento\Framework\Setup\BackupRollbackFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Command for uninstalling language and backup-code feature
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class UninstallLanguageCommand extends Command
{
/**
* Language code argument name
*/
const PACKAGE_ARGUMENT = 'package';
/**
* Backup-code option name
*/
const BACKUP_CODE_OPTION = 'backup-code';
/**
* @var DependencyChecker
*/
private $dependencyChecker;
/**
* @var Remove
*/
private $remove;
/**
* @var ComposerInformation
*/
private $composerInfo;
/**
* @var Cache
*/
private $cache;
/**
* @var BackupRollbackFactory
*/
private $backupRollbackFactory;
/**
* Inject dependencies
*
* @param DependencyChecker $dependencyChecker
* @param Remove $remove
* @param ComposerInformation $composerInfo
* @param Cache $cache
* @param BackupRollbackFactory $backupRollbackFactory
*/
public function __construct(
DependencyChecker $dependencyChecker,
Remove $remove,
ComposerInformation $composerInfo,
Cache $cache,
BackupRollbackFactory $backupRollbackFactory
) {
$this->dependencyChecker = $dependencyChecker;
$this->remove = $remove;
$this->composerInfo = $composerInfo;
$this->cache = $cache;
$this->backupRollbackFactory = $backupRollbackFactory;
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('i18n:uninstall')
->setDescription('Uninstalls language packages')
->setDefinition([
new InputArgument(
self::PACKAGE_ARGUMENT,
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
'Language package name'
),
new InputOption(
self::BACKUP_CODE_OPTION,
'-b',
InputOption::VALUE_NONE,
'Take code and configuration files backup (excluding temporary files)'
),
]);
parent::configure();
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$languages = $input->getArgument(self::PACKAGE_ARGUMENT);
$packagesToRemove = [];
$dependencies = $this->dependencyChecker->checkDependencies($languages, true);
foreach ($languages as $package) {
if (!$this->validate($package)) {
$output->writeln("<info>Package $package is not a Magento language and will be skipped.</info>");
} else {
if (sizeof($dependencies[$package]) > 0) {
$output->writeln("<info>Package $package has dependencies and will be skipped.</info>");
} else {
$packagesToRemove[] = $package;
}
}
}
if ($packagesToRemove !== []) {
if ($input->getOption(self::BACKUP_CODE_OPTION)) {
$backupRestore = $this->backupRollbackFactory->create($output);
$backupRestore->codeBackup(time());
} else {
$output->writeln('<info>You are removing language package without a code backup.</info>');
}
$output->writeln($this->remove->remove($packagesToRemove));
$this->cache->clean();
} else {
$output->writeln('<info>Nothing is removed.</info>');
}
}
/**
* Validates user input
*
* @param string $package
*
* @return bool
*/
private function validate($package)
{
$installedPackages = $this->composerInfo->getRootRequiredPackageTypesByName();
if (isset($installedPackages[$package]) && $installedPackages[$package] === 'magento2-language') {
return true;
}
return false;
}
}