UninstallLanguageCommand.php 4.6 KB

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