Skip to content

Commit 6a0e645

Browse files
axiNyholm
andcommitted
Add command to delete empty messages (#349)
* Add command to delete empty messages * Updated baseline Co-authored-by: Tobias Nyholm <tobias.nyholm@gmail.com>
1 parent 0f9cddc commit 6a0e645

File tree

3 files changed

+141
-1
lines changed

3 files changed

+141
-1
lines changed

Catalogue/CatalogueManager.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function findMessages(array $config = []): array
8282
$isNew = $config['isNew'] ?? null;
8383
$isObsolete = $config['isObsolete'] ?? null;
8484
$isApproved = $config['isApproved'] ?? null;
85+
$isEmpty = $config['isEmpty'] ?? null;
8586

8687
$messages = [];
8788
$catalogues = [];
@@ -106,7 +107,7 @@ public function findMessages(array $config = []): array
106107
}
107108
}
108109

109-
$messages = \array_filter($messages, static function (CatalogueMessage $m) use ($isNew, $isObsolete, $isApproved) {
110+
$messages = \array_filter($messages, static function (CatalogueMessage $m) use ($isNew, $isObsolete, $isApproved, $isEmpty) {
110111
if (null !== $isNew && $m->isNew() !== $isNew) {
111112
return false;
112113
}
@@ -116,6 +117,9 @@ public function findMessages(array $config = []): array
116117
if (null !== $isApproved && $m->isApproved() !== $isApproved) {
117118
return false;
118119
}
120+
if (null !== $isEmpty && empty($m->getMessage()) !== $isEmpty) {
121+
return false;
122+
}
119123

120124
return true;
121125
});

Command/DeleteEmptyCommand.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <tobias.nyholm@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\Bundle\Command;
13+
14+
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Helper\ProgressBar;
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Input\InputOption;
19+
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Console\Question\ConfirmationQuestion;
21+
use Translation\Bundle\Catalogue\CatalogueFetcher;
22+
use Translation\Bundle\Catalogue\CatalogueManager;
23+
use Translation\Bundle\Service\ConfigurationManager;
24+
use Translation\Bundle\Service\StorageManager;
25+
26+
/**
27+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
28+
*/
29+
class DeleteEmptyCommand extends Command
30+
{
31+
use BundleTrait;
32+
use StorageTrait;
33+
34+
protected static $defaultName = 'translation:delete-empty';
35+
36+
/**
37+
* @var ConfigurationManager
38+
*/
39+
private $configurationManager;
40+
41+
/**
42+
* @var CatalogueManager
43+
*/
44+
private $catalogueManager;
45+
46+
/**
47+
* @var CatalogueFetcher
48+
*/
49+
private $catalogueFetcher;
50+
51+
public function __construct(
52+
StorageManager $storageManager,
53+
ConfigurationManager $configurationManager,
54+
CatalogueManager $catalogueManager,
55+
CatalogueFetcher $catalogueFetcher
56+
) {
57+
$this->storageManager = $storageManager;
58+
$this->configurationManager = $configurationManager;
59+
$this->catalogueManager = $catalogueManager;
60+
$this->catalogueFetcher = $catalogueFetcher;
61+
parent::__construct();
62+
}
63+
64+
protected function configure(): void
65+
{
66+
$this
67+
->setName(self::$defaultName)
68+
->setDescription('Delete all translations currently empty.')
69+
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
70+
->addArgument('locale', InputArgument::OPTIONAL, 'The locale to use. If omitted, we use all configured locales.', null)
71+
->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want remove translations from.')
72+
;
73+
}
74+
75+
protected function execute(InputInterface $input, OutputInterface $output): int
76+
{
77+
$configName = $input->getArgument('configuration');
78+
$locales = [];
79+
if (null !== $inputLocale = $input->getArgument('locale')) {
80+
$locales = [$inputLocale];
81+
}
82+
83+
$config = $this->configurationManager->getConfiguration($configName);
84+
$this->configureBundleDirs($input, $config);
85+
$this->catalogueManager->load($this->catalogueFetcher->getCatalogues($config, $locales));
86+
87+
$storage = $this->getStorage($configName);
88+
$messages = $this->catalogueManager->findMessages(['locale' => $inputLocale, 'isEmpty' => true]);
89+
90+
$messageCount = \count($messages);
91+
if (0 === $messageCount) {
92+
$output->writeln('No messages are empty');
93+
94+
return 0;
95+
}
96+
97+
if ($input->isInteractive()) {
98+
$helper = $this->getHelper('question');
99+
$question = new ConfirmationQuestion(\sprintf('You are about to remove %d translations. Do you wish to continue? (y/N) ', $messageCount), false);
100+
if (!$helper->ask($input, $output, $question)) {
101+
return 0;
102+
}
103+
}
104+
105+
$progress = null;
106+
if (OutputInterface::VERBOSITY_NORMAL === $output->getVerbosity() && OutputInterface::VERBOSITY_QUIET !== $output->getVerbosity()) {
107+
$progress = new ProgressBar($output, $messageCount);
108+
}
109+
foreach ($messages as $message) {
110+
$storage->delete($message->getLocale(), $message->getDomain(), $message->getKey());
111+
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
112+
$output->writeln(\sprintf(
113+
'Deleted empty message "<info>%s</info>" from domain "<info>%s</info>" and locale "<info>%s</info>"',
114+
$message->getKey(),
115+
$message->getDomain(),
116+
$message->getLocale()
117+
));
118+
}
119+
120+
if ($progress) {
121+
$progress->advance();
122+
}
123+
}
124+
125+
if ($progress) {
126+
$progress->finish();
127+
}
128+
129+
return 0;
130+
}
131+
}

phpstan-baseline.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
parameters:
22
ignoreErrors:
3+
-
4+
message: "#^Call to an undefined method Symfony\\\\Component\\\\Console\\\\Application\\:\\:getKernel\\(\\)\\.$#"
5+
count: 1
6+
path: Command/DeleteEmptyCommand.php
7+
38
-
49
message: "#^Call to an undefined method Symfony\\\\Component\\\\Console\\\\Application\\:\\:getKernel\\(\\)\\.$#"
510
count: 1

0 commit comments

Comments
 (0)