Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Catalogue/CatalogueManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function findMessages(array $config = []): array
$isNew = $config['isNew'] ?? null;
$isObsolete = $config['isObsolete'] ?? null;
$isApproved = $config['isApproved'] ?? null;
$isEmpty = $config['isEmpty'] ?? null;

$messages = [];
$catalogues = [];
Expand All @@ -106,7 +107,7 @@ public function findMessages(array $config = []): array
}
}

$messages = \array_filter($messages, static function (CatalogueMessage $m) use ($isNew, $isObsolete, $isApproved) {
$messages = \array_filter($messages, static function (CatalogueMessage $m) use ($isNew, $isObsolete, $isApproved, $isEmpty) {
if (null !== $isNew && $m->isNew() !== $isNew) {
return false;
}
Expand All @@ -116,6 +117,9 @@ public function findMessages(array $config = []): array
if (null !== $isApproved && $m->isApproved() !== $isApproved) {
return false;
}
if (null !== $isEmpty && empty($m->getMessage()) !== $isEmpty) {
return false;
}

return true;
});
Expand Down
131 changes: 131 additions & 0 deletions Command/DeleteEmptyCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Bundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Translation\Bundle\Catalogue\CatalogueFetcher;
use Translation\Bundle\Catalogue\CatalogueManager;
use Translation\Bundle\Service\ConfigurationManager;
use Translation\Bundle\Service\StorageManager;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class DeleteEmptyCommand extends Command
{
use BundleTrait;
use StorageTrait;

protected static $defaultName = 'translation:delete-empty';

/**
* @var ConfigurationManager
*/
private $configurationManager;

/**
* @var CatalogueManager
*/
private $catalogueManager;

/**
* @var CatalogueFetcher
*/
private $catalogueFetcher;

public function __construct(
StorageManager $storageManager,
ConfigurationManager $configurationManager,
CatalogueManager $catalogueManager,
CatalogueFetcher $catalogueFetcher
) {
$this->storageManager = $storageManager;
$this->configurationManager = $configurationManager;
$this->catalogueManager = $catalogueManager;
$this->catalogueFetcher = $catalogueFetcher;
parent::__construct();
}

protected function configure(): void
{
$this
->setName(self::$defaultName)
->setDescription('Delete all translations currently empty.')
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
->addArgument('locale', InputArgument::OPTIONAL, 'The locale to use. If omitted, we use all configured locales.', null)
->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want remove translations from.')
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$configName = $input->getArgument('configuration');
$locales = [];
if (null !== $inputLocale = $input->getArgument('locale')) {
$locales = [$inputLocale];
}

$config = $this->configurationManager->getConfiguration($configName);
$this->configureBundleDirs($input, $config);
$this->catalogueManager->load($this->catalogueFetcher->getCatalogues($config, $locales));

$storage = $this->getStorage($configName);
$messages = $this->catalogueManager->findMessages(['locale' => $inputLocale, 'isEmpty' => true]);

$messageCount = \count($messages);
if (0 === $messageCount) {
$output->writeln('No messages are empty');

return 0;
}

if ($input->isInteractive()) {
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion(\sprintf('You are about to remove %d translations. Do you wish to continue? (y/N) ', $messageCount), false);
if (!$helper->ask($input, $output, $question)) {
return 0;
}
}

$progress = null;
if (OutputInterface::VERBOSITY_NORMAL === $output->getVerbosity() && OutputInterface::VERBOSITY_QUIET !== $output->getVerbosity()) {
$progress = new ProgressBar($output, $messageCount);
}
foreach ($messages as $message) {
$storage->delete($message->getLocale(), $message->getDomain(), $message->getKey());
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$output->writeln(\sprintf(
'Deleted empty message "<info>%s</info>" from domain "<info>%s</info>" and locale "<info>%s</info>"',
$message->getKey(),
$message->getDomain(),
$message->getLocale()
));
}

if ($progress) {
$progress->advance();
}
}

if ($progress) {
$progress->finish();
}

return 0;
}
}
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
parameters:
ignoreErrors:
-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Console\\\\Application\\:\\:getKernel\\(\\)\\.$#"
count: 1
path: Command/DeleteEmptyCommand.php

-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Console\\\\Application\\:\\:getKernel\\(\\)\\.$#"
count: 1
Expand Down