Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Develop / TranslationCleanupCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Develop\TranslationCleanupCommand.
6  */
7
8 namespace Drupal\Console\Command\Develop;
9
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Console\Input\InputArgument;
14 use Symfony\Component\Finder\Finder;
15 use Symfony\Component\Yaml\Parser;
16 use Symfony\Component\Console\Command\Command;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Console\Core\Command\Shared\CommandTrait;
19 use Drupal\Console\Core\Utils\ConfigurationManager;
20
21 class TranslationCleanupCommand extends Command
22 {
23     use CommandTrait;
24
25     /**
26      * @var string
27      */
28     protected $consoleRoot;
29
30     /**
31      * @var ConfigurationManager
32      */
33     protected $configurationManager;
34
35     /**
36      * TranslationCleanupCommand constructor.
37      *
38      * @param $consoleRoot
39      * @param configurationManager $configurationManager
40      */
41     public function __construct(
42         $consoleRoot,
43         ConfigurationManager $configurationManager
44     ) {
45         $this->consoleRoot = $consoleRoot;
46         $this->configurationManager = $configurationManager;
47         parent::__construct();
48     }
49
50     /**
51      * {@inheritdoc}
52      */
53
54     protected function configure()
55     {
56         $this
57             ->setName('translation:cleanup')
58             ->setDescription($this->trans('commands.translation.cleanup.description'))
59             ->addArgument(
60                 'language',
61                 InputArgument::OPTIONAL,
62                 $this->trans('commands.translation.cleanup.arguments.language'),
63                 null
64             );
65     }
66
67     /**
68      * {@inheritdoc}
69      */
70     protected function execute(InputInterface $input, OutputInterface $output)
71     {
72         $io = new DrupalStyle($input, $output);
73
74         $language = $input->getArgument('language');
75
76         $languages = $this->configurationManager->getConfiguration()->get('application.languages');
77         unset($languages['en']);
78
79         if ($language && !isset($languages[$language])) {
80             $io->error(
81                 sprintf(
82                     $this->trans('commands.translation.cleanup.messages.invalid-language'),
83                     $language
84                 )
85             );
86             return 1;
87         }
88
89         if ($language) {
90             $languages = [$language => $languages[$language]];
91         }
92
93         $this->cleanupTranslations($io, $language, $languages);
94
95         $io->success(
96             $this->trans('commands.translation.cleanup.messages.success')
97         );
98     }
99
100     protected function cleanupTranslations($io, $language = null, $languages)
101     {
102         $finder = new Finder();
103
104         foreach ($languages as $langCode => $languageName) {
105             if (file_exists($this->consoleRoot . sprintf(DRUPAL_CONSOLE_LANGUAGE, $langCode))) {
106                 foreach ($finder->files()->name('*.yml')->in($this->consoleRoot . sprintf(DRUPAL_CONSOLE_LANGUAGE, $langCode)) as $file) {
107                     $filename = $file->getBasename('.yml');
108                     if (!file_exists($this->consoleRoot . sprintf(DRUPAL_CONSOLE_LANGUAGE, 'en') . $filename . '.yml')) {
109                         $io->info(
110                             sprintf(
111                                 $this->trans('commands.translation.cleanup.messages.file-deleted'),
112                                 $filename,
113                                 $languageName
114                             )
115                         );
116                         unlink($this->consoleRoot . sprintf(DRUPAL_CONSOLE_LANGUAGE, $langCode). '/' . $filename . '.yml');
117                     }
118                 }
119             }
120         }
121     }
122 }